|
|
|
@ -1,14 +1,11 @@
|
|
|
|
|
#include "stdio.h"
|
|
|
|
|
#include "PLCDevice.h"
|
|
|
|
|
#include "modbus.h"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <common.h>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
extern SysConf g_sys_conf;
|
|
|
|
|
|
|
|
|
|
modbus_t* g_modbus = NULL; //Modbus TCP
|
|
|
|
|
|
|
|
|
|
bool PLCDevice::init_plc(PLCDevice* PLCptr)
|
|
|
|
|
{
|
|
|
|
|
// modbus_connect() 建立连接,成功返回0,错误返回-1
|
|
|
|
@ -17,15 +14,15 @@ bool PLCDevice::init_plc(PLCDevice* PLCptr)
|
|
|
|
|
const char* comport = g_sys_conf.ComPort.data();
|
|
|
|
|
qDebug() << "COM:" << comport;
|
|
|
|
|
//连接PLC
|
|
|
|
|
g_modbus = modbus_new_rtu(comport, 9600, 'N', 8, 1);
|
|
|
|
|
modbus_set_debug(g_modbus, 0); // 用flag设置debug调试标志位,flag=1时显示modbus消息的字节
|
|
|
|
|
modbus_set_response_timeout(g_modbus, 1, 0); // 设置响应超时
|
|
|
|
|
modbus_connect(g_modbus);
|
|
|
|
|
modbus_set_slave(g_modbus, 1); // 设置从站id
|
|
|
|
|
PLCptr->g_modbus = modbus_new_rtu(comport, 9600, 'N', 8, 1);
|
|
|
|
|
modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位,flag=1时显示modbus消息的字节
|
|
|
|
|
modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时
|
|
|
|
|
modbus_connect(PLCptr->g_modbus);
|
|
|
|
|
modbus_set_slave(PLCptr->g_modbus, 1); // 设置从站id
|
|
|
|
|
|
|
|
|
|
uint8_t data;
|
|
|
|
|
int ret = modbus_read_bits(g_modbus, 30000, 1, &data);
|
|
|
|
|
if (g_modbus && ret == -1) {
|
|
|
|
|
int ret = modbus_read_bits(PLCptr->g_modbus, 30000, 1, &data);
|
|
|
|
|
if (PLCptr->g_modbus && ret == -1) {
|
|
|
|
|
PLCptr->g_plc_ok = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
@ -35,6 +32,29 @@ bool PLCDevice::init_plc(PLCDevice* PLCptr)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr)
|
|
|
|
|
{
|
|
|
|
|
// modbus_connect() 建立连接,成功返回0,错误返回-1
|
|
|
|
|
// modbus_new_rtu 生成RTU的连接,建立成功则返回指向modbus_t结构的指针,否则将返回NULL/0
|
|
|
|
|
|
|
|
|
|
PLCptr->g_modbus = modbus_new_tcp("192.168.1.1", 7002);
|
|
|
|
|
qDebug("new g_modbus:\t%p\n", PLCptr->g_modbus);
|
|
|
|
|
modbus_set_debug(PLCptr->g_modbus, 0);
|
|
|
|
|
modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时
|
|
|
|
|
modbus_connect(PLCptr->g_modbus);
|
|
|
|
|
modbus_set_slave(PLCptr->g_modbus, 1); // 设置从机地址
|
|
|
|
|
int nRet = modbus_connect(PLCptr->g_modbus);// 连接设备
|
|
|
|
|
// 连接失败
|
|
|
|
|
if (-1 == nRet)
|
|
|
|
|
{
|
|
|
|
|
PLCptr->g_plc_ok = false;
|
|
|
|
|
qDebug("connect failed:%s\n", modbus_strerror(errno));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
PLCptr->g_plc_ok = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PLCDevice::disconnect_plc(void)
|
|
|
|
|
{
|
|
|
|
|
if (g_modbus)
|
|
|
|
|