上传一些TCP Modebus的测试代码

CigaretteSH
Jinhuan 9 months ago
parent 8c7ccf26cb
commit f8453203c4

@ -37,7 +37,6 @@ void CleanWorkThread::doWork()
qint64 spaceSize;
qint64 dirSize;
QString dirPath = g_conf_path.save_pics_path;
//QString iDriver = "D:/";
char drive[_MAX_DRIVE];
char dir_1[_MAX_DIR];
char fname[_MAX_FNAME];
@ -48,7 +47,6 @@ void CleanWorkThread::doWork()
LPCWSTR strDriver = (LPCWSTR)iDriver.utf16();
ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace;
qint64 gb = (1024 * 1024 * 1024);
//QString dir("D:/image");
QString dir(g_conf_path.save_pics_path + "/ALL");
if (delSelection == 2)

@ -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)

@ -1,16 +1,19 @@
#include <QMutex>
#include "modbus.h"
class PLCDevice
{
public:
QMutex g_mutex; //访问Modbus的互斥锁
modbus_t* g_modbus = NULL; //Modbus
bool g_plc_ok;
//[0]:1=连接,0=断开
uint8_t g_plc_status;
static bool init_plc(PLCDevice* PLCptr);
static bool init_plc_tcp(PLCDevice* PLCptr);
int write_bit_2_plc(int addr, int value);
int read_bits_from_plc(int addr, int nb, uint8_t* value);

@ -21,7 +21,7 @@ ConfPath g_conf_path;
SysConf g_sys_conf; //系统配置参数
ModbusConf g_modbus_conf; //modbus地址参数
PLCDevice* m_PLCDevice;
PLCDevice *m_PLCDevice,*m_PLCTCPDevice;
bool g_plc_dialog_open; //是否打开plc配置对话框
QDateTime g_ts_start;
@ -306,6 +306,9 @@ Cigarette::Cigarette(QWidget* parent)
//初始化通讯PLC
m_PLCDevice = new PLCDevice;
PLCDevice::init_plc(m_PLCDevice);
m_PLCTCPDevice = new PLCDevice;
PLCDevice::init_plc_tcp(m_PLCTCPDevice);
if (m_PLCDevice->g_plc_ok)
{
printf("Connected to dev!\n");

@ -3,8 +3,11 @@
#include <WS2tcpip.h>
#include <stdio.h>
#include <qtcpsocket.h>
#include <PLCDevice.h>
#pragma comment(lib, "ws2_32.lib")
extern PLCDevice* m_PLCTCPDevice;
void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_) {
ip = QString::fromStdString(ip_);
port = port_;
@ -85,9 +88,12 @@ void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
//std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString();
//mySocket->write(fileName.c_str());
char temp = num % 10;
mySocket->write((char*)&temp,sizeof(char));
//mySocket->write("\n");
mySocket->flush();
m_PLCTCPDevice->write_short_2_plc(0, 1, (uint16_t*)& temp);
}
#ifdef __TCPServer

Loading…
Cancel
Save