- // Thread 2nd: Modbus-RTU Master
- costate
- {
- if ( mode != MODBUS_RTU_MASTER )
- {
- abort;
- }
- waitfor ( DelayMs( MODBUS_MASTER_POLLING_PERIOD ) );
- // Instantiate a modbus request
- pMBSer->m_ucNodeID = 23;
- pMBSer->m_MBReqeust.m_ucCMD = 3;
- pMBSer->m_MBReqeust.m_usStartAddr = 1234;
- pMBSer->m_MBReqeust.m_usRegNum = 19;
- // Initialize the modbus request string
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- ConstructModbusRTUMsg( pMBSer, srcMsg );
- // Dump the Modbus RTU request message 字串1
- DumpHexBuffer( srcMsg, 8 );
- // Issue out the Modbus requst
- serBwrite( srcMsg, 8 );
- waitfor ( DelayMs( TRANSACTION_TIMEOUT ) );
- // Receive the Modbus response from the Modbus Slave
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- srcMsgLen = 0;
- dwStartTime = MS_TIMER;
- while ( MS_TIMER - dwStartTime < TRANSACTION_TIMEOUT )
- {
- iRetVal = serBread( srcMsg + srcMsgLen, sizeof( srcMsg ), 0 );
- srcMsgLen += iRetVal;
- }
- if ( srcMsgLen > 0 )
- {
- DumpHexBuffer( srcMsg, srcMsgLen );
- }
- }
- // Thread 3rd: Modbus-RTU Slave
- 字串6
- costate
- {
- if ( mode != MODBUS_RTU_SLAVE )
- {
- abort;
- }
- // Read Modbus request from the serial port
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- srcMsgLen = serBread( srcMsg, sizeof( srcMsg ), 100 );
- if ( srcMsgLen > 0 )
- {
- // Dump the buffer contents of the modbus request
- DumpHexBuffer( srcMsg, srcMsgLen );
- // Process the Modbus request, generate the Modbus respone message
- ProcessModbusRTUMsg( srcMsg, srcMsgLen, dstMsg, &dstMsgLen );
- // Send Modbus response message to the Modbus master node
- serBwrite( dstMsg, dstMsgLen ); 字串4
- }
- }
- // Thread 4th: Modbus-ASCII Master
- costate
- {
- if ( mode != MODBUS_ASCII_MASTER )
- {
- abort;
- }
- waitfor ( DelayMs( MODBUS_MASTER_POLLING_PERIOD ) );
- // Instantiate a modbus request
- pMBSer->m_ucNodeID = 23;
- pMBSer->m_MBReqeust.m_ucCMD = 3;
- pMBSer->m_MBReqeust.m_usStartAddr = 1234;
- pMBSer->m_MBReqeust.m_usRegNum = 19;
- // Initialize the modbus request string
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- ConstructModbusAsciiMsg( pMBSer, srcMsg );
- // Dump the modbus requst message
- DumpAsciiBuffer( srcMsg, 17 ); 字串1
- // Issue out the Modbus request message
- serBwrite( srcMsg, 17 );
- // Receive the response from the Modbus slave
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- srcMsgLen = 0;
- dwStartTime = MS_TIMER;
- while ( MS_TIMER - dwStartTime < TRANSACTION_TIMEOUT )
- {
- iRetVal = serBread( srcMsg + srcMsgLen, sizeof( srcMsg ), 0 );
- srcMsgLen += iRetVal;
- }
- if ( srcMsgLen > 0 )
- {
- DumpAsciiBuffer( srcMsg, srcMsgLen );
- }
- }
- // Thread 5th: Modbus-ASCII Slave
- costate
- {
- if ( mode != MODBUS_ASCII_SLAVE )
- 字串6
- {
- abort;
- }
- // Read Modbus request from the serial port
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- srcMsgLen = serBread( srcMsg, sizeof( srcMsg ), 100 );
- if ( srcMsgLen > 0 )
- {
- // Dump the buffer contents of the modbus request
- DumpAsciiBuffer( srcMsg, srcMsgLen );
- ProcessModbusAsciiMsg( srcMsg, srcMsgLen, dstMsg, &dstMsgLen );
- // Dump the contents of the modbus response
- DumpAsciiBuffer( dstMsg, dstMsgLen );
- // Send response message to the modbus master node
- serBwrite( dstMsg, dstMsgLen );
- }
- }
- // Thread 6th: Modbus-TCP Master
- 字串4
- costate
- {
- if ( mode != MODBUS_TCP_MASTER )
- {
- abort;
- }
- waitfor ( DelayMs( 500 ) );
- RefreshTCPSocketStatus( pClient );
- if ( pClient->m_bIsConnected )
- {
- pMBTCP->m_MBHeader.m_usProtoID = 0;
- pMBTCP->m_MBHeader.m_usMsgLen = 6;
- pMBTCP->m_MBHeader.m_ucUnitID = 29;
- pMBTCP->m_MBReqeust.m_ucCMD = 3;
- pMBTCP->m_MBReqeust.m_usStartAddr = 1234;
- pMBTCP->m_MBReqeust.m_usRegNum = 50;
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- ConstructModbusTCPMsg( pMBTCP, srcMsg );
- sock_fastwrite( pClient->m_pSocket, srcMsg, 12 ); 字串1
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- srcMsgLen = 0;
- dwStartTime = MS_TIMER;
- while ( MS_TIMER - dwStartTime < TRANSACTION_TIMEOUT )
- {
- tcp_tick( pClient->m_pSocket );
- iRetVal = sock_fastread( pClient->m_pSocket, srcMsg + srcMsgLen, sizeof( srcMsg ) );
- srcMsgLen += iRetVal;
- }
- if ( srcMsgLen != 0 )
- {
- DumpHexBuffer( srcMsg, srcMsgLen );
- }
- }
- }
- // Thread 7th: Modbus-TCP Slave
- costate
- {
- if ( mode != MODBUS_TCP_SLAVE )
- 字串9
- {
- abort;
- }
- waitfor ( DelayMs( 500 ) );
- RefreshTCPSocketStatus( pServer );
- if ( pServer->m_bIsConnected )
- {
- memset( srcMsg, 0x00, sizeof( srcMsg ) );
- srcMsgLen = sock_fastread( pServer->m_pSocket, srcMsg, sizeof( srcMsg ) );
- if ( srcMsgLen != 0 )
- {
- DumpHexBuffer( srcMsg, srcMsgLen );
- memset( dstMsg, 0x00, sizeof( dstMsg ) );
- ProcessModbusTCPMsg( srcMsg, srcMsgLen, dstMsg, &dstMsgLen );
- if ( dstMsgLen != 0 )
- {
- sock_fastwrite( pServer->m_pSocket, dstMsg, dstMsgLen );
- 字串6
- DumpHexBuffer( dstMsg, dstMsgLen );
- }
- }
- }
- }
- }
- }
复制代码 |