紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3483|回复: 1

Modbus通讯库函数的测试程序

[复制链接]
下下次 发表于 2010-7-29 22:55:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 下下次 于 2010-7-29 23:01 编辑

  1. Modbus通讯库函数的测试程序 // #############################################################################
  2. // *****************************************************************************
  3. //             Copyright (c) 2003-2004, Bocon Automation Corp.
  4. //     THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY
  5. //           INFORMATION WHICH IS THE PROPERTY OF E-GUAGE AUTOMATION CORP.
  6. //
  7. //   ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM
  8. //           E-GUAGE AUTOMATION CORP., IS STRICTLY PROHIBITED.
  9. // *****************************************************************************
  10. // #############################################################################
  11. //************************* DEBUGGING MACROS ***********************************
  12. #define BLUE_TEXT   "\x1b[34m" // foreground colors for printf 字串4
  13. #define RED_TEXT     "\x1b[31m" // foreground colors for printf
  14. #define BLACK_TEXT   "\x1b[30m" // foreground colors for printf
  15. #define BINBUFSIZE               255
  16. #define BOUTBUFSIZE             255
  17. #use "Modbus.lib"
  18. #use "TCP-IP.lib"
  19. #define MODBUS_RTU_MASTER         0x10
  20. #define MODBUS_RTU_SLAVE           0x20
  21. #define MODBUS_ASCII_MASTER         0x30
  22. #define MODBUS_ASCII_SLAVE         0x40
  23. #define MODBUS_TCP_MASTER         0x50
  24. #define MODBUS_TCP_SLAVE           0x60
  25. #define TRANSACTION_TIMEOUT         2000L
  26. #define MODBUS_MASTER_POLLING_PERIOD   2000L
  27. // ***************************************************************************** 字串3
  28. // Design Notes: 非阻塞方式读取键盘输入
  29. // -----------------------------------------------------------------------------
  30. char PeekChar()
  31. {
  32.   auto char Key;
  33.   if (!kbhit())
  34.   {
  35.     return 0;
  36.   }
  37.   dkGetCharFromStdio();
  38.   Key = dkCharData;
  39.   asm push      ip
  40.   asm ipset 1
  41.   dkCharReady--;
  42.   asm pop      ip
  43.   return Key;
  44. }
  45. // *****************************************************************************
  46. // Design Notes: 打印测试信息
  47. // -----------------------------------------------------------------------------
  48. void PrintTestInfo()
  49. {
  50.   printf( RED_TEXT );
  51.   printf( "+++Test the Modbus RTU/ASCII/TCP protocol:\n" );
  52.   printf( "\t 1: Modbus-RTU Maser\n" );
  53.   printf( "\t 2: Modbus-RTU Slave\n" );
  54.   printf( "\t 3: Modbus-ASCII Master\n" );
  55. 字串2


  56.   printf( "\t 4: Modbus-ASCII Slave\n" );
  57.   printf( "\t 5: Modbus-TCP Master\n" );
  58.   printf( "\t 6: Modbus-TCP Slave\n" );
  59.   printf( BLUE_TEXT );
  60.   printf( "Press 'Q' to exit the program\n" );
  61. }
  62. // *****************************************************************************
  63. // Design Notes: Print out the buffer contents
  64. // -----------------------------------------------------------------------------
  65. void DumpAsciiBuffer( char * pBuffer, int iStrlen )
  66. {
  67.   int i;
  68.   printf( "\n==>The buffer length is %d\n", iStrlen );
  69.   for ( i = 0; i < iStrlen; i++ )
  70.   {
  71.     printf( "%c ", pBuffer );
  72.   }
  73.   printf( "\n" );
  74. }
  75. // *****************************************************************************
  76. // Design Notes: Print out the buffer contents 字串6
  77. // -----------------------------------------------------------------------------
  78. void DumpHexBuffer( char * pBuffer, int iStrlen )
  79. {
  80.   int i;
  81.   printf( "\n==>The buffer length is %d\n", iStrlen );
  82.   for ( i = 0; i < iStrlen; i++ )
  83.   {
  84.     printf( "0x%02X ", pBuffer );
  85.   }
  86.   printf( "\n" );
  87. }
  88. // *****************************************************************************
  89. // Design Notes:
  90. // -----------------------------------------------------------------------------
  91. void main()
  92. {
  93. char key;
  94.   char mode;
  95.   int iRetVal;
  96.   unsigned long dwStartTime;
  97.   char srcMsg[256];
  98.   char dstMsg[256];
  99.   unsigned short srcMsgLen;
  100.   unsigned short dstMsgLen;
  101.   struct MBSerial   mbSer;
  102.   struct MBSerial * pMBSer;
  103.   struct MBTCP     mbTCP;
  104. 字串4


  105.   struct MBTCP   * pMBTCP;
  106.   // The server socket for passive calling
  107.   tcp_Socket serverSock;
  108.   struct TCPContext   server;
  109.   struct TCPContext * pServer;
  110.   // The client socket for active calling
  111.   tcp_Socket clientSock;
  112.   struct TCPContext   client;
  113.   struct TCPContext * pClient;
  114.   // Initialize memory for the server-node context
  115.   pServer = &server;
  116.   memset( pServer, 0x00, sizeof( struct TCPContext ) );
  117.   pServer->m_pSocket = &serverSock;
  118.   // Initialize memory for the client-node context
  119.   pClient = &client;
  120.   memset( pClient, 0x00, sizeof( struct TCPContext ) );
  121.   pClient->m_pSocket = &clientSock;
  122.   // Initialize the TCP/IP stack
  123.   sock_init();
  124.   // Initialize the network interface
  125.   ifconfig(
  126.     IF_ETH0,
  127.     IFS_DOWN, 字串2
  128.     IFS_IPADDR, aton( "172.21.66.25" ),
  129.     IFS_NETMASK, 0xFFFFFF00uL,
  130.     IFS_ROUTER_SET, aton( "172.21.66.1" ),
  131.     IFS_NAMESERVER_SET, aton( "172.21.66.100" ),
  132.     IFS_NAMESERVER_ADD, aton( "172.21.66.144" ),
  133.     IFS_UP,
  134.     IFS_END );
  135.   // Make sure the interface exits pending status
  136.   if ( ifpending( IF_ETH0 ) % 2 )
  137.   {
  138.     printf( "The cable is plugged out!\n" );
  139.   }
  140.   // Make sure the interface is up
  141.   if ( !ifstatus( IF_ETH0 ) )
  142.   {
  143.     // Display some hardware-error information and reset the system
  144.     forceSoftReset();
  145.   }
  146.   // Initialize Modbus Master node
  147.   pClient->m_bIsTCPServer = 0;
  148.   pClient->m_dwIPAddr = aton( "172.21.66.154" );
  149.   pClient->m_usPort = 502; 字串1
  150.   pClient->m_usMaxSockRetries = 5;
  151.   pClient->m_usMaxSessionRetries = 3;
  152.   // Reset the Modbus Master socket
  153.   ResetTCPSocket( pClient );
  154.   // Initialize the Modbus Slave node
  155.   pServer->m_bIsTCPServer = 1;
  156.   pServer->m_dwIPAddr = gethostid();
  157.   pServer->m_usPort = 502;
  158.   pServer->m_usMaxSockRetries = 5;
  159.   pServer->m_usMaxSessionRetries = 3;
  160.   // Reset the Modbus Slave socket
  161.   ResetTCPSocket( pServer );
  162.   serBopen( 2400 );
  163.   serBparity( PARAM_NOPARITY );
  164.   serBdatabits( PARAM_8BIT );
  165.   // Print out the manipulation information
  166.   PrintTestInfo();
  167.   pMBSer = &mbSer;
  168.   pMBTCP = &mbTCP;
  169.   memset( pMBSer, 0x00, sizeof( struct MBSerial ) );
  170.   memset( pMBTCP, 0x00, sizeof( struct MBTCP ) );
  171.   mode = 0xFF;      // Invalid mode
  172. 字串4


  173.   while ( 1 )
  174.   {
  175.     // Thread 1st: The user input dispatch
  176.     costate
  177.     {
  178.       key = PeekChar();
  179.       switch ( key )
  180.       {
  181.       case '1':
  182.         {
  183.           mode = MODBUS_RTU_MASTER;
  184.           printf( RED_TEXT );
  185.           printf( "System now runs in Modbus RTU Maser mode\n" );
  186.           printf( BLUE_TEXT );
  187.         }
  188.         break;
  189.       case '2':
  190.         {
  191.           mode = MODBUS_RTU_SLAVE;
  192.           printf( RED_TEXT );
  193.           printf( "System now runs in Modbus RTU Slave mode\n" );
  194.           printf( BLUE_TEXT ); 字串6
  195.         }
  196.         break;
  197.       case '3':
  198.         {
  199.           mode = MODBUS_ASCII_MASTER;
  200.           printf( RED_TEXT );
  201.           printf( "System now runs in Modbus ASCII Master mode\n" );
  202.           printf( BLUE_TEXT );
  203.         }
  204.         break;
  205.       case '4':
  206.         {
  207.           mode = MODBUS_ASCII_SLAVE;
  208.           printf( RED_TEXT );
  209.           printf( "System now runs in Modbus ASCII Slave mode\n" );
  210.           printf( BLUE_TEXT );
  211.         }
  212.         break;
  213.       case '5':
  214.         { 字串9
  215.           mode = MODBUS_TCP_MASTER;
  216.           printf( RED_TEXT );
  217.           printf( "System now runs in Modbus TCP Master mode\n" );
  218.           printf( BLUE_TEXT );
  219.           ResetTCPSocket( pServer );
  220.         }
  221.         break;
  222.       case '6':
  223.         {
  224.           mode = MODBUS_TCP_SLAVE;
  225.           printf( RED_TEXT );
  226.           printf( "System now runs in Modbus TCP Slve mode\n" );
  227.           printf( BLUE_TEXT );
  228.           ResetTCPSocket( pClient );
  229.         }
  230.         break;
  231.       default:
  232.         {
  233. 字串7


  234.         }
  235.         break;
  236.       }
  237.     }
复制代码
 楼主| 下下次 发表于 2010-7-29 23:02:32 | 显示全部楼层
  1.    // Thread 2nd: Modbus-RTU Master
  2.     costate
  3.     {
  4.       if ( mode != MODBUS_RTU_MASTER )
  5.       {
  6.         abort;
  7.       }
  8.       waitfor ( DelayMs( MODBUS_MASTER_POLLING_PERIOD ) );
  9.       // Instantiate a modbus request
  10.       pMBSer->m_ucNodeID = 23;
  11.       pMBSer->m_MBReqeust.m_ucCMD = 3;
  12.       pMBSer->m_MBReqeust.m_usStartAddr = 1234;
  13.       pMBSer->m_MBReqeust.m_usRegNum = 19;
  14.       // Initialize the modbus request string
  15.       memset( srcMsg, 0x00, sizeof( srcMsg ) );
  16.       ConstructModbusRTUMsg( pMBSer, srcMsg );
  17.       // Dump the Modbus RTU request message 字串1
  18.       DumpHexBuffer( srcMsg, 8 );
  19.       // Issue out the Modbus requst
  20.       serBwrite( srcMsg, 8 );
  21.       waitfor ( DelayMs( TRANSACTION_TIMEOUT ) );
  22.       // Receive the Modbus response from the Modbus Slave
  23.       memset( srcMsg, 0x00, sizeof( srcMsg ) );
  24.       srcMsgLen = 0;
  25.       dwStartTime = MS_TIMER;
  26.       while ( MS_TIMER - dwStartTime < TRANSACTION_TIMEOUT )
  27.       {
  28.         iRetVal = serBread( srcMsg + srcMsgLen, sizeof( srcMsg ), 0 );
  29.         srcMsgLen += iRetVal;
  30.       }
  31.       if ( srcMsgLen > 0 )
  32.       {
  33.         DumpHexBuffer( srcMsg, srcMsgLen );
  34.       }
  35.     }
  36.     // Thread 3rd: Modbus-RTU Slave
  37. 字串6


  38.     costate
  39.     {
  40.       if ( mode != MODBUS_RTU_SLAVE )
  41.       {
  42.         abort;
  43.       }
  44.       // Read Modbus request from the serial port
  45.       memset( srcMsg, 0x00, sizeof( srcMsg ) );
  46.       srcMsgLen = serBread( srcMsg, sizeof( srcMsg ), 100 );
  47.       if ( srcMsgLen > 0 )
  48.       {
  49.         // Dump the buffer contents of the modbus request
  50.         DumpHexBuffer( srcMsg, srcMsgLen );
  51.         // Process the Modbus request, generate the Modbus respone message
  52.         ProcessModbusRTUMsg( srcMsg, srcMsgLen, dstMsg, &dstMsgLen );
  53.         // Send Modbus response message to the Modbus master node
  54.         serBwrite( dstMsg, dstMsgLen ); 字串4
  55.       }
  56.     }
  57.     // Thread 4th: Modbus-ASCII Master
  58.     costate
  59.     {
  60.       if ( mode != MODBUS_ASCII_MASTER )
  61.       {
  62.         abort;
  63.       }
  64.       waitfor ( DelayMs( MODBUS_MASTER_POLLING_PERIOD ) );
  65.       // Instantiate a modbus request
  66.       pMBSer->m_ucNodeID = 23;
  67.       pMBSer->m_MBReqeust.m_ucCMD = 3;
  68.       pMBSer->m_MBReqeust.m_usStartAddr = 1234;
  69.       pMBSer->m_MBReqeust.m_usRegNum = 19;
  70.       // Initialize the modbus request string
  71.       memset( srcMsg, 0x00, sizeof( srcMsg ) );
  72.       ConstructModbusAsciiMsg( pMBSer, srcMsg );
  73.       // Dump the modbus requst message
  74.       DumpAsciiBuffer( srcMsg, 17 ); 字串1
  75.       // Issue out the Modbus request message
  76.       serBwrite( srcMsg, 17 );
  77.       // Receive the response from the Modbus slave
  78.       memset( srcMsg, 0x00, sizeof( srcMsg ) );
  79.       srcMsgLen = 0;
  80.       dwStartTime = MS_TIMER;
  81.       while ( MS_TIMER - dwStartTime < TRANSACTION_TIMEOUT )
  82.       {
  83.         iRetVal = serBread( srcMsg + srcMsgLen, sizeof( srcMsg ), 0 );
  84.         srcMsgLen += iRetVal;
  85.       }
  86.       if ( srcMsgLen > 0 )
  87.       {
  88.         DumpAsciiBuffer( srcMsg, srcMsgLen );
  89.       }
  90.     }
  91.     // Thread 5th: Modbus-ASCII Slave
  92.     costate
  93.     {
  94.       if ( mode != MODBUS_ASCII_SLAVE )
  95. 字串6


  96.       {
  97.         abort;
  98.       }
  99.       // Read Modbus request from the serial port
  100.       memset( srcMsg, 0x00, sizeof( srcMsg ) );
  101.       srcMsgLen = serBread( srcMsg, sizeof( srcMsg ), 100 );
  102.       if ( srcMsgLen > 0 )
  103.       {
  104.         // Dump the buffer contents of the modbus request
  105.         DumpAsciiBuffer( srcMsg, srcMsgLen );
  106.         ProcessModbusAsciiMsg( srcMsg, srcMsgLen, dstMsg, &dstMsgLen );
  107.         // Dump the contents of the modbus response
  108.         DumpAsciiBuffer( dstMsg, dstMsgLen );
  109.         // Send response message to the modbus master node
  110.         serBwrite( dstMsg, dstMsgLen );
  111.       }
  112.     }
  113.     // Thread 6th: Modbus-TCP Master
  114. 字串4


  115.     costate
  116.     {
  117.       if ( mode != MODBUS_TCP_MASTER )
  118.       {
  119.         abort;
  120.       }
  121.       waitfor ( DelayMs( 500 ) );
  122.       RefreshTCPSocketStatus( pClient );
  123.       if ( pClient->m_bIsConnected )
  124.       {
  125.         pMBTCP->m_MBHeader.m_usProtoID = 0;
  126.         pMBTCP->m_MBHeader.m_usMsgLen = 6;
  127.         pMBTCP->m_MBHeader.m_ucUnitID = 29;
  128.         pMBTCP->m_MBReqeust.m_ucCMD = 3;
  129.         pMBTCP->m_MBReqeust.m_usStartAddr = 1234;
  130.         pMBTCP->m_MBReqeust.m_usRegNum = 50;
  131.         memset( srcMsg, 0x00, sizeof( srcMsg ) );
  132.         ConstructModbusTCPMsg( pMBTCP, srcMsg );
  133.         sock_fastwrite( pClient->m_pSocket, srcMsg, 12 ); 字串1
  134.         memset( srcMsg, 0x00, sizeof( srcMsg ) );
  135.         srcMsgLen = 0;
  136.         dwStartTime = MS_TIMER;
  137.         while ( MS_TIMER - dwStartTime < TRANSACTION_TIMEOUT )
  138.         {
  139.           tcp_tick( pClient->m_pSocket );
  140.           iRetVal = sock_fastread( pClient->m_pSocket, srcMsg + srcMsgLen, sizeof( srcMsg ) );
  141.           srcMsgLen += iRetVal;
  142.         }
  143.         if ( srcMsgLen != 0 )
  144.         {
  145.           DumpHexBuffer( srcMsg, srcMsgLen );
  146.         }
  147.       }
  148.     }
  149.     // Thread 7th: Modbus-TCP Slave
  150.     costate
  151.     {
  152.       if ( mode != MODBUS_TCP_SLAVE )
  153. 字串9


  154.       {
  155.         abort;
  156.       }
  157.       waitfor ( DelayMs( 500 ) );
  158.       RefreshTCPSocketStatus( pServer );
  159.       if ( pServer->m_bIsConnected )
  160.       {
  161.         memset( srcMsg, 0x00, sizeof( srcMsg ) );
  162.         srcMsgLen = sock_fastread( pServer->m_pSocket, srcMsg, sizeof( srcMsg ) );
  163.         if ( srcMsgLen != 0 )
  164.         {
  165.           DumpHexBuffer( srcMsg, srcMsgLen );
  166.           memset( dstMsg, 0x00, sizeof( dstMsg ) );
  167.           ProcessModbusTCPMsg( srcMsg, srcMsgLen, dstMsg, &dstMsgLen );
  168.           if ( dstMsgLen != 0 )
  169.           {
  170.             sock_fastwrite( pServer->m_pSocket, dstMsg, dstMsgLen );
  171. 字串6


  172.             DumpHexBuffer( dstMsg, dstMsgLen );
  173.           }
  174.         }
  175.       }
  176.     }
  177.   }
  178. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-5-5 13:45 , Processed in 0.281250 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表