Files
VX5-DiaphragmPump/modbus/modbusserial.cpp
T
2026-08-26 16:13:59 +08:00

470 lines
14 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "modbusserial.h"
#include "InkJetPrinter.h"
#include <QDebug>
#include <QProcess>
#include "LogInfo.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
#include "demo.h"
/* ----------------------- Defines ------------------------------------------*/
//#define REG_INPUT_START 1
//#define REG_INPUT_NREGS 400
//#define REG_HOLDING_START 40001
//#define REG_HOLDING_NREGS 400
//#define REG_INPUT_START 1000
//#define REG_INPUT_NREGS 4
//#define REG_HOLDING_START 2000
//#define REG_HOLDING_NREGS 130
/* ----------------------- Static variables ---------------------------------*/
//static USHORT usRegInputStart = REG_INPUT_START;
//USHORT* usRegInputBuf;
//static USHORT usRegHoldingStart = REG_HOLDING_START;
//USHORT* usRegHoldingBuf;
void *uart_receive_thread(void *arg);
ModbusSerial::ModbusSerial(ModbusPrintParas *mPara, const ModbusPrintStatus* mStatus)
{
#ifdef MODBUS
// const UCHAR ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
// qDebug()<< "modbusserial class\r\n";
modbus_status = 0;
modbus_up_QRCODE = 0;
port_num = 0;
input_registers = (uint16_t*) mStatus;
holding_registers = (uint16_t*) mPara;
QProcess process;
process.start("find /dev -name ttyUSB*");
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QString str_output = output;
QString str_tmp;
for(int j = 0; j < str_output.length(); j++)
{
if(str_output[j] > '0' && str_output[j] < '9')
str_tmp.append(str_output[j]);
}
port_num = str_tmp.toInt(false, 10);
qDebug()<<"port_num="<<port_num;
if(0 == active_modbus(holding_registers, input_registers, port_num))
{
qDebug()<< "active_modbus success!\r\n";
modbus_status = 1;
}else
{
modbus_status = 0;
qDebug()<< "active_modbus ENOERR!\r\n";
}
#ifdef __com_485_
pthread_t thread1;
int res = pthread_create(&thread1, NULL, uart_receive_thread, NULL);
if(res != 0) {
qDebug()<< "create mqtt connect thread fail \r\n";
}
else
{
qDebug()<< "create mqtt connect thread success \r\n";
}
#endif
modbus_timer = new QTimer();
//connect(modbus_timer, SIGNAL(timeout()), this, SLOT(processCommands()));
//modbus_timer->start(100);
#endif
}
ModbusSerial::~ModbusSerial(void)
{
//#ifdef MODBUS
// disconnect(&serial_thread, SIGNAL(started()), this, SLOT(vPollingThread()));
// if (serial_thread.isRunning()) {
// serial_thread.quit();
// serial_thread.wait();
// }
( void )eMBDisable( );
vSetPollingThreadState( STOPPED );
( void )eMBClose();
//#endif
if(modbus_timer)
{
delete modbus_timer;
}
}
extern InkJetPrinter *pInkJectPrinter;
//void ModbusSerial::vPollingThread()
//{
// Serial_SetPollingThreadState( RUNNING );
//
// if (modbus_status != 1) {
//// char fileName[100];
//// memset(fileName, 0, 100);
//// snprintf(fileName, 100, "/home/plg/doc/104.html");
//// qDebug()<<"fileName:"<<fileName;
//// emit openFile(fileName, false);
// return;
// }
//
// if( eMBEnable( ) == MB_ENOERR )
// {
// do
// {
// if( eMBPoll( ) != MB_ENOERR )
// {
// qDebug()<<"modbus poll error";
// break;
// }
// }
// while( Serial_GetPollingThreadState( ) != SHUTDOWN );
// }
//
// qDebug()<<"eMBEnable( ) fail";
// if( eMBEnable( ) == MB_ENOERR )
// ( void )eMBDisable( );
//
// Serial_SetPollingThreadState( STOPPED );
//
// return;
//}
//
//enum ThreadState ModbusSerial::Serial_GetPollingThreadState()
//{
// enum ThreadState eCurState;
//
// modbusThreadMutex.lock();
// eCurState = Serial_PollThreadState;
// modbusThreadMutex.unlock();
//
// return eCurState;
//}
//
//
//void ModbusSerial::Serial_SetPollingThreadState( enum ThreadState eNewState )
//{
// modbusThreadMutex.lock();
// Serial_PollThreadState = eNewState;
// modbusThreadMutex.unlock();
//}
qint32 ModbusSerial::getModbusStatus()
{
return modbus_status;
}
#if 0
void ModbusSerial::processCommands()
{
QProcess process;
process.start("find /dev -name ttyUSB*");
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QString str_output = output;
// qDebug()<< "str_output=\r\n"<<str_output;
#if 0
if( (true != str_output.contains("ttyUSB", Qt::CaseSensitive)) && (modbus_status==1) )
{
modbus_status = 0;
( void )eMBDisable( );
vSetPollingThreadState( STOPPED );
( void )eMBClose();
qDebug()<< "ttyUSB is gone\r\n";
}
#endif //改用板上485通讯
if( (modbus_status != 1) && (true == str_output.contains("ttyUSB", Qt::CaseSensitive)) )
{
QString str_tmp;
for(int j = 0; j < str_output.length(); j++)
{
if(str_output[j] > '0' && str_output[j] < '9')
str_tmp.append(str_output[j]);
}
port_num = str_tmp.toInt(false, 10);
if(0 == active_modbus(holding_registers, input_registers, port_num))
{
// qDebug() << "updateQRcode";
// modbus_up_QRCODE = true;
// emit updateQRcode();
qDebug()<< "active_modbus success!\r\n";
modbus_status = 1;
}else
{
modbus_status = 0;
// qDebug()<< "active_modbus ENOERR!\r\n";
}
}
// qDebug()<<"ModbusStatus: "<< modbus_status;
// qDebug()<<"PollingThreadState:"<< eGetPollingThreadState( );
if(modbus_status == 1)
{
if(holding_registers[0] == PRINT_START_COMMAND) {
holding_registers[0] = NULL_COMMAND;
LogInfo::setRunStatusText(tr("接收到开始打印命令"));
emit startPrint();
}
else if(holding_registers[0] == PRINT_PAUSE_COMMAND) {
holding_registers[0] = NULL_COMMAND;
LogInfo::setRunStatusText(tr("接收到暂停打印命令"));
emit pausePrint();
}
else if(holding_registers[0] == PRINT_STOP_COMMAND) {
holding_registers[0] = NULL_COMMAND;
LogInfo::setRunStatusText(tr("接收到停止打印命令"));
emit stopPrint();
}else if(holding_registers[0] == UPDATE_QRCODE) {
//else if(holding_registers[0] == 34) {
holding_registers[0] = NULL_COMMAND;
input_registers[0] = 0;//sysPara->modbusStatus.printFinishFlag = 0;//置打印标志
LogInfo::setRunStatusText(tr("接收到DM码数据命令"));
qDebug() << "updateQRcode";
modbus_up_QRCODE = true;
emit updateQRcode();
}
}
}
#endif
#ifdef __com_485_
class DevOp;
UCHAR revdata[100];
void ModbusSerial::processCommands()
{
if(revdata[0] == 10)
{
if(revdata[5] == PRINT_START_COMMAND) {
revdata[5] = NULL_COMMAND;
emit startPrint();
}
else if(revdata[5] == PRINT_PAUSE_COMMAND) {
revdata[5] = NULL_COMMAND;
emit pausePrint();
}
else if(revdata[5] == PRINT_STOP_COMMAND) {
revdata[5] = NULL_COMMAND;
emit stopPrint();
}else if(revdata[5] == UPDATE_QRCODE) {
//else if(holding_registers[0] == 34) {
revdata[5] = NULL_COMMAND;
qDebug() << "updateQRcode";
modbus_up_QRCODE = true;
emit updateQRcode();
}
}
}
void *uart_receive_thread(void *arg)
{
QByteArray temp;
QByteArray recResponse;
while(1)
{
if(DevOp::comForRFID->isOpen())
{
//int n = DevOp::comForRFID->write(temp);
//qDebug()<<"power com send "<<n<<"byte";
//return n;
}
else
{
qDebug()<<"read uart for RFID not open!";
return 0;
}
//sleep(200); // 延时200毫秒等待读写器返回数据,延时太小可能无法接收完整的数据包
//MySleep(200);
int n = DevOp::comForRFID->bytesAvailable();
if(n>0)
{
//recResponse.append(DevOp::comForRFID->read(n));
recResponse.clear();
recResponse = DevOp::comForRFID->read(n);
QString str;
str.sprintf("%d - %d - %d- %d", recResponse.at(0),recResponse.at(1),recResponse.at(2),recResponse.at(5));
qDebug()<<str;
memset(revdata,0,sizeof(revdata));
memcpy(revdata, recResponse, n);
if(revdata[0] == 10)
{
temp.clear();
temp.append((char *)revdata,n);
DevOp::comForRFID->write(temp);
qDebug()<<"write response data";
}
}
}
//return n;
}
#endif
qint32 ModbusSerial::processCommands(qint32 index)
{
qint32 rc = 0;
// 打印机指令
if(index == ((offsetof(ModbusPrintParas, printCommand) + sizeof(uint16_t))/2 - 1)) { // 打印控制
if(holding_registers[index] == PRINT_START_COMMAND) {
emit startPrint();
}
else if(holding_registers[index] == PRINT_PAUSE_COMMAND) {
emit pausePrint();
}
else if(holding_registers[index] == PRINT_STOP_COMMAND) {
emit stopPrint();
}
}
else if(index == ((offsetof(ModbusPrintParas, QRcode_buf) + 110)/2 - 1)) { // 传送DM码数据
qDebug() << "updateQRcode";
modbus_up_QRCODE = true;
emit updateQRcode();
}
else if(index == ((offsetof(ModbusPrintParas, selectFileName) + 100)/2 - 1)) { // 打开文件
char fileName[100];
int fileIndex = offsetof(ModbusPrintParas, selectFileName)/2;
memcpy(fileName, &holding_registers[fileIndex], 100);
//qDebug()<<"oopen file: "<<fileName;
if (strlen(fileName)) {
QFile mdOpenFile(QString("/home/plg/doc/%1").arg(fileName));
if (mdOpenFile.exists()) {
emit openFile(mdOpenFile.fileName(), false);
}
}
}
else if(index == ((offsetof(ModbusPrintParas, faultRusume) + sizeof(uint16_t))/2 - 1)) { // 报警恢复
emit faultResume();
}
else if(index == ((offsetof(ModbusPrintParas, setCalendar) + sizeof(calandar_and_dat_t))/2 - 1)) { // 设置计数器1,2
emit writeCalendar();
}
else if(index == ((offsetof(ModbusPrintParas, lineSpaceAndCharSpace) + sizeof(uint32_t))/2 - 1)) { // 设置字符间距和行间距
int lineSpace = holding_registers[index];
int charSpace = holding_registers[index-1];
//qDebug()<<"char space:"<< charSpace;
//qDebug()<<"line space:"<< lineSpace;
emit mbLineAndCharSpace(lineSpace, charSpace);
}
return rc;
}
extern "C" void CallProcessCommands(int index);
void CallProcessCommands(int index)
{
pInkJectPrinter->modbus->processCommands(index);
}
//
//eMBErrorCode
//eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
//{
// eMBErrorCode eStatus = MB_ENOERR;
// int iRegIndex;
//
// if( ( usAddress >= REG_INPUT_START )
// && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
// {
// usRegInputBuf = (USHORT*) pInkJectPrinter->modbus->input_registers;
// iRegIndex = ( int )( usAddress - usRegInputStart );
// while( usNRegs > 0 )
// {
// *pucRegBuffer++ = ( unsigned char )(( usRegInputBuf[iRegIndex] >> 8 )& 0xFF);
// *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
// iRegIndex++;
// usNRegs--;
// }
// }
// else
// {
// eStatus = MB_ENOREG;
// }
//
// return eStatus;
//}
//
//eMBErrorCode
//eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
//{
// eMBErrorCode eStatus = MB_ENOERR;
// int iRegIndex;
//
// if( ( usAddress >= REG_HOLDING_START ) &&
// ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
// {
//
// usRegHoldingBuf = (USHORT*) pInkJectPrinter->modbus->holding_registers;
// iRegIndex = ( int )( usAddress - usRegHoldingStart );
// switch ( eMode )
// {
// /* Pass current register values to the protocol stack. */
// case MB_REG_READ:
// while( usNRegs > 0 )
// {
// *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
// *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
// iRegIndex++;
// usNRegs--;
// }
// break;
//
// /* Update current register values with new values from the
// * protocol stack. */
// case MB_REG_WRITE:
// while( usNRegs > 0 )
// {
// usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
// usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
// pInkJectPrinter->modbus->processCommands(iRegIndex);
// iRegIndex++;
// usNRegs--;
// }
// }
// }
// else
// {
// eStatus = MB_ENOREG;
// }
// return eStatus;
//}
//
//
//eMBErrorCode
//eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
//{
// Q_UNUSED(pucRegBuffer);
// Q_UNUSED(usAddress);
// Q_UNUSED(usNCoils);
// Q_UNUSED(eMode);
// return MB_ENOREG;
//}
//
//eMBErrorCode
//eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
//{
// Q_UNUSED(pucRegBuffer);
// Q_UNUSED(usAddress);
// Q_UNUSED(usNDiscrete);
// return MB_ENOREG;
//}