尝试加入mobude tcp 从机设置

CigaretteSH
Jinhuan 12 months ago
parent 7583d17f52
commit dc5c0efdc7

@ -34,9 +34,7 @@ bool PLCDevice::init_plc(PLCDevice* PLCptr)
bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr) bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr)
{ {
// modbus_connect() 建立连接成功返回0错误返回-1 #ifdef __ModebusServer
// modbus_new_rtu 生成RTU的连接建立成功则返回指向modbus_t结构的指针否则将返回NULL/0
PLCptr->g_modbus = modbus_new_tcp("192.168.1.200", 2000); PLCptr->g_modbus = modbus_new_tcp("192.168.1.200", 2000);
modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位flag=1时显示modbus消息的字节 modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位flag=1时显示modbus消息的字节
modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时 modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时
@ -56,7 +54,25 @@ bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr)
PLCptr->g_plc_ok = true; PLCptr->g_plc_ok = true;
qDebug("TCP connect successful "); qDebug("TCP connect successful ");
} }
#endif
#ifdef __ModebusClient
PLCptr->g_modbus = modbus_new_tcp("127.0.0.1", 502);
modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位flag=1时显示modbus消息的字节
modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时
// 侦听主站连接
PLCptr->m_modbusSocket = modbus_tcp_listen(PLCptr->g_modbus, 1); // 最多支持的主机数量 1
/*设置线圈, 离散输入, 输入寄存器, 保持寄存器个数(数组元素个数))*/
PLCptr->mapping = modbus_mapping_new(PLCptr->m_numBits, PLCptr->m_numInputBits, PLCptr->m_numInputRegisters, PLCptr->m_numRegisters);
if (PLCptr->mapping == NULL)
{
fprintf(stderr, "Unable to assign mapping%s\n", modbus_strerror(errno));
modbus_free(PLCptr->g_modbus);
PLCptr->m_initialized = false;
return false;
}
PLCptr->m_initialized = true;
#endif
return true; return true;
} }
@ -106,9 +122,9 @@ int PLCDevice::write_short_2_plc(int addr, int nb, uint16_t* value)
{ {
if (g_plc_ok) if (g_plc_ok)
{ {
//g_mutex.lock(); g_mutex.lock();
int ret = modbus_write_registers(g_modbus, addr, nb, value); int ret = modbus_write_registers(g_modbus, addr, nb, value);
//g_mutex.unlock(); g_mutex.unlock();
//printf("modbus_write_registers:addr=%d,nb=%d,value=%d,ret=%d\n", addr, nb, *value, ret); //printf("modbus_write_registers:addr=%d,nb=%d,value=%d,ret=%d\n", addr, nb, *value, ret);
return ret; return ret;
} }
@ -133,3 +149,19 @@ int PLCDevice::read_short_from_plc(int addr, int nb, uint16_t* value)
return false; return false;
} }
} }
int PLCDevice::write_2_plc(int addr, int nb, uint16_t* value)
{
if (g_plc_ok)
{
//g_mutex.lock();
int ret = modbus_write_registers(g_modbus, addr, nb, value);
//g_mutex.unlock();
//printf("modbus_write_registers:addr=%d,nb=%d,value=%d,ret=%d\n", addr, nb, *value, ret);
return ret;
}
else {
//std::cout << "PLC no ok" << std::endl;
return false;
}
}

@ -1,6 +1,6 @@
#include <QMutex> #include <QMutex>
#include "modbus.h" #include "modbus.h"
#include "common.h"
class PLCDevice class PLCDevice
{ {
public: public:
@ -11,7 +11,17 @@ public:
//[0]:1=连接,0=断开 //[0]:1=连接,0=断开
uint8_t g_plc_status; uint8_t g_plc_status;
#ifdef __ModebusClient
int m_modbusSocket;
bool m_initialized;
modbus_mapping_t* mapping{ nullptr };
/*Mapping*/
int m_numBits{ 500 };
int m_numInputBits{ 500 };
int m_numRegisters{ 500 };
int m_numInputRegisters{ 500 };
#endif
static bool init_plc(PLCDevice* PLCptr); static bool init_plc(PLCDevice* PLCptr);
static bool init_plc_tcp(PLCDevice* PLCptr); static bool init_plc_tcp(PLCDevice* PLCptr);
@ -19,5 +29,6 @@ public:
int read_bits_from_plc(int addr, int nb, uint8_t* value); int read_bits_from_plc(int addr, int nb, uint8_t* value);
int write_short_2_plc(int addr, int nb, uint16_t* value); int write_short_2_plc(int addr, int nb, uint16_t* value);
int read_short_from_plc(int addr, int nb, uint16_t* value); int read_short_from_plc(int addr, int nb, uint16_t* value);
int write_2_plc(int addr, int nb, uint16_t* value);
bool disconnect_plc(void); bool disconnect_plc(void);
}; };

