mmodbus.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "gd32f10x_usart.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. #if _MMODBUS_FREERTOS == 1
  39. #include "cmsis_os.h"
  40. #define mmodbus_delay(x) osDelay(x)
  41. #else
  42. #define mmodbus_delay(x) delay_1ms(x)
  43. #endif
  44. /*
  45. 功能码
  46. MModbusCMD_ReadCoilStatus(读线圈状态):用于读取一个或多个逻辑线圈的状态。
  47. MModbusCMD_ReadDiscreteInputs(读离散输入状态):用于读取一个或多个数字输入信号的状态。
  48. MModbusCMD_ReadHoldingRegisters(读保持寄存器):用于读取一个或多个保持寄存器的值。
  49. MModbusCMD_ReadInputRegisters(读输入寄存器):用于读取一个或多个输入寄存器的值。
  50. MModbusCMD_WriteSingleCoil(写单个线圈):用于控制单个逻辑线圈的状态。
  51. MModbusCMD_WriteSingleRegister(写单个保持寄存器):用于写入单个保持寄存器的值。
  52. MModbusCMD_WriteMultipleCoils(写多个线圈):用于控制多个逻辑线圈的状态。
  53. MModbusCMD_WriteMultipleRegisters(写多个保持寄存器):用于写入多个保持寄存器的值。
  54. */
  55. typedef enum
  56. {
  57. MModbusCMD_ReadCoilStatus = 1,
  58. MModbusCMD_ReadDiscreteInputs = 2,
  59. MModbusCMD_ReadHoldingRegisters = 3,
  60. MModbusCMD_ReadInputRegisters = 4,
  61. MModbusCMD_WriteSingleCoil = 5,
  62. MModbusCMD_WriteSingleRegister = 6,
  63. MModbusCMD_WriteMultipleCoils = 15,
  64. MModbusCMD_WriteMultipleRegisters = 16
  65. }MModbusCMD_t;
  66. //大小端
  67. typedef enum
  68. {
  69. MModBus_16bitOrder_AB=1,
  70. MModBus_16bitOrder_BA,
  71. }MModBus_16bitOrder_t;
  72. typedef enum
  73. {
  74. MModBus_32bitOrder_ABCD = 0,
  75. MModBus_32bitOrder_DCBA,
  76. MModBus_32bitOrder_BADC,
  77. MModBus_32bitOrder_CDAB,
  78. }MModBus_32bitOrder_t;
  79. /*
  80. rxIndex:接收缓冲区的当前接收位置。
  81. rxBuf:接收缓冲区。
  82. rxTime:最近一次接收数据的时间戳。
  83. txBusy:发送是否忙碌的标志位。
  84. timeout:超时时间。
  85. byteOrder16:16 位数据的字节序(即高位字节在前还是低位字节在前)。
  86. byteOrder32:32 位数据的字节序。
  87. done:数据是否接收完成的标志位。
  88. txDmaDone:DMA 发送是否完成的标志位(该成员只在使用 DMA 发送时有效)。
  89. */
  90. typedef struct
  91. {
  92. uint16_t rxIndex;
  93. uint8_t rxBuf[_MMODBUS_RXSIZE];
  94. uint32_t rxTime;
  95. uint8_t txBusy;
  96. uint32_t timeout;
  97. MModBus_16bitOrder_t byteOrder16;
  98. MModBus_32bitOrder_t byteOrder32;
  99. uint8_t done;
  100. #if (_MMODBUS_TXDMA == 1)
  101. uint8_t txDmaDone;
  102. #endif
  103. }MModBus_t;
  104. //##################################################################################################
  105. void mmodbus_callback(void);
  106. void mmodbus_callback_txDMA(void);
  107. bool mmodbus_init(uint32_t setTimeout);
  108. void mmodbus_set16bitOrder(MModBus_16bitOrder_t MModBus_16bitOrder_);
  109. void mmodbus_set32bitOrder(MModBus_32bitOrder_t MModBus_32bitOrder_);
  110. // coils numbers 00001 to 09999
  111. bool mmodbus_readCoil(uint8_t slaveAddress, uint16_t number, uint8_t *data);
  112. bool mmodbus_readCoils(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data);
  113. // discrete input 10001 to 19999
  114. bool mmodbus_readDiscreteInput(uint8_t slaveAddress, uint16_t number, uint8_t *data);
  115. bool mmodbus_readDiscreteInputs(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data);
  116. // input register 30001 to 39999
  117. bool mmodbus_readInputRegister32f(uint8_t slaveAddress, uint16_t number, float *data);
  118. bool mmodbus_readInputRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data);
  119. bool mmodbus_readInputRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data);
  120. bool mmodbus_readInputRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data);
  121. bool mmodbus_readInputRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data);
  122. bool mmodbus_readInputRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data);
  123. // holding register 40001 to 49999
  124. bool mmodbus_readHoldingRegister32f(uint8_t slaveAddress, uint16_t number, float *data);
  125. bool mmodbus_readHoldingRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data);
  126. bool mmodbus_readHoldingRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data);
  127. bool mmodbus_readHoldingRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data);
  128. bool mmodbus_readHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data);
  129. bool mmodbus_readHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data);
  130. // coils numbers 00001 to 09999
  131. bool mmodbus_writeCoil(uint8_t slaveAddress, uint16_t number, uint8_t data);
  132. // holding register 40001 to 49999
  133. bool mmodbus_writeHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t data);
  134. bool mmodbus_writeHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data);
  135. uint16_t mmodbus_crc16(const uint8_t *nData, uint16_t wLength);
  136. //##################################################################################################
  137. #endif