mmodbus.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef _M_MODBUS_H_
  2. #define _M_MODBUS_H_
  3. /*
  4. Author: Nima Askari
  5. WebSite: http://www.github.com/NimaLTD
  6. Instagram: http://instagram.com/github.NimaLTD
  7. Youtube: https://www.youtube.com/channel/UCUhY7qY1klJm1d2kulr9ckw
  8. Version: 1.3.2
  9. Reversion History:
  10. (1.3.2)
  11. Speedup receiver, add write multiple holding
  12. (1.3.1)
  13. Remove delay in sending function when DMA is disabled
  14. (1.3.0)
  15. Add 16 bit order
  16. (1.2.1)
  17. bug fixed.
  18. (1.2.0)
  19. add read 32bit register order.
  20. (1.1.2)
  21. fix read holding register.
  22. (1.1.1)
  23. fix functions name.
  24. (1.1.0)
  25. add 16bit,32bit,float read.
  26. (1.0.1)
  27. add delay in receive routin.
  28. (1.0.0)
  29. First release.
  30. */
  31. #include "gd32f30x.h"
  32. #include "mmodbusConfig.h"
  33. //#include "usart.h"
  34. #include <stdbool.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <stdint.h>
  38. #include "FreeRTOS.h"
  39. #include "task.h"
  40. #define mmodbus_delay(x) delay_ms(x)
  41. /*
  42. 功能码
  43. MModbusCMD_ReadCoilStatus(读线圈状态):用于读取一个或多个逻辑线圈的状态。
  44. MModbusCMD_ReadDiscreteInputs(读离散输入状态):用于读取一个或多个数字输入信号的状态。
  45. MModbusCMD_ReadHoldingRegisters(读保持寄存器):用于读取一个或多个保持寄存器的值。
  46. MModbusCMD_ReadInputRegisters(读输入寄存器):用于读取一个或多个输入寄存器的值。
  47. MModbusCMD_WriteSingleCoil(写单个线圈):用于控制单个逻辑线圈的状态。
  48. MModbusCMD_WriteSingleRegister(写单个保持寄存器):用于写入单个保持寄存器的值。
  49. MModbusCMD_WriteMultipleCoils(写多个线圈):用于控制多个逻辑线圈的状态。
  50. MModbusCMD_WriteMultipleRegisters(写多个保持寄存器):用于写入多个保持寄存器的值。
  51. */
  52. typedef enum
  53. {
  54. MModbusCMD_ReadCoilStatus = 1,
  55. MModbusCMD_ReadDiscreteInputs = 2,
  56. MModbusCMD_ReadHoldingRegisters = 3,
  57. MModbusCMD_ReadInputRegisters = 4,
  58. MModbusCMD_WriteSingleCoil = 5,
  59. MModbusCMD_WriteSingleRegister = 6,
  60. MModbusCMD_WriteMultipleCoils = 15,
  61. MModbusCMD_WriteMultipleRegisters = 16
  62. }MModbusCMD_t;
  63. //大小端
  64. typedef enum
  65. {
  66. MModBus_16bitOrder_AB=1,
  67. MModBus_16bitOrder_BA=2,
  68. }MModBus_16bitOrder_t;
  69. typedef enum
  70. {
  71. MModBus_32bitOrder_ABCD = 0,
  72. MModBus_32bitOrder_DCBA,
  73. MModBus_32bitOrder_BADC,
  74. MModBus_32bitOrder_CDAB,
  75. }MModBus_32bitOrder_t;
  76. /*
  77. rxIndex:接收缓冲区的当前接收位置。
  78. rxBuf:接收缓冲区。
  79. rxTime:最近一次接收数据的时间戳。
  80. txBusy:发送是否忙碌的标志位。
  81. timeout:超时时间。
  82. byteOrder16:16 位数据的字节序(即高位字节在前还是低位字节在前)。
  83. byteOrder32:32 位数据的字节序。
  84. done:数据是否接收完成的标志位。
  85. txDmaDone:DMA 发送是否完成的标志位(该成员只在使用 DMA 发送时有效)。
  86. */
  87. typedef struct
  88. {
  89. uint16_t rxIndex;
  90. uint8_t rxBuf[_MMODBUS_RXSIZE];
  91. uint32_t rxTime;
  92. uint8_t txBusy;
  93. uint32_t timeout;
  94. MModBus_16bitOrder_t byteOrder16;
  95. MModBus_32bitOrder_t byteOrder32;
  96. uint8_t done;
  97. #if (_MMODBUS_TXDMA == 1)
  98. uint8_t txDmaDone;
  99. #endif
  100. }MModBus_t;
  101. //##################################################################################################
  102. void mmodbus_callback(void);
  103. bool mmodbus_sendRaw(uint8_t *data, uint16_t size, uint32_t timeout);
  104. uint16_t mmodbus_crc16(const uint8_t *nData, uint16_t wLength);
  105. void mmodbus_callback_txDMA(void);
  106. bool mmodbus_init(uint32_t setTimeout);
  107. void mmodbus_set16bitOrder(MModBus_16bitOrder_t MModBus_16bitOrder_);
  108. void mmodbus_set32bitOrder(MModBus_32bitOrder_t MModBus_32bitOrder_);
  109. // coils numbers 00001 to 09999
  110. bool mmodbus_readCoil(uint8_t slaveAddress, uint16_t number, uint8_t *data);
  111. bool mmodbus_readCoils(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data);
  112. // discrete input 10001 to 19999
  113. bool mmodbus_readDiscreteInput(uint8_t slaveAddress, uint16_t number, uint8_t *data);
  114. bool mmodbus_readDiscreteInputs(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data);
  115. // input register 30001 to 39999
  116. bool mmodbus_readInputRegister32f(uint8_t slaveAddress, uint16_t number, float *data);
  117. bool mmodbus_readInputRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data);
  118. bool mmodbus_readInputRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data);
  119. bool mmodbus_readInputRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data);
  120. bool mmodbus_readInputRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data);
  121. bool mmodbus_readInputRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data);
  122. // holding register 40001 to 49999
  123. bool mmodbus_readHoldingRegister32f(uint8_t slaveAddress, uint16_t number, float *data);
  124. bool mmodbus_readHoldingRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data);
  125. bool mmodbus_readHoldingRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data);
  126. bool mmodbus_readHoldingRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data);
  127. bool mmodbus_readHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data);
  128. bool mmodbus_readHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data);
  129. // coils numbers 00001 to 09999
  130. bool mmodbus_writeCoil(uint8_t slaveAddress, uint16_t number, uint8_t data);
  131. // holding register 40001 to 49999
  132. bool mmodbus_writeHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t data);
  133. bool mmodbus_writeHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data);
  134. uint16_t mmodbus_crc16(const uint8_t *nData, uint16_t wLength);
  135. //##################################################################################################
  136. #endif