demo.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * File : demo.c
  3. * This file is part of freemodbus in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-04-04 yi.qiu first version
  13. * 2010-04-07 LiJin
  14. */
  15. /* ----------------------- Platform includes --------------------------------*/
  16. #include <rtthread.h>
  17. #include "port.h"
  18. /* ----------------------- Modbus includes ----------------------------------*/
  19. #include "mb.h"
  20. #include "mbconfig.h"
  21. #include "mbframe.h"
  22. #include "mbproto.h"
  23. #include "mbfunc.h"
  24. USHORT buf[256];
  25. void rt_modbus_thread_entry(void* parameter)
  26. {
  27. eMBErrorCode eStatus;
  28. eStatus = eMBInit( MB_RTU, 0x0A, 0, 115200, MB_PAR_EVEN );
  29. /* Enable the Modbus Protocol Stack. */
  30. eStatus = eMBEnable( );
  31. rt_thread_delay(50);
  32. while(1)
  33. {
  34. /* request holding reg */
  35. eMBMReadHoldingRegisters(0x0A, 0x1, 0x10, buf);
  36. rt_kprintf("stop\n");
  37. rt_thread_delay(100);
  38. /* request coils */
  39. eMBMReadCoils(0x0A, 0x1, 128, buf);
  40. rt_thread_delay(100);
  41. //while(1);
  42. }
  43. }
  44. int modbus_demo_init(void)
  45. {
  46. rt_thread_t modbus_thread;
  47. modbus_thread = rt_thread_create("modbus",
  48. rt_modbus_thread_entry, RT_NULL,
  49. 2048, 20, 20);
  50. if (modbus_thread != RT_NULL)
  51. rt_thread_startup(modbus_thread);
  52. return 0;
  53. }
  54. eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  55. {
  56. return MB_ENOREG;
  57. }
  58. eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
  59. eMBRegisterMode eMode )
  60. {
  61. return MB_ENOREG;
  62. }
  63. eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
  64. eMBRegisterMode eMode )
  65. {
  66. return MB_ENOREG;
  67. }
  68. eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  69. {
  70. return MB_ENOREG;
  71. }