| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- //#include "modbus_io.h"
- //IO_STATUS OUTPUT; //结构变量声明
- //IO_STATUS INTPUT; //结构变量声明
- //#define RS485_RE GET_PIN(D, 1)
- //static void test_thread(void *param)
- //{
- // uint16_t tab_reg[64] = {0};
- // modbus_t *ctx = RT_NULL;
- // ctx = modbus_new_rtu("/dev/uart3", 9600, 'N', 8, 1);
- // modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
- // modbus_rtu_set_rts(ctx, RS485_RE, MODBUS_RTU_RTS_UP);
- // modbus_set_slave(ctx, 1);
- // modbus_connect(ctx);
- // modbus_set_response_timeout(ctx, 0, 1000000);
- // uint8_t num = 0;
- // uint8_t i = 0;
- // uint8_t temp = 0;
- // uint8_t rc = 0;
- // uint8_t read_bit[16] = {0};
- // uint8_t read_r_bit = 0;
- // while (1)
- // {
- // memset(tab_reg, 0, 64 * 2);
- // int regs = modbus_read_registers(ctx, 0, 20, tab_reg);
- // modbus_read_input_bits(ctx, 0, 8 ,read_bit);
- // for(i = 0; i < 8; i ++)
- // {
- // rt_kprintf("read_bit:%d\n",read_bit[i]);
- // if(read_bit[i] == 0)SW_FALSE(i);
- // else if(read_bit[i] == 1)SW_TRUE(i);
- // }
- // rt_kprintf("OUTPUT.status:%02x\n",OUTPUT.status);
- // if(out_0)rt_kprintf("out_0\n");
- // if(out_1)rt_kprintf("out_1\n");
- // if(out_2)rt_kprintf("out_2\n");
- // if(out_3)rt_kprintf("out_3\n");
- // if(out_4)rt_kprintf("out_4\n");
- // if(out_5)rt_kprintf("out_5\n");
- // if(out_6)rt_kprintf("out_6\n");
- // if(out_7)rt_kprintf("out_7\n");
-
-
- // for(i = 0; i < 8; i ++)
- // {
- //
- //
- // modbus_read_bits(ctx, i, 8 ,read_bit);
- // rt_kprintf("i:%02x, read_bit:%d\n",i,read_bit[i]);
- // if(read_bit[i] == 0)
- // {
- // rc = modbus_write_bit(ctx, i, 1 );
- // rt_kprintf("i:%d,rc:%d\n",i,rc);
- // rt_thread_mdelay(200);
- // }
- // else if(read_bit[i] == 1)
- // {
- // rc = modbus_write_bit(ctx, i, 0 );
- // rt_kprintf("i:%d,rc:%d\n",i,rc);
- // rt_thread_mdelay(200);
- // }
- // }
- // temp = modbus_read_input_bits(ctx, 0, 8 ,&read_r_bit);
- // rt_kprintf("read_input_bit:%02x,temp:%d\n",read_r_bit,temp);
- // for(i = 0; i <= 8; i ++)
- // {
- // temp = modbus_write_bit(ctx, i, 0 );
- // rt_kprintf("i:%d,rc:%d\n",i,temp);
- // rt_thread_mdelay(500);
- // }
-
-
- // printf("-------------------------------------------\n");
- // printf("[%4d][read num = %d]", num, regs);
- // num++;
- // int i;
- // for (i = 0; i < 20; i++)
- // {
- // printf("<%#x>", tab_reg[i]);
- // }
- // printf("\n");
- // printf("-------------------------------------------\n");
- // rt_thread_mdelay(500);
- // }
- //7-关闭modbus端口
- // modbus_close(ctx);
- //8-释放modbus资源
- // modbus_free(ctx);
- //}
- //static int rtu_test_init(void)
- //{
- // rt_pin_mode(RS485_RE, PIN_MODE_OUTPUT);
- // rt_thread_t tid;
- // tid = rt_thread_create("test",
- // test_thread, RT_NULL,
- // 2048,
- // 12, 10);
- // if (tid != RT_NULL)
- // rt_thread_startup(tid);
- // return RT_EOK;
- //}
- //INIT_APP_EXPORT(rtu_test_init);
- //MSH_CMD_EXPORT(rtu_test_init,zzz);
- ///*
- // * Copyright (c) 2006-2018, RT-Thread Development Team
- // *
- // * SPDX-License-Identifier: Apache-2.0
- // *
- // * Change Logs:
- // * Date Author Notes
- // * 2019-05-19 Warfalcon first implementation
- // */
- //#include <rthw.h>
- //#include <rtthread.h>
- //#include <rtdevice.h>
- //#include <board.h>
- //#include "easyflash.h"
- //#include "modbus.h"
- //#include "string.h"
- //// #define DEV_MODBUS_DEBUG
- //#define THE_QUEUE_NAME "rtu-msg"
- //#define THE_QUEUE_TIMEOUT 200
- //#define THE_MAX_QUEUE_MSG_SIZE 256
- //#define THE_MAX_QUEUE_SIZE 16
- //#define MODBUS_SERIAL_DEV "/dev/uart3"
- //#define MODBUS_SERIAL_BANDRATE 9600
- //#define THE_RELAY_CTRL_OPEN 1
- //#define THE_RELAY_CTRL_CLOSE 0
- //#define THE_RELAY_IS_OPEN 1
- //#define THE_RELAY_IS_CLOSE 0
- //#define THE_DEVICE_COMM_ERRCNT 10
- //#define THE_DEVICE_COMM_ERROR(err_cnt) \
- //{ \
- // if (err_cnt < 0xff) \
- // { \
- // err_cnt++; \
- // } \
- //}
- //#define THE_DEVICE_COMM_CLEAN(err_cnt) \
- //{ \
- // if (err_cnt >= THE_DEVICE_COMM_ERRCNT) \
- // { \
- // err_cnt = THE_DEVICE_COMM_ERRCNT - 1; \
- // } \
- // else \
- // { \
- // if (err_cnt) \
- // err_cnt--; \
- // } \
- //}
- //#define LOG_TAG "modbus.IO"
- //#define LOG_LVL LOG_LVL_ASSERT
- //#include <ulog.h>
- //static rt_mq_t msg_queue = RT_NULL;
- //static char *msg_buff = RT_NULL;
- ///* 每1个控制门号以1个字节来表示, 非0代表控制 */
- //static rt_uint8_t *relay_coil_buff = RT_NULL;
- ///* 每1个状态门号以1个字节来表示, 非0代表门开 */
- //static rt_uint8_t *input_status_buff = RT_NULL;
- ///* 每1个控制失败门号以1个字节来表示 */
- //static rt_uint8_t *relay_fail_buff = RT_NULL;
- ///* 每1个设备号以1个字节来表示 */
- //static rt_uint8_t *dev_error_buff = RT_NULL;
- ///* RS485总线挂载的最大设备数 */
- //static rt_uint8_t device_num;
- ///* 每个设备的最大通道数 */
- //static rt_uint8_t device_chn_num;
- //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)
- //{
- // if (input_status_buff == RT_NULL)
- // return RT_ERROR;
- // if (dev_error_buff == RT_NULL)
- // return RT_ERROR;
- // if (p_device_num == RT_NULL)
- // return RT_ERROR;
- // if (p_device_chn_num == RT_NULL)
- // return RT_ERROR;
- // *relay_status = input_status_buff;
- // *dev_error = dev_error_buff;
- // *p_device_num = device_num;
- // *p_device_chn_num = device_chn_num;
- // return RT_EOK;
- //}
- //rt_err_t dev_modbus_send_queue_msg(char *queue_msg)
- //{
- // RT_ASSERT(queue_msg != RT_NULL);
- // return rt_mq_urgent(msg_queue, queue_msg, rt_strlen(queue_msg) + 1);
- //}
- //static void dev_modbus_thread(void *param)
- //{
- // rt_err_t err;
- // modbus_t *ctx;
- // ctx = modbus_new_rtu(MODBUS_SERIAL_DEV, MODBUS_SERIAL_BANDRATE, 'N', 8, 1);
- // RT_ASSERT(ctx != RT_NULL);
- // /* 初始化RS485方向控制引脚 */
- //#ifdef BSP_USING_SUB_CTRL
- // rt_pin_mode(BSP_RS485_DIR_PIN, PIN_MODE_OUTPUT);
- // modbus_rtu_set_serial_mode(ctx, BSP_RS485_DIR_PIN);
- // modbus_rtu_set_rts(ctx, BSP_RS485_DIR_PIN, MODBUS_RTU_RTS_UP);
- //#endif
- // /* 初始化先设置为广播地址, 从机地址最大不能超过247 */
- // modbus_set_slave(ctx, 0);
- // /* 打开串口操作,包括波特率设置和串口初始化等 */
- // modbus_connect(ctx);
- // /* 初始化设置超时时间为1s */
- // modbus_set_response_timeout(ctx, 1, 0);
- //#ifdef DEV_MODBUS_DEBUG
- // modbus_set_debug(ctx, RT_TRUE);
- //#else
- // modbus_set_debug(ctx, RT_FALSE);
- //#endif
- // msg_queue = rt_mq_create(THE_QUEUE_NAME, THE_MAX_QUEUE_MSG_SIZE, THE_MAX_QUEUE_SIZE, RT_IPC_FLAG_FIFO);
- // RT_ASSERT(msg_queue != RT_NULL);
- // msg_buff = rt_malloc(THE_MAX_QUEUE_MSG_SIZE);
- // RT_ASSERT(msg_buff != RT_NULL);
- //
- // relay_coil_buff = rt_malloc(device_chn_num * device_num);
- // RT_ASSERT(relay_coil_buff != RT_NULL);
- // rt_memset(relay_coil_buff, 0, device_chn_num * device_num);
- // input_status_buff = rt_malloc(device_chn_num * device_num);
- // RT_ASSERT(input_status_buff != RT_NULL);
- // rt_memset(input_status_buff, 0, device_chn_num * device_num);
- // relay_fail_buff = rt_malloc(device_chn_num * device_num);
- // RT_ASSERT(relay_fail_buff != RT_NULL);
- // rt_memset(relay_fail_buff, 0, device_chn_num * device_num);
- // dev_error_buff = rt_malloc(device_num);
- // RT_ASSERT(dev_error_buff != RT_NULL);
- // rt_memset(dev_error_buff, 0, device_num);
- //
- // while (1)
- // {
- // err = rt_mq_recv(msg_queue, msg_buff, THE_MAX_QUEUE_MSG_SIZE, THE_QUEUE_TIMEOUT);
- // if (err == RT_EOK)
- // {
- // /* 消息解析完毕进行数据发送 */
- // for (int i = 0; i < device_num; i++)
- // {
- // /* 设置从机地址,从机地址从1开始 */
- // modbus_set_slave(ctx, i + 1);
- //
- // uint32_t timeout_cnt = 0;
- // for (int j = 0; j < device_chn_num; j++)
- // {
- // if (relay_coil_buff[i * device_chn_num + j])
- // timeout_cnt++;
- // }
- // if (timeout_cnt)
- // /* 设置超时时间,按照1个继电器1s超时时间计算 */
- // modbus_set_response_timeout(ctx, timeout_cnt, 0);
- // else
- // /* 当前设备没有需要打开的继电器,直接跳过 */
- // continue;//跳出本次循环剩余的的语句,进行一下次循环。
- //
- // /* 通过MODBUS发送数据,等待回应 */
- // if (modbus_write_bits(ctx, 0, device_chn_num, &relay_coil_buff[i * device_chn_num]) > 0)
- // {
- // LOG_D("modbus device[%d] write success.", i + 1);
- // THE_DEVICE_COMM_CLEAN(dev_error_buff[i]);
- // }
- // else
- // {
- // LOG_D("modbus device[%d] write failed.", i + 1);
- // THE_DEVICE_COMM_ERROR(dev_error_buff[i]);
- // }
- //
- // }
- // /* 等待继电器执行 */
- // rt_thread_mdelay(RT_TICK_PER_SECOND);
- // rt_memset(relay_fail_buff, 0, device_num * device_chn_num);
- // /* 读取输入端口状态值,查看是否运行反馈成功*/
- // for (int i = 0; i < device_num; i++)
- // {
- // /* 设置从机地址,从机地址从1开始 */
- // modbus_set_slave(ctx, i + 1);
- // /* 设置单个设备的超时时间: 1s */
- // modbus_set_response_timeout(ctx, 1, 0);
- // /* 通过MODBUS发送数据,等待回应 */
- // if (modbus_read_input_bits(ctx, 0, device_chn_num, &input_status_buff[i * device_chn_num]) > 0)
- // {
- // LOG_D("modbus device[%d] read success.", i + 1);
- // THE_DEVICE_COMM_CLEAN(dev_error_buff[i]);
- //
- // for (int j = 0; j < device_chn_num; j++)
- // {
- // 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]))
- // {
- // LOG_D("relay[%d] open failed.", (i * device_chn_num) + j + 1);
- // relay_fail_buff[i * device_chn_num + j] = 1;
- // }
- // }
- // }
- // else
- // {
- // LOG_D("modbus device[%d] read failed.", i + 1);
- // THE_DEVICE_COMM_ERROR(dev_error_buff[i]);
- //
- // for (int j = 0; j < device_chn_num; j++)
- // {
- // 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]))
- // relay_fail_buff[i * device_chn_num + j] = 1;
- // }
- // }
- // }
- // /* 构造门打开失败的topic_data */
- // char topic_data[256];
- // rt_memset(topic_data, 0, sizeof(topic_data));
- // }
- // else if (err == -RT_ETIMEOUT) /* 超时代表定时扫描时间到 */
- // {
- // /* 设置单个设备超时时间: 1s */
- // modbus_set_response_timeout(ctx, 1, 0);
- // for (int i = 0; i < device_num; i++)
- // {
- // /* 设置从机地址,从机地址从1开始 */
- // modbus_set_slave(ctx, i + 1);
- // /* 读门状态 */
- // if (modbus_read_input_bits(ctx, 0, device_chn_num, &input_status_buff[i * device_chn_num]) > 0)
- // {
- // THE_DEVICE_COMM_CLEAN(dev_error_buff[i]);
- // }
- // else
- // {
- // LOG_D("modbus device[%d] relay status read failed.", i + 1);
- // THE_DEVICE_COMM_ERROR(dev_error_buff[i]);
- // }
- // }
- // }
- // else
- // {
- // LOG_D("queue recv error.");
- // break;
- // }
- // }
- //
- // /* 关闭MODBUS组件和资源 */
- // modbus_close(ctx);
- // modbus_free(ctx);
- // if (msg_queue)
- // {
- // rt_mq_delete(msg_queue);
- // msg_queue = RT_NULL;
- // }
- // if (msg_buff)
- // {
- // rt_free(msg_buff);
- // msg_buff = RT_NULL;
- // }
- // if (relay_coil_buff)
- // {
- // rt_free(relay_coil_buff);
- // relay_coil_buff = RT_NULL;
- // }
- // if (input_status_buff)
- // {
- // rt_free(input_status_buff);
- // input_status_buff = RT_NULL;
- // }
- // if (dev_error_buff)
- // {
- // rt_free(dev_error_buff);
- // dev_error_buff = RT_NULL;
- // }
- //}
- //static int dev_modbus_rtu_init(void)
- //{
- // device_num = 1;
- // device_chn_num = 8;
- // rt_thread_t tid;
- // tid = rt_thread_create("modbus", dev_modbus_thread, RT_NULL, 2048, 11, 10);
- // if (tid != RT_NULL)
- // rt_thread_startup(tid);
- //
- // return RT_EOK;
- //}
- //INIT_APP_EXPORT(dev_modbus_rtu_init);
|