添加modbus从机的代码

CigaretteSH
Jinhuan 8 months ago
parent dc5c0efdc7
commit 45381dfdf2

@ -55,8 +55,8 @@ bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr)
qDebug("TCP connect successful ");
}
#endif
#ifdef __ModebusClient
#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); // 设置响应超时
@ -66,16 +66,117 @@ bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr)
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;
PLCptr->modSerThread = new std::thread(&PLCDevice::recieveMessages, PLCptr);
PLCptr->modSerThread->detach();
std::cout << "Running ModbusTcpSlave" << std::endl;
#endif
return true;
}
#ifdef __ModebusClient
/***************************************************************
* @file PLCDevice.cpp
* @author seer-txj
* @brief master
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
void PLCDevice::recieveMessages()
{
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
int fd_num = 0, fd_max = 0, ret = 0, i = 0, clnt_sock = -1;
fd_set reads, cpy_reads;
FD_ZERO(&reads);
FD_SET(m_modbusSocket, &reads);
fd_max = m_modbusSocket;
while (m_initialized)
{
cpy_reads = reads;
if ((fd_num = select(fd_max + 1, &cpy_reads, 0, 0, 0)) == -1)
break;
if (fd_num == 0)
continue;
for (i = 0; i < fd_max + 1; i++)
{
if (FD_ISSET(i, &cpy_reads))
{
if (i == m_modbusSocket)
{
clnt_sock = modbus_tcp_accept(g_modbus, &m_modbusSocket);
if ((m_modbusSocket == -1) || (clnt_sock == -1))
{
std::cerr << modbus_strerror(errno) << std::endl;
continue;
}
FD_SET(clnt_sock, &reads);
if (fd_max < clnt_sock)
fd_max = clnt_sock;
}
else
{
ret = modbus_receive(g_modbus, query);
if (ret == 0)
{
m_errCount = 0;
continue;
}
else if (ret > 0)
{
m_errCount = 0;
// 填写输出内容用于测试实际输出在threadSendTCP::sendData中实现这边只要reply
/* mapping->tab_registers[0] = 0;
mapping->tab_registers[1] = 11;
mapping->tab_registers[2] = 22;
mapping->tab_input_registers[0] = 0;
mapping->tab_input_registers[1] = 33;
mapping->tab_input_registers[2] = 44;
mapping->tab_bits[0] = 0;
mapping->tab_bits[1] = 1;
mapping->tab_bits[2] = 0;
mapping->tab_input_bits[0] = 1;
mapping->tab_input_bits[1] = 0;
mapping->tab_input_bits[2] = 1;*/
modbus_reply(g_modbus, query, sizeof(query), mapping);
}
else
{
modbus_set_error_recovery(g_modbus, MODBUS_ERROR_RECOVERY_NONE);
modbus_set_error_recovery(g_modbus, MODBUS_ERROR_RECOVERY_LINK);
modbus_close(g_modbus);
FD_CLR(i, &reads);
#ifdef _WIN32
closesocket(i);
#else
close(i);
#endif // _WIN32
m_errCount++;
}
if(m_errCount > 5)
{
m_initialized = false;
break;
}
}
}
}
}
m_initialized = false;
}
#endif
bool PLCDevice::disconnect_plc(void)
{
if (g_modbus)

@ -11,17 +11,21 @@ public:
//[0]:1=连接,0=断开
uint8_t g_plc_status;
#ifdef __ModebusClient
int m_modbusSocket;
bool m_initialized;
UINT8 m_errCount=0;
modbus_mapping_t* mapping{ nullptr };
/*Mapping*/
int m_numBits{ 500 };
int m_numInputBits{ 500 };
int m_numRegisters{ 500 };
int m_numInputRegisters{ 500 };
void recieveMessages();
std::thread *modSerThread;
#endif
static bool init_plc(PLCDevice* PLCptr);
static bool init_plc_tcp(PLCDevice* PLCptr);

@ -125,7 +125,13 @@ Cigarette::Cigarette(QWidget* parent)
delete dirinfo, dirinfo = nullptr;
}
else
{
#ifdef __DEBUGPATH
g_conf_path.config_path = "../conf/conf.txt";
#else
g_conf_path.config_path = DEFAULT_CONFPATH_PATH;
#endif
}
bool update_rotate = false;
if (!read_rotate_message()) {//读取旋转参数

@ -8,12 +8,15 @@
//#define __DEBUGPATH //修改debug时候的路径
//#define __DEBUG //debug信息输出功能
#define __ExportData // FTP发送
//#define __UDPSend //网络发送功能
#define __TCPSend // TCP发送
//#define __TCPServer // TCP服务器
#define __ExportData // FTP发送
//#define __ModebusServer // 建立modbus主机
#define __ModebusClient // 建立modbus从机
#if defined (__TCPSend)
//#define __TCPServer // TCP服务器
//#define __TCPClient // TCP客户端
//#define __ModebusServer // 建立modbus主机
#define __ModebusClient // 建立modbus从机
#endif
#define USB_BASLER_NEW_FW //使用basler定制固件
//#define IMM_PROCESS //拍照后立马处理,不等校验信号
@ -26,7 +29,7 @@
#endif
#define AI_WARM_UP //AI识别开始前的热身动作
//#define LICENSE_VERIFY //开启license文件校验
#define LICENSE_VERIFY //开启license文件校验
//#define identify_Hik_YSXID//识别海康相机YSXID
//#define DRAW_RECT // 鼠标画框功能
#define SYNC_CAMERA //相机同步处理图片

@ -35,34 +35,36 @@ void threadSendTCP::stop()
if (tcpServer->isListening())
tcpServer->close();
if(tcpServer) delete tcpServer;
#endif
#ifdef __TCPClient
mySocket->deleteLater();
if (mySocket) delete mySocket;
#endif
isLoop = false;
// wait();
mySocket->deleteLater();
if (mySocket) delete mySocket;
}
#ifndef __TCPServer
#ifdef __TCPClient
bool threadSendTCP::connectTCP() {
//mySocket = new QTcpSocket();
////mySocket = new QTcpServer();
//// 取消已有的连接
//mySocket->abort();
//// 连接服务器
//mySocket->connectToHost(ip, port);
//if (!mySocket->waitForConnected(100)) {
// qDebug() << "connect failed!";
// return false;
//}
//qDebug() << "connect successfully!";
//
mySocket = new QTcpSocket();
// 取消已有的连接
mySocket->abort();
// 连接服务器
mySocket->connectToHost(ip, port);
if (!mySocket->waitForConnected(100)) {
qDebug() << "connect failed!";
return false;
}
qDebug() << "connect successfully!";
return true;
}
#endif
void threadSendTCP::run()
{
#ifndef __TCPServer
#ifdef __TCPClient
if (!connectTCP())
{
qDebug() << "TCP connect error!";
@ -73,49 +75,56 @@ void threadSendTCP::run()
Local_TCP_Info_queue->take(TCPSendInfo);
num++;
#ifdef __TCPServer
if (ClientStatus == QAbstractSocket::ConnectedState)
{
sendData(&TCPSendInfo, num);
}
#else
sendData(&TCPSendInfo, num);
if (ClientStatus == QAbstractSocket::ConnectedState)
{
sendData(&TCPSendInfo, num);
}
#endif
#ifdef __TCPClient
sendData(&TCPSendInfo, num);
#endif
//mySocket->write("Hello! here is tcp client!\n");
//mySocket->flush();
}
}
void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
std::string fileName = TCPSendInfo->pics_name;
QDateTime ts_start = QDateTime::currentDateTime();
#if defined(__TCPServer) || defined(__TCPClient)
mySocket->write(fileName.c_str());
char temp = num % 10;
mySocket->write((char*)&temp,sizeof(char));
mySocket->write("\n");
mySocket->flush();
#endif
#if defined(__ModebusServer) || defined(__ModebusClient)
std::vector<int> asciiVals = stringToAscii(fileName);
//mySocket->write(fileName.c_str());
//char temp = num % 10;
//mySocket->write((char*)&temp,sizeof(char));
//mySocket->write("\n");
//mySocket->flush();
//
//遍历发送
int val_[35] = {};
QDateTime now_ts = QDateTime::currentDateTime();
qDebug() << "now_ts=" << now_ts;
for (int i = 0; i < asciiVals.size(); i++)
{
val_[i] = asciiVals[i];
}
#endif
#ifdef __ModebusServer
//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);
for(int i = 0;i<sizeof(val_)/sizeof(int);i++)
{
m_PLCTCPDevice->mapping->tab_registers[i] = val_[i];
m_PLCTCPDevice->mapping->tab_input_registers[i] = val_[i];
}
#endif
now_ts = QDateTime::currentDateTime();
qDebug() << "end-now_ts=" << now_ts;
QDateTime ts_end = QDateTime::currentDateTime();
int time_process = ts_start.msecsTo(ts_end);
qDebug() << "end-now_ts=" << time_process;
}
//字符串转ASCII
std::vector<int> threadSendTCP::stringToAscii(const std::string& str)
{
@ -132,12 +141,10 @@ void threadSendTCP::onNewConnection()
mySocket = tcpServer->nextPendingConnection(); //创建socket
qDebug() << "NewConnectionConnected";
ClientStatus = QAbstractSocket::ConnectedState;
}
void threadSendTCP::onClientConnected()
{
qDebug() << "ClientConnected";
}

@ -45,7 +45,7 @@ public:
wait();
}
void stop();
#ifndef __TCPServer
#ifdef __TCPClient
bool connectTCP();
#endif
protected:
@ -56,16 +56,20 @@ public:
void start_work();
void sendData(_TCPSendInfo* TCPSendInfo, int Num);
std::vector<int> stringToAscii(const std::string& str);
#ifdef __TCPServer
private slots:
void onNewConnection();
void onClientConnected();
void onClientDisconnected();
#endif
public:
SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue;
std::atomic_bool isLoop = { 0 };
#if defined(__TCPServer) || defined(__TCPClient)
QTcpSocket* mySocket = NULL;
#endif
#ifdef __TCPServer
QTcpServer *tcpServer = NULL;
QAbstractSocket::SocketState ClientStatus;

Loading…
Cancel
Save