| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2019-06-21 flybreak first version
- */
- #include <rtthread.h>
- #include <user_mb_app.h>
- #include "control.h"
- #ifdef PKG_MODBUS_MASTER_SAMPLE
- #define SLAVE_ADDR MB_SAMPLE_TEST_SLAVE_ADDR
- #define PORT_NUM MB_MASTER_USING_PORT_NUM
- #define PORT_BAUDRATE MB_MASTER_USING_PORT_BAUDRATE
- #else
- #define SLAVE_ADDR 0x01
- #define PORT_NUM 5
- #define PORT_BAUDRATE 9600
- #endif
- #define PORT_PARITY MB_PAR_NONE
- #define MB_POLL_THREAD_PRIORITY 30
- #define MB_SEND_THREAD_PRIORITY 31
- //#define MB_SEND_REG_START 1034
- #define MB_SEND_REG_START 0
- #define MB_SEND_REG_NUM 10
- #define MB_POLL_CYCLE_MS 500
- Device_Status_typedef device_status;
- extern USHORT usMRegInBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_INPUT_NREGS];
- extern SHORT usMRegHoldBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_HOLDING_NREGS];
- static void send_thread_entry(void *parameter)
- {
- eMBMasterReqErrCode error_code = MB_MRE_NO_ERR;
- rt_uint16_t error_count = 0;
- //USHORT data[3] = {0};
- while (1)
- {
- /* Test Modbus Master */
- // data[0] = (USHORT)(rt_tick_get() / 10);
- // data[1] = (USHORT)(rt_tick_get() % 10);
- // data[3] = data[1] + 1;
- // error_code = eMBMasterReqWriteMultipleHoldingRegister(SLAVE_ADDR, /* salve address */
- // MB_SEND_REG_START, /* register start address */
- // MB_SEND_REG_NUM, /* register total number */
- // data, /* data to be written */
- // RT_WAITING_FOREVER); /* timeout */
- error_code = eMBMasterReqReadHoldingRegister(SLAVE_ADDR, MB_SEND_REG_START,2,1000);//读多个保持寄存器
- rt_kprintf("error_code: %d \n",error_code);//一直出错,我便把错误返回值打印出来
- if(error_code==0)
- {
- device_status.sensor_485_sta = 100;
- error_count = 0;
- //温度=25并且湿度=30不更新,否则会导致屏幕上显示空
- // if((int)usMRegHoldBuf[0][0]/10!=25&&(int)usMRegHoldBuf[0][1]/10!=30)
- // {
- rt_sprintf(device_status.tempture,"%d.%d",(int)usMRegHoldBuf[0][0]/100, (uint)usMRegHoldBuf[0][0] % 100);
- rt_sprintf(device_status.humidity,"%d.%d",(int)usMRegHoldBuf[0][1]/100, (uint)usMRegHoldBuf[0][1] % 100);
- // sprintf(device_status.tempture,"%0.01f",(float)usMRegHoldBuf[0][0]/100);
- // sprintf(device_status.humidity,"%0.01f",(float)usMRegHoldBuf[0][1]/100);
- // }
- }
- rt_kprintf("slave data-tem:%d,hum:%d\n",usMRegHoldBuf[0][0],usMRegHoldBuf[0][1]);//0代表从机ID=1
- rt_kprintf("slave data-tem:%s,hum:%s\n",device_status.tempture,device_status.humidity);//0代表从机ID=1
- rt_thread_delay(5000);
- /* Record the number of errors */
- if (error_code != 0)
- {
- error_count++;
- if(error_count == 10)
- {
- error_count = 0;
- device_status.sensor_485_sta = 0;
- }
- }
- }
- }
- static void mb_master_poll(void *parameter)
- {
- eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
- eMBMasterEnable();
- while (1)
- {
- eMBMasterPoll();
- rt_thread_mdelay(MB_POLL_CYCLE_MS);
- }
- }
- int mb_master_samlpe(void)
- {
- static rt_uint8_t is_init = 0;
- rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
- if (is_init > 0)
- {
- rt_kprintf("sample is running\n");
- return -RT_ERROR;
- }
- tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
- if (tid1 != RT_NULL)
- {
- rt_thread_startup(tid1);
- }
- else
- {
- goto __exit;
- }
- tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
- if (tid2 != RT_NULL)
- {
- rt_thread_startup(tid2);
- }
- else
- {
- goto __exit;
- }
- is_init = 1;
- return RT_EOK;
- __exit:
- if (tid1)
- rt_thread_delete(tid1);
- if (tid2)
- rt_thread_delete(tid2);
- return -RT_ERROR;
- }
- MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);
|