#include "modbustcp.h" #include "InkJetPrinter.h" #include #include #include "../../LogInfo.h" #include #include /* ----------------------- Modbus includes ----------------------------------*/ #include "mb.h" #include "mbport.h" /* ----------------------- Defines ------------------------------------------*/ #define REG_INPUT_START 1 #define REG_INPUT_NREGS 400 #define REG_HOLDING_START 1//40001 //40001 #define REG_HOLDING_NREGS 400 /* ----------------------- Static variables ---------------------------------*/ static USHORT usRegInputStart = REG_INPUT_START; //static USHORT usRegInputBuf[REG_INPUT_NREGS]; static USHORT usRegHoldingStart = REG_HOLDING_START; //static USHORT usRegHoldingBuf[REG_HOLDING_NREGS]; USHORT* usRegInputBuf; USHORT* usRegHoldingBuf; ModbusTcp::ModbusTcp(ModbusPrintParas *mPara, const ModbusPrintStatus* mStatus) { #ifdef MODBUS modbus_status = 0; modbus_up_QRCODE = 0; if( eMBTCPInit( MB_TCP_PORT_USE_DEFAULT ) != MB_ENOERR ) { qDebug()<< "can't initialize modbus stack!\r\n"; modbus_status = -1; } else { eSetPollingThreadState( STOPPED ); /* CLI interface. */ //bDoExit = FALSE; } input_registers = (uint16_t*) mStatus; holding_registers = (uint16_t*) mPara; moveToThread(&m_thread); m_thread.start(); if (modbus_status != -1) { connect(&m_thread, SIGNAL(started()), this, SLOT(pvPollingThread())); modbus_status = 1; } #endif } ModbusTcp::~ModbusTcp(void) { #ifdef MODBUS disconnect(&m_thread, SIGNAL(started()), this, SLOT(pvPollingThread())); if (m_thread.isRunning()) { m_thread.quit(); m_thread.wait(); } ( void )eMBClose( ); #endif } extern InkJetPrinter *pInkJectPrinter; void ModbusTcp::pvPollingThread() { #ifdef MODBUS eSetPollingThreadState( RUNNING ); if (modbus_status != 1) { return; } if( eMBEnable( ) == MB_ENOERR ) { do { if( eMBPoll( ) != MB_ENOERR ) { qDebug()<<"modbus poll error"; break; } } while( eGetPollingThreadState( ) != SHUTDOWN ); } if( eMBEnable( ) == MB_ENOERR ) ( void )eMBDisable( ); eSetPollingThreadState( STOPPED ); #endif return; } enum ThreadState ModbusTcp::eGetPollingThreadState( ) { enum ThreadState eCurState; modbusThreadMutex.lock(); eCurState = ePollThreadState; modbusThreadMutex.unlock(); return eCurState; } void ModbusTcp::eSetPollingThreadState( enum ThreadState eNewState ) { modbusThreadMutex.lock(); ePollThreadState = eNewState; modbusThreadMutex.unlock(); } qint32 ModbusTcp::getModbusStatus() { return modbus_status; } qint32 ModbusTcp::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) + 240)/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: "<= 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; }