#include "modbusserial.h" #include "InkJetPrinter.h" #include #include #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="<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:"< '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 "<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()<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: "<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; //}