@ -5,25 +5,29 @@
#include "basecamera.h" #include "basecamera.h"
#include "QtCore\qdatetime.h" #include "QtCore\qdatetime.h"
//#define __DEBUGPATH //修改debug时候的路径 //#define __DEBUGPATH //修改debug时候的路径
//#define __DEBUG //debug信息输出功能 //#define __DEBUG //debug信息输出功能
//#define __UDPSend //网络发送功能
#define __TCPSend // TCP发送 //#define __UDPSend //网络发送功能
//#define __TCPServer // TCP服务器 #define __TCPSend // TCP发送
#define __ExportData // FTP发送 //#define __TCPServer // TCP服务器
#define __ExportData // FTP发送
//#define __ModebusServer // 建立modbus主机
#define __ModebusClient // 建立modbus从机
#define USB_BASLER_NEW_FW //使用basler定制固件 #define USB_BASLER_NEW_FW //使用basler定制固件
//#define IMM_PROCESS //拍照后立马处理,不等校验信号 //#define IMM_PROCESS //拍照后立马处理,不等校验信号
//#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号 //#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号
#define ONE_TIME_SHIFT //错开一拍发送反馈(默认错开两次) #define ONE_TIME_SHIFT //错开一拍发送反馈(默认错开两次)
#define AI_WARM_UP //AI识别开始前的热身动作
#define LICENSE_VERIFY //开启license文件校验
//CAP_FEED_BACK和DOUBLE_FEED_BACK不要一起开 //CAP_FEED_BACK和DOUBLE_FEED_BACK不要一起开
#if defined (ONE_TIME_SHIFT) #if defined (ONE_TIME_SHIFT)
//#define CAP_FEED_BACK //拍照时也检测有没有测试结果,有的话就反馈 //#define CAP_FEED_BACK //拍照时也检测有没有测试结果,有的话就反馈
//#define DOUBLE_FEED_BACK //一次ng两次反馈ng信号 //#define DOUBLE_FEED_BACK //一次ng两次反馈ng信号
#endif #endif
#define AI_WARM_UP //AI识别开始前的热身动作
//#define LICENSE_VERIFY //开启license文件校验
//#define identify_Hik_YSXID//识别海康相机YSXID //#define identify_Hik_YSXID//识别海康相机YSXID
//#define __ExportData // 输出检测数据到XML文档
//#define DRAW_RECT // 鼠标画框功能 //#define DRAW_RECT // 鼠标画框功能
#define SYNC_CAMERA //相机同步处理图片 #define SYNC_CAMERA //相机同步处理图片

@ -104,8 +104,13 @@ void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
{ {
val_[i] = asciiVals[i]; val_[i] = asciiVals[i];
} }
//m_PLCTCPDevice->write_short_2_plc(40000, 70, (uint16_t*)&val_);//小盒 #ifdef __ModebusServer
m_PLCTCPDevice->write_short_2_plc(40070, 70, (uint16_t*)&val_);//条盒 //m_PLCTCPDevice->write_2_plc(40000, 70, (uint16_t*)&val_);//小盒
m_PLCTCPDevice->write_2_plc(40070, 70, (uint16_t*)&val_);//条盒
#endif
#ifdef __ModebusClient
memcpy(&(m_PLCTCPDevice->mapping->tab_registers[0]),(uint16_t*)&val_,70);
#endif
now_ts = QDateTime::currentDateTime(); now_ts = QDateTime::currentDateTime();
qDebug() << "end-now_ts=" << now_ts; qDebug() << "end-now_ts=" << now_ts;

Loading…
Cancel
Save