Files

291 lines
8.2 KiB
C++
Raw Permalink 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 "modbustcp.h"
#include "InkJetPrinter.h"
#include <QDebug>
#include <QFile>
#include "../../LogInfo.h"
#include <iostream>
#include <string>
/* ----------------------- 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: "<<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, printContent) + sizeof(uint16_t))/2 - 1)){
int content = holding_registers[index];
#ifdef PROJECT_PLC1
emit receivedPrintContent(content);
#endif
}
else if(index == ((offsetof(ModbusPrintParas, faultRusume) + sizeof(uint16_t))/2 - 1)) { // 报警恢复
emit faultResume();
}
else if(index == 113) { // 设置计数器1当前值
qDebug()<<"writeCalendar1"<< endl;
emit writeCalendar1();
}
else if(index == 117) { // 设置计数器1结束值
qDebug()<<"writeCalendar1EndValue"<< endl;
emit writeCalendar1EndValue();
}
else if(index == 119) { // 设置计数器2结束值
qDebug()<<"writeCalendar2EndValue"<< endl;
emit writeCalendar2EndValue();
}
else if(index == 121) { // 设置计数器2重复值
qDebug()<<"writeCalendar2RepeatValue"<< endl;
emit writeCalendar2RepeatValue();
}
else if(index == 129) { // 设置计数器2起始值
qDebug()<<"writeCalendar2InitValue"<< endl;
emit writeCalendar2InitValue();
}
else if(index == 136) { // 立即重新计米
qDebug()<<"CNTResetNow"<< endl;
emit CNTResetNow();
}
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];
emit mbLineAndCharSpace(lineSpace, charSpace);
}
return rc;
}
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;
}