-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModbusTcpClient.cpp
More file actions
executable file
·120 lines (87 loc) · 2.86 KB
/
ModbusTcpClient.cpp
File metadata and controls
executable file
·120 lines (87 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "ModbusTcpClient.h"
#include "videopanel.h"
#include<iostream>
#include <stdio.h>
using namespace std;
#define MODBUS_IP "192.168.1.232" //"192.168.250.1"
#define MODBUS_PORT 10000
using namespace std;
int mbStatus=0;
typedef struct mbPort{
int port;
int life;
}mp;
vector<mp> mbPortList;
ModbusTCPThread::ModbusTCPThread(QObject *parent) : QThread(parent)
{
}
void ModbusTCPThread::run()
{
// int ret=0;
// while(1){
// ret=_modbus->read_gpio();
// if(ret==1){
// emit getAlready();
// }
// msleep(_modbus->waitTime);
// }
}
ModbusTcpClient::ModbusTcpClient(VideoPanel *v)
{
//printf("[ModbusTcpClient] ip=%s port=%d\n", MODBUS_IP, MODBUS_PORT);
// create a modbus object
//_modbus = new modbus(MODBUS_IP, MODBUS_PORT);
// set slave id
// _modbus->modbus_set_slave_id(1);
// printf("[ModbusTcpClient] connect start\n");
// // connect with the server
// if (_modbus->modbus_connect())
// {
// printf("[ModbusTcpClient] connect success!\n");
// }
// else{
// printf("[ModbusTcpClient] connect failed!\n");
// }
//connect(this, &ModbusTcpClient::startTrigger, v, &VideoPanel::pollHardware);
//connect(this, &ModbusTcpClient::sendPLCstate, v, &VideoPanel::modbusRecvPLCstate);
// start thread
// ModbusTCPThread* modbusThread = new ModbusTCPThread(this);
// modbusThread->_modbus=_modbus;
// //connect(modbusThread, &ModbusTCPThread::getAlready, v, &VideoPanel::operAlready);
// printf("[ModbusTCPThread] ModbusTCPThread start running!\n");
// modbusThread->start();
// connect(v, &VideoPanel::modbussend_result, this, &ModbusTcpClient::send_result);
}
ModbusTcpClient::~ModbusTcpClient()
{
delete _modbus;
}
// send result ---- addr:x03 value: OK 1, NG 2, null 3
void ModbusTcpClient::send_result(int valueResult)
{
//printf("[ModbusTcpClient] 0x03 send result:%d\n", valueResult);
//_modbus->modbus_write_register(0x03, valueResult);
_modbus->modbus_write_io(0,0);
_modbus->modbus_write_io(1,0);
//_modbus->modbus_read_io();
//uint16_t read_input_regs[1];
//_modbus->modbus_read_holding_registers(0x03, 1, read_input_regs);
//printf("[ModbusTcpClient] 0x03 recv result:%d\n", read_input_regs[0]);
}
// send alarm ---- addr:x04 value: zhengchang 1, yichang 2(unload etc..)
void ModbusTcpClient::send_alarm(int valueAlarm)
{
printf("[ModbusTcpClient] 0x04 send result modbus:%d\n", valueAlarm);
_modbus->modbus_write_register(0x04, valueAlarm);
}
// recv No.5 Station modbus ---- addr:x02 value: weidaowei 1, daowei 2
void ModbusTcpClient::read_recv()
{
_modbus->modbus_read_holding_registers(0x02, 1, _numRecvRegs);
printf("[ModbusTcpClient] recv:%d", _numRecvRegs[0]);
if (_numRecvRegs[0] == 2)
{
_numRecvRegs[0] = 1;
emit startTrigger(2);
}
}