modbus_io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //#include "modbus_io.h"
  2. //IO_STATUS OUTPUT; //结构变量声明
  3. //IO_STATUS INTPUT; //结构变量声明
  4. //#define RS485_RE GET_PIN(D, 1)
  5. //static void test_thread(void *param)
  6. //{
  7. // uint16_t tab_reg[64] = {0};
  8. // modbus_t *ctx = RT_NULL;
  9. // ctx = modbus_new_rtu("/dev/uart3", 9600, 'N', 8, 1);
  10. // modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
  11. // modbus_rtu_set_rts(ctx, RS485_RE, MODBUS_RTU_RTS_UP);
  12. // modbus_set_slave(ctx, 1);
  13. // modbus_connect(ctx);
  14. // modbus_set_response_timeout(ctx, 0, 1000000);
  15. // uint8_t num = 0;
  16. // uint8_t i = 0;
  17. // uint8_t temp = 0;
  18. // uint8_t rc = 0;
  19. // uint8_t read_bit[16] = {0};
  20. // uint8_t read_r_bit = 0;
  21. // while (1)
  22. // {
  23. // memset(tab_reg, 0, 64 * 2);
  24. // int regs = modbus_read_registers(ctx, 0, 20, tab_reg);
  25. // modbus_read_input_bits(ctx, 0, 8 ,read_bit);
  26. // for(i = 0; i < 8; i ++)
  27. // {
  28. // rt_kprintf("read_bit:%d\n",read_bit[i]);
  29. // if(read_bit[i] == 0)SW_FALSE(i);
  30. // else if(read_bit[i] == 1)SW_TRUE(i);
  31. // }
  32. // rt_kprintf("OUTPUT.status:%02x\n",OUTPUT.status);
  33. // if(out_0)rt_kprintf("out_0\n");
  34. // if(out_1)rt_kprintf("out_1\n");
  35. // if(out_2)rt_kprintf("out_2\n");
  36. // if(out_3)rt_kprintf("out_3\n");
  37. // if(out_4)rt_kprintf("out_4\n");
  38. // if(out_5)rt_kprintf("out_5\n");
  39. // if(out_6)rt_kprintf("out_6\n");
  40. // if(out_7)rt_kprintf("out_7\n");
  41. // for(i = 0; i < 8; i ++)
  42. // {
  43. //
  44. //
  45. // modbus_read_bits(ctx, i, 8 ,read_bit);
  46. // rt_kprintf("i:%02x, read_bit:%d\n",i,read_bit[i]);
  47. // if(read_bit[i] == 0)
  48. // {
  49. // rc = modbus_write_bit(ctx, i, 1 );
  50. // rt_kprintf("i:%d,rc:%d\n",i,rc);
  51. // rt_thread_mdelay(200);
  52. // }
  53. // else if(read_bit[i] == 1)
  54. // {
  55. // rc = modbus_write_bit(ctx, i, 0 );
  56. // rt_kprintf("i:%d,rc:%d\n",i,rc);
  57. // rt_thread_mdelay(200);
  58. // }
  59. // }
  60. // temp = modbus_read_input_bits(ctx, 0, 8 ,&read_r_bit);
  61. // rt_kprintf("read_input_bit:%02x,temp:%d\n",read_r_bit,temp);
  62. // for(i = 0; i <= 8; i ++)
  63. // {
  64. // temp = modbus_write_bit(ctx, i, 0 );
  65. // rt_kprintf("i:%d,rc:%d\n",i,temp);
  66. // rt_thread_mdelay(500);
  67. // }
  68. // printf("-------------------------------------------\n");
  69. // printf("[%4d][read num = %d]", num, regs);
  70. // num++;
  71. // int i;
  72. // for (i = 0; i < 20; i++)
  73. // {
  74. // printf("<%#x>", tab_reg[i]);
  75. // }
  76. // printf("\n");
  77. // printf("-------------------------------------------\n");
  78. // rt_thread_mdelay(500);
  79. // }
  80. //7-关闭modbus端口
  81. // modbus_close(ctx);
  82. //8-释放modbus资源
  83. // modbus_free(ctx);
  84. //}
  85. //static int rtu_test_init(void)
  86. //{
  87. // rt_pin_mode(RS485_RE, PIN_MODE_OUTPUT);
  88. // rt_thread_t tid;
  89. // tid = rt_thread_create("test",
  90. // test_thread, RT_NULL,
  91. // 2048,
  92. // 12, 10);
  93. // if (tid != RT_NULL)
  94. // rt_thread_startup(tid);
  95. // return RT_EOK;
  96. //}
  97. //INIT_APP_EXPORT(rtu_test_init);
  98. //MSH_CMD_EXPORT(rtu_test_init,zzz);
  99. ///*
  100. // * Copyright (c) 2006-2018, RT-Thread Development Team
  101. // *
  102. // * SPDX-License-Identifier: Apache-2.0
  103. // *
  104. // * Change Logs:
  105. // * Date Author Notes
  106. // * 2019-05-19 Warfalcon first implementation
  107. // */
  108. //#include <rthw.h>
  109. //#include <rtthread.h>
  110. //#include <rtdevice.h>
  111. //#include <board.h>
  112. //#include "easyflash.h"
  113. //#include "modbus.h"
  114. //#include "string.h"
  115. //// #define DEV_MODBUS_DEBUG
  116. //#define THE_QUEUE_NAME "rtu-msg"
  117. //#define THE_QUEUE_TIMEOUT 200
  118. //#define THE_MAX_QUEUE_MSG_SIZE 256
  119. //#define THE_MAX_QUEUE_SIZE 16
  120. //#define MODBUS_SERIAL_DEV "/dev/uart3"
  121. //#define MODBUS_SERIAL_BANDRATE 9600
  122. //#define THE_RELAY_CTRL_OPEN 1
  123. //#define THE_RELAY_CTRL_CLOSE 0
  124. //#define THE_RELAY_IS_OPEN 1
  125. //#define THE_RELAY_IS_CLOSE 0
  126. //#define THE_DEVICE_COMM_ERRCNT 10
  127. //#define THE_DEVICE_COMM_ERROR(err_cnt) \
  128. //{ \
  129. // if (err_cnt < 0xff) \
  130. // { \
  131. // err_cnt++; \
  132. // } \
  133. //}
  134. //#define THE_DEVICE_COMM_CLEAN(err_cnt) \
  135. //{ \
  136. // if (err_cnt >= THE_DEVICE_COMM_ERRCNT) \
  137. // { \
  138. // err_cnt = THE_DEVICE_COMM_ERRCNT - 1; \
  139. // } \
  140. // else \
  141. // { \
  142. // if (err_cnt) \
  143. // err_cnt--; \
  144. // } \
  145. //}
  146. //#define LOG_TAG "modbus.IO"
  147. //#define LOG_LVL LOG_LVL_ASSERT
  148. //#include <ulog.h>
  149. //static rt_mq_t msg_queue = RT_NULL;
  150. //static char *msg_buff = RT_NULL;
  151. ///* 每1个控制门号以1个字节来表示, 非0代表控制 */
  152. //static rt_uint8_t *relay_coil_buff = RT_NULL;
  153. ///* 每1个状态门号以1个字节来表示, 非0代表门开 */
  154. //static rt_uint8_t *input_status_buff = RT_NULL;
  155. ///* 每1个控制失败门号以1个字节来表示 */
  156. //static rt_uint8_t *relay_fail_buff = RT_NULL;
  157. ///* 每1个设备号以1个字节来表示 */
  158. //static rt_uint8_t *dev_error_buff = RT_NULL;
  159. ///* RS485总线挂载的最大设备数 */
  160. //static rt_uint8_t device_num;
  161. ///* 每个设备的最大通道数 */
  162. //static rt_uint8_t device_chn_num;
  163. //rt_err_t dev_modbus_get_period_data(rt_uint8_t **relay_status, rt_uint16_t **relay_alarm, rt_uint8_t **dev_error, rt_uint8_t *p_device_num, rt_uint8_t *p_device_chn_num)
  164. //{
  165. // if (input_status_buff == RT_NULL)
  166. // return RT_ERROR;
  167. // if (dev_error_buff == RT_NULL)
  168. // return RT_ERROR;
  169. // if (p_device_num == RT_NULL)
  170. // return RT_ERROR;
  171. // if (p_device_chn_num == RT_NULL)
  172. // return RT_ERROR;
  173. // *relay_status = input_status_buff;
  174. // *dev_error = dev_error_buff;
  175. // *p_device_num = device_num;
  176. // *p_device_chn_num = device_chn_num;
  177. // return RT_EOK;
  178. //}
  179. //rt_err_t dev_modbus_send_queue_msg(char *queue_msg)
  180. //{
  181. // RT_ASSERT(queue_msg != RT_NULL);
  182. // return rt_mq_urgent(msg_queue, queue_msg, rt_strlen(queue_msg) + 1);
  183. //}
  184. //static void dev_modbus_thread(void *param)
  185. //{
  186. // rt_err_t err;
  187. // modbus_t *ctx;
  188. // ctx = modbus_new_rtu(MODBUS_SERIAL_DEV, MODBUS_SERIAL_BANDRATE, 'N', 8, 1);
  189. // RT_ASSERT(ctx != RT_NULL);
  190. // /* 初始化RS485方向控制引脚 */
  191. //#ifdef BSP_USING_SUB_CTRL
  192. // rt_pin_mode(BSP_RS485_DIR_PIN, PIN_MODE_OUTPUT);
  193. // modbus_rtu_set_serial_mode(ctx, BSP_RS485_DIR_PIN);
  194. // modbus_rtu_set_rts(ctx, BSP_RS485_DIR_PIN, MODBUS_RTU_RTS_UP);
  195. //#endif
  196. // /* 初始化先设置为广播地址, 从机地址最大不能超过247 */
  197. // modbus_set_slave(ctx, 0);
  198. // /* 打开串口操作,包括波特率设置和串口初始化等 */
  199. // modbus_connect(ctx);
  200. // /* 初始化设置超时时间为1s */
  201. // modbus_set_response_timeout(ctx, 1, 0);
  202. //#ifdef DEV_MODBUS_DEBUG
  203. // modbus_set_debug(ctx, RT_TRUE);
  204. //#else
  205. // modbus_set_debug(ctx, RT_FALSE);
  206. //#endif
  207. // msg_queue = rt_mq_create(THE_QUEUE_NAME, THE_MAX_QUEUE_MSG_SIZE, THE_MAX_QUEUE_SIZE, RT_IPC_FLAG_FIFO);
  208. // RT_ASSERT(msg_queue != RT_NULL);
  209. // msg_buff = rt_malloc(THE_MAX_QUEUE_MSG_SIZE);
  210. // RT_ASSERT(msg_buff != RT_NULL);
  211. //
  212. // relay_coil_buff = rt_malloc(device_chn_num * device_num);
  213. // RT_ASSERT(relay_coil_buff != RT_NULL);
  214. // rt_memset(relay_coil_buff, 0, device_chn_num * device_num);
  215. // input_status_buff = rt_malloc(device_chn_num * device_num);
  216. // RT_ASSERT(input_status_buff != RT_NULL);
  217. // rt_memset(input_status_buff, 0, device_chn_num * device_num);
  218. // relay_fail_buff = rt_malloc(device_chn_num * device_num);
  219. // RT_ASSERT(relay_fail_buff != RT_NULL);
  220. // rt_memset(relay_fail_buff, 0, device_chn_num * device_num);
  221. // dev_error_buff = rt_malloc(device_num);
  222. // RT_ASSERT(dev_error_buff != RT_NULL);
  223. // rt_memset(dev_error_buff, 0, device_num);
  224. //
  225. // while (1)
  226. // {
  227. // err = rt_mq_recv(msg_queue, msg_buff, THE_MAX_QUEUE_MSG_SIZE, THE_QUEUE_TIMEOUT);
  228. // if (err == RT_EOK)
  229. // {
  230. // /* 消息解析完毕进行数据发送 */
  231. // for (int i = 0; i < device_num; i++)
  232. // {
  233. // /* 设置从机地址,从机地址从1开始 */
  234. // modbus_set_slave(ctx, i + 1);
  235. //
  236. // uint32_t timeout_cnt = 0;
  237. // for (int j = 0; j < device_chn_num; j++)
  238. // {
  239. // if (relay_coil_buff[i * device_chn_num + j])
  240. // timeout_cnt++;
  241. // }
  242. // if (timeout_cnt)
  243. // /* 设置超时时间,按照1个继电器1s超时时间计算 */
  244. // modbus_set_response_timeout(ctx, timeout_cnt, 0);
  245. // else
  246. // /* 当前设备没有需要打开的继电器,直接跳过 */
  247. // continue;//跳出本次循环剩余的的语句,进行一下次循环。
  248. //
  249. // /* 通过MODBUS发送数据,等待回应 */
  250. // if (modbus_write_bits(ctx, 0, device_chn_num, &relay_coil_buff[i * device_chn_num]) > 0)
  251. // {
  252. // LOG_D("modbus device[%d] write success.", i + 1);
  253. // THE_DEVICE_COMM_CLEAN(dev_error_buff[i]);
  254. // }
  255. // else
  256. // {
  257. // LOG_D("modbus device[%d] write failed.", i + 1);
  258. // THE_DEVICE_COMM_ERROR(dev_error_buff[i]);
  259. // }
  260. //
  261. // }
  262. // /* 等待继电器执行 */
  263. // rt_thread_mdelay(RT_TICK_PER_SECOND);
  264. // rt_memset(relay_fail_buff, 0, device_num * device_chn_num);
  265. // /* 读取输入端口状态值,查看是否运行反馈成功*/
  266. // for (int i = 0; i < device_num; i++)
  267. // {
  268. // /* 设置从机地址,从机地址从1开始 */
  269. // modbus_set_slave(ctx, i + 1);
  270. // /* 设置单个设备的超时时间: 1s */
  271. // modbus_set_response_timeout(ctx, 1, 0);
  272. // /* 通过MODBUS发送数据,等待回应 */
  273. // if (modbus_read_input_bits(ctx, 0, device_chn_num, &input_status_buff[i * device_chn_num]) > 0)
  274. // {
  275. // LOG_D("modbus device[%d] read success.", i + 1);
  276. // THE_DEVICE_COMM_CLEAN(dev_error_buff[i]);
  277. //
  278. // for (int j = 0; j < device_chn_num; j++)
  279. // {
  280. // if ((relay_coil_buff[i * device_chn_num + j] == THE_RELAY_IS_OPEN) &&
  281. // (relay_coil_buff[i * device_chn_num + j] != input_status_buff[i * device_chn_num + j]))
  282. // {
  283. // LOG_D("relay[%d] open failed.", (i * device_chn_num) + j + 1);
  284. // relay_fail_buff[i * device_chn_num + j] = 1;
  285. // }
  286. // }
  287. // }
  288. // else
  289. // {
  290. // LOG_D("modbus device[%d] read failed.", i + 1);
  291. // THE_DEVICE_COMM_ERROR(dev_error_buff[i]);
  292. //
  293. // for (int j = 0; j < device_chn_num; j++)
  294. // {
  295. // if ((relay_coil_buff[i * device_chn_num + j] == THE_RELAY_IS_OPEN) && (relay_coil_buff[i * device_chn_num + j] != input_status_buff[i * device_chn_num + j]))
  296. // relay_fail_buff[i * device_chn_num + j] = 1;
  297. // }
  298. // }
  299. // }
  300. // /* 构造门打开失败的topic_data */
  301. // char topic_data[256];
  302. // rt_memset(topic_data, 0, sizeof(topic_data));
  303. // }
  304. // else if (err == -RT_ETIMEOUT) /* 超时代表定时扫描时间到 */
  305. // {
  306. // /* 设置单个设备超时时间: 1s */
  307. // modbus_set_response_timeout(ctx, 1, 0);
  308. // for (int i = 0; i < device_num; i++)
  309. // {
  310. // /* 设置从机地址,从机地址从1开始 */
  311. // modbus_set_slave(ctx, i + 1);
  312. // /* 读门状态 */
  313. // if (modbus_read_input_bits(ctx, 0, device_chn_num, &input_status_buff[i * device_chn_num]) > 0)
  314. // {
  315. // THE_DEVICE_COMM_CLEAN(dev_error_buff[i]);
  316. // }
  317. // else
  318. // {
  319. // LOG_D("modbus device[%d] relay status read failed.", i + 1);
  320. // THE_DEVICE_COMM_ERROR(dev_error_buff[i]);
  321. // }
  322. // }
  323. // }
  324. // else
  325. // {
  326. // LOG_D("queue recv error.");
  327. // break;
  328. // }
  329. // }
  330. //
  331. // /* 关闭MODBUS组件和资源 */
  332. // modbus_close(ctx);
  333. // modbus_free(ctx);
  334. // if (msg_queue)
  335. // {
  336. // rt_mq_delete(msg_queue);
  337. // msg_queue = RT_NULL;
  338. // }
  339. // if (msg_buff)
  340. // {
  341. // rt_free(msg_buff);
  342. // msg_buff = RT_NULL;
  343. // }
  344. // if (relay_coil_buff)
  345. // {
  346. // rt_free(relay_coil_buff);
  347. // relay_coil_buff = RT_NULL;
  348. // }
  349. // if (input_status_buff)
  350. // {
  351. // rt_free(input_status_buff);
  352. // input_status_buff = RT_NULL;
  353. // }
  354. // if (dev_error_buff)
  355. // {
  356. // rt_free(dev_error_buff);
  357. // dev_error_buff = RT_NULL;
  358. // }
  359. //}
  360. //static int dev_modbus_rtu_init(void)
  361. //{
  362. // device_num = 1;
  363. // device_chn_num = 8;
  364. // rt_thread_t tid;
  365. // tid = rt_thread_create("modbus", dev_modbus_thread, RT_NULL, 2048, 11, 10);
  366. // if (tid != RT_NULL)
  367. // rt_thread_startup(tid);
  368. //
  369. // return RT_EOK;
  370. //}
  371. //INIT_APP_EXPORT(dev_modbus_rtu_init);