modbus_app.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "board.h"
  2. #include "poweroff_save_app.h"
  3. #include "modbus_encoder.h"
  4. #include "modbus_slave.h"
  5. #include "modbus_master.h"
  6. #include "variable.h"
  7. #include "SEGGER_RTT.h"
  8. #include "encrypt_md5.h"
  9. #include "encrypt_xxtea.h"
  10. #include "modbus_app.h"
  11. #include <string.h>
  12. #define MODBUS_APP_NUMBER 3
  13. typedef struct
  14. {
  15. unsigned char uart_no; //串口号
  16. unsigned char modbus_dir; //modbus方向,0是从机,1是主机
  17. //当做为从机时,master_or_slave保存modbus_slave_t 对象指针
  18. //当做为主机时,master_or_slave保存modbus_master_t 对象指针
  19. void *master_or_slave; //主站信息
  20. unsigned char count;
  21. unsigned char step; //状态步
  22. unsigned char rx_buffer[1024];
  23. unsigned char tx_buffer[512];
  24. } modbus_status_t;
  25. //modbus主站对象信息
  26. modbus_master_t master1;
  27. static modbus_slave_t slaves[2];
  28. void servo_on_respone(modbus_master_cmd_t *cmd, unsigned char *respone, int length);
  29. //modbus协议的一些状态信息
  30. static modbus_status_t modbus_status[MODBUS_APP_NUMBER] = {
  31. { 0, 0, &slaves[0] ,2}, //组0,ID2,串口2,从站
  32. //{ 2, 0, &slaves[0] ,2}, //组0,ID2,串口2,从站
  33. { 1, 0, &slaves[0] ,2}, //组1,ID1,串口3主站
  34. { 3, 1, &master1 ,1}
  35. };
  36. /**
  37. * 系统默认的回调函数
  38. *
  39. * @author lxz (2019/6/12/周三)
  40. *
  41. * @param address
  42. * @param number
  43. */
  44. static void slave1_on_comm(unsigned char id, unsigned char cmd, unsigned short address, unsigned short number)
  45. {
  46. //通知软件需要退出进入bootloader
  47. if (cmd == 0x23)
  48. {
  49. extern void app_save_parameter(void);
  50. //先保存参数
  51. app_save_parameter();
  52. //设置标志位
  53. //Stm32_Clock_DeInit();
  54. //设置标志
  55. hw_noinit_write(0, 0xA5A5A5A5);
  56. //设置ID
  57. hw_noinit_write(3, id);
  58. hw_noinit_write(2, 115200);
  59. hw_noinit_write(4, 115200);
  60. hw_noinit_write(6, 115200);
  61. hw_board_reboot();
  62. }
  63. }
  64. /**
  65. * modbus应用环境的初始化,包括了一些数据域的初始化注册
  66. *
  67. * @author lxz (2019/5/17/周五)
  68. */
  69. void mosbus_app_init(void)
  70. {
  71. modbus_slave_t *slave = &slaves[0];
  72. //保存ID码,保证bootloader会同步回来
  73. hw_noinit_write(1, 1);
  74. hw_noinit_write(3, 1);
  75. hw_noinit_write(5, 1);
  76. //从站初始化
  77. memset(&slaves[0], 0, sizeof(slaves));
  78. slave->id = 1; //设置从站ID
  79. slave->on_comm = slave1_on_comm; //设置从站通讯的回调函数
  80. //添加从站有效的数据域信息
  81. modbus_slave_add_dataarea(slave,
  82. 0x01, //数据类型为位
  83. 0, //映射开始地址为0
  84. MIDDLE_COILS_NUMBER, //数据单位个数
  85. (unsigned char *)&middle_coils[0] //数据所在地址
  86. );
  87. modbus_slave_add_dataarea(slave,
  88. 0x01, //数据类型为位
  89. 0x0800, //映射开始地址为0x800,用于兼容台达DVP协议的M码地址
  90. MIDDLE_COILS_NUMBER, //数据单位个数
  91. (unsigned char *)&middle_coils[0] //数据所在地址
  92. );
  93. modbus_slave_add_dataarea(slave,
  94. 0x03, //数据类型为16位数值
  95. 0, //映射开始地址为0
  96. USER_DATA_NUMBER, //数据单位个数
  97. (unsigned char *)&user_datas[0] //数据所在地址
  98. );
  99. modbus_slave_add_dataarea(slave,
  100. 0x03, //数据类型为16位数值
  101. 0x1000, //映射开始地址为0x1000,用于兼容台达DVP协议的D码地址
  102. USER_DATA_NUMBER, //数据单位个数
  103. (unsigned char *)&user_datas[0] //数据所在地址
  104. );
  105. //升级系统必须的命令
  106. modbus_slave_add_dataarea(slave,
  107. 0xFE, //数据类型为系统定制,升级时使用
  108. 0, //映射开始地址为0
  109. 512, //数据单位个数
  110. 0 //数据所在地址
  111. );
  112. //WIFI一些对应的通讯缓冲
  113. slave = &slaves[1];
  114. slave->id = 0x7F;
  115. modbus_slave_add_dataarea(slave,
  116. 0x01, //数据类型为系统定制,升级时使用
  117. 0x00, //映射开始地址为0
  118. 24, //数据单位个数
  119. (unsigned char *)&middle_coils[1000 >> 3] //数据所在地址
  120. );
  121. modbus_slave_add_dataarea(slave,
  122. 0x03, //数据类型为系统定制,升级时使用
  123. 0x00, //映射开始地址为0
  124. 128, //数据单位个数
  125. (unsigned char *)&user_datas[1600] //数据所在地址
  126. );
  127. #if 1
  128. //初始化主站环境
  129. modbus_master_init();
  130. //初始化主站信息
  131. memset(&master1, 0, sizeof(master1));
  132. master1.encoder_type = MODBUS_ENCODER_RTU; //RTU模式
  133. master1.interval = 10000; //10ms的命令间隔
  134. master1.one_byte_time = 1000; //一个字节的时间为1ms
  135. master1.retry = 3; //命令的重试次数为3次
  136. #endif
  137. }
  138. /**
  139. * modbus 从台应用代码
  140. *
  141. * @author lxz (2019/5/17/周五)
  142. *
  143. * @param status
  144. */
  145. static void modbus_slave_app(modbus_status_t *status)
  146. {
  147. int length;
  148. int index = 0;
  149. modbus_encoder_t *encoder;
  150. switch (status->step)
  151. {
  152. case 0:
  153. hw_dma_uart_begin_read(status->uart_no, &status->rx_buffer[0], 1024);
  154. status->step++;
  155. break;
  156. case 1:
  157. length = hw_dma_uart_read_finish(status->uart_no);
  158. if (length > 0)
  159. {
  160. status->step = 0;
  161. encoder = modbus_encoder_match(status->rx_buffer, length);
  162. if (encoder != (void *)0)
  163. {
  164. length = encoder->decode(&status->rx_buffer[0], &status->rx_buffer[0], length);
  165. if (length > 0)
  166. {
  167. while (index < status->count)
  168. {
  169. length = modbus_slave_run(
  170. &((modbus_slave_t *)status->master_or_slave)[index],
  171. &status->tx_buffer[0],
  172. &status->rx_buffer[0]);
  173. if (length > 0)
  174. {
  175. length = encoder->encode(&status->tx_buffer[0], &status->tx_buffer[0], length);
  176. if (length > 0)
  177. {
  178. hw_dma_uart_begin_write(status->uart_no, (const char *)&status->tx_buffer[0], length);
  179. status->step = 2;
  180. }
  181. break;
  182. }
  183. index++;
  184. }
  185. }
  186. }
  187. }
  188. break;
  189. case 2:
  190. if (hw_dma_uart_write_finish(status->uart_no))
  191. {
  192. status->step = 0;
  193. }
  194. break;
  195. }
  196. }
  197. /**
  198. * 主站应用代码
  199. *
  200. * @author lxz
  201. *
  202. * @param status
  203. */
  204. static void modbus_master_app(modbus_status_t *status)
  205. {
  206. int length;
  207. switch (status->step)
  208. {
  209. case 0:
  210. //判断是否有包需要处理
  211. length = hw_dma_uart_read_finish(status->uart_no);
  212. //将接收信息提供给主站,并获取主站输出
  213. length = modbus_master_run(
  214. (modbus_master_t *)status->master_or_slave,
  215. status->tx_buffer,
  216. status->rx_buffer,
  217. length);
  218. if (length > 0)
  219. {
  220. //主站有需要输出时进行输出
  221. hw_dma_uart_begin_write(status->uart_no, (const char *)&status->tx_buffer[0], length);
  222. status->step++;
  223. }
  224. break;
  225. case 1:
  226. //等待输出完毕
  227. if (hw_dma_uart_write_finish(status->uart_no))
  228. {
  229. status->step = 0;
  230. hw_dma_uart_begin_read(status->uart_no, &status->rx_buffer[0], 1024);
  231. }
  232. break;
  233. }
  234. }
  235. /**
  236. * modbus应用的循环运行函数,根据自己需求编写
  237. *
  238. * @author lxz (2019/5/17/周五)
  239. *
  240. * @param void
  241. */
  242. void modbus_app_proc(void)
  243. {
  244. int index = 0;
  245. while (index < MODBUS_APP_NUMBER)
  246. {
  247. if (modbus_status[index].modbus_dir == 0)
  248. {
  249. modbus_slave_app(&modbus_status[index]);
  250. } else
  251. {
  252. modbus_master_app(&modbus_status[index]);
  253. }
  254. index++;
  255. }
  256. }