From 5279977f2e734c7b0600351c3944497d3474512f Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sat, 10 Apr 2021 19:07:48 +0800 Subject: [PATCH] format code --- APP/inc/app_task.h | 6 +- APP/inc/cpuusage.h | 2 +- APP/inc/delay_conf.h | 34 +-- APP/inc/rtconfig.h | 16 +- APP/src/app.c | 16 +- APP/src/app_task.c | 246 +++++++++--------- BSP/inc/bsp.h | 48 ++-- BSP/src/bsp.c | 180 ++++++------- BSP/src/usart.c | 2 +- FreeModbus/modbus/ascii/mbascii.c | 4 +- FreeModbus/modbus/ascii/mbascii.h | 2 +- FreeModbus/modbus/functions/mbfunccoils.c | 8 +- FreeModbus/modbus/functions/mbfunccoils_m.c | 96 +++---- FreeModbus/modbus/functions/mbfuncdiag.c | 2 +- FreeModbus/modbus/functions/mbfuncdisc.c | 6 +- FreeModbus/modbus/functions/mbfuncdisc_m.c | 46 ++-- FreeModbus/modbus/functions/mbfuncholding.c | 4 +- FreeModbus/modbus/functions/mbfuncholding_m.c | 138 +++++----- FreeModbus/modbus/functions/mbfuncinput.c | 4 +- FreeModbus/modbus/functions/mbfuncinput_m.c | 34 +-- FreeModbus/modbus/functions/mbfuncother.c | 2 +- FreeModbus/modbus/functions/mbutils.c | 2 +- FreeModbus/modbus/include/mb.h | 42 +-- FreeModbus/modbus/include/mb_m.h | 44 ++-- FreeModbus/modbus/include/mbconfig.h | 2 +- FreeModbus/modbus/include/mbframe.h | 2 +- FreeModbus/modbus/include/mbfunc.h | 2 +- FreeModbus/modbus/include/mbport.h | 2 +- FreeModbus/modbus/include/mbproto.h | 2 +- FreeModbus/modbus/include/mbutils.h | 2 +- FreeModbus/modbus/mb.c | 2 +- FreeModbus/modbus/mb_m.c | 214 +++++++-------- FreeModbus/modbus/rtu/mbcrc.c | 12 +- FreeModbus/modbus/rtu/mbcrc.h | 2 +- FreeModbus/modbus/rtu/mbrtu.c | 2 +- FreeModbus/modbus/rtu/mbrtu.h | 4 +- FreeModbus/modbus/rtu/mbrtu_m.c | 126 ++++----- FreeModbus/modbus/tcp/mbtcp.c | 14 +- FreeModbus/modbus/tcp/mbtcp.h | 2 +- FreeModbus/port/port.h | 6 +- FreeModbus/port/rtt/portserial.c | 6 +- FreeModbus/port/rtt/portserial_m.c | 6 +- 42 files changed, 696 insertions(+), 696 deletions(-) diff --git a/APP/inc/app_task.h b/APP/inc/app_task.h index f8a6bbb..93d2620 100644 --- a/APP/inc/app_task.h +++ b/APP/inc/app_task.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include "bsp.h" @@ -15,7 +15,7 @@ #include "user_mb_app.h" /*******************************************************************************************************/ -// DEFINES +// DEFINES /*******************************************************************************************************/ @@ -37,7 +37,7 @@ extern uint16_t usSRegHoldBuf[] ; extern void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor); extern void cpu_usage_init(void); - + void rtthread_startup(void); diff --git a/APP/inc/cpuusage.h b/APP/inc/cpuusage.h index 76873ef..6e8c32b 100644 --- a/APP/inc/cpuusage.h +++ b/APP/inc/cpuusage.h @@ -11,7 +11,7 @@ void cpu_usage_init(void); - + diff --git a/APP/inc/delay_conf.h b/APP/inc/delay_conf.h index d9115bd..6232637 100644 --- a/APP/inc/delay_conf.h +++ b/APP/inc/delay_conf.h @@ -2,7 +2,7 @@ // Filename : delay_config.h // Version : V1.00 // Programmer(s) : Liuqiuhu -// funcion : This file is used to configure the delay time +// funcion : This file is used to configure the delay time /*********************************************************************************************************/ #ifndef __DELAY_CONF_H__ #define __DELAY_CONF_H__ @@ -10,34 +10,34 @@ #include"rtconfig.h" #if RT_TICK_PER_SECOND == 1 -#define DELAY_1S (RT_TICK_PER_SECOND) -#define DELAY_S(X) (X*DELAY_1S) +#define DELAY_1S (RT_TICK_PER_SECOND) +#define DELAY_S(X) (X*DELAY_1S) -#elif RT_TICK_PER_SECOND == 10 -#define DELAY_100MS(X) (X) -#define DELAY_S(X) (X*10) +#elif RT_TICK_PER_SECOND == 10 +#define DELAY_100MS(X) (X) +#define DELAY_S(X) (X*10) -#elif RT_TICK_PER_SECOND == 100 -#define DELAY_10MS(X) (X) -#define DELAY_100MS(X) (X*10) -#define DELAY_S(X) (X*100) +#elif RT_TICK_PER_SECOND == 100 +#define DELAY_10MS(X) (X) +#define DELAY_100MS(X) (X*10) +#define DELAY_S(X) (X*100) #elif (RT_TICK_PER_SECOND == 1000) -#define DELAY_1MS (RT_TICK_PER_SECOND/1000) +#define DELAY_1MS (RT_TICK_PER_SECOND/1000) #define DELAY_MS(X) (X*DELAY_1MS) -#define DELAY_S(X) (X*1000*DELAY_1MS) +#define DELAY_S(X) (X*1000*DELAY_1MS) #elif (RT_TICK_PER_SECOND == 10000) #define DELAY_100US(X) (X*RT_TICK_PER_SECOND/10000) -#define DELAY_1MS (RT_TICK_PER_SECOND/1000) +#define DELAY_1MS (RT_TICK_PER_SECOND/1000) #define DELAY_MS(X) (X*DELAY_1MS) -#define DELAY_S(X) (X*1000*DELAY_1MS) +#define DELAY_S(X) (X*1000*DELAY_1MS) #endif -#define DELAY_SYS_RUN_LED DELAY_MS(500) -#define DELAY_SYS_SLEEP_LED DELAY_MS(1000) +#define DELAY_SYS_RUN_LED DELAY_MS(500) +#define DELAY_SYS_SLEEP_LED DELAY_MS(1000) -#endif +#endif diff --git a/APP/inc/rtconfig.h b/APP/inc/rtconfig.h index 03aaf38..4bf9303 100644 --- a/APP/inc/rtconfig.h +++ b/APP/inc/rtconfig.h @@ -3,16 +3,16 @@ #define __RTTHREAD_CFG_H__ /* RT_NAME_MAX*/ -#define RT_NAME_MAX 24 +#define RT_NAME_MAX 24 /* RT_ALIGN_SIZE*/ -#define RT_ALIGN_SIZE 8 +#define RT_ALIGN_SIZE 8 /* PRIORITY_MAX */ -#define RT_THREAD_PRIORITY_MAX 32 +#define RT_THREAD_PRIORITY_MAX 32 /* Tick per Second */ -#define RT_TICK_PER_SECOND 10000 //0.1ms +#define RT_TICK_PER_SECOND 10000 //0.1ms /* SECTION: RT_DEBUG */ /* Thread Debug */ @@ -26,9 +26,9 @@ /* Using Software Timer */ #define RT_USING_TIMER_SOFT -#define RT_TIMER_THREAD_PRIO 4 -#define RT_TIMER_THREAD_STACK_SIZE 512 -#define RT_TIMER_TICK_PER_SECOND 1000 +#define RT_TIMER_THREAD_PRIO 4 +#define RT_TIMER_THREAD_STACK_SIZE 512 +#define RT_TIMER_TICK_PER_SECOND 1000 /* SECTION: IPC */ /* Using Semaphore*/ @@ -69,7 +69,7 @@ /* SECTION: Console options */ /* the buffer size of console*/ -#define RT_CONSOLEBUF_SIZE 128 +#define RT_CONSOLEBUF_SIZE 128 /* SECTION: finsh, a C-Express shell */ //#define RT_USING_FINSH diff --git a/APP/src/app.c b/APP/src/app.c index 7de6699..537dda0 100644 --- a/APP/src/app.c +++ b/APP/src/app.c @@ -2,18 +2,18 @@ int main(void) { - rt_uint32_t UNUSED level; + rt_uint32_t UNUSED level; - /* disable interrupt first */ - level = rt_hw_interrupt_disable(); + /* disable interrupt first */ + level = rt_hw_interrupt_disable(); - /* init system setting */ - SystemInit(); + /* init system setting */ + SystemInit(); - /* startup RT-Thread RTOS */ - rtthread_startup(); + /* startup RT-Thread RTOS */ + rtthread_startup(); - return 0; + return 0; } diff --git a/APP/src/app_task.c b/APP/src/app_task.c index a6c92e9..25685b7 100644 --- a/APP/src/app_task.c +++ b/APP/src/app_task.c @@ -8,15 +8,15 @@ extern int Image$$RW_IRAM1$$ZI$$Limit; extern int __bss_end; #endif -uint8_t CpuUsageMajor, CpuUsageMinor; //CPUʹ +uint8_t CpuUsageMajor, CpuUsageMinor; //CPU使用率 USHORT usModbusUserData[MB_PDU_SIZE_MAX]; UCHAR ucModbusUserData[MB_PDU_SIZE_MAX]; -//====================ϵͳ߳ȼ================================== -#define thread_SysMonitor_Prio 11 -#define thread_ModbusSlavePoll_Prio 10 -#define thread_ModbusMasterPoll_Prio 9 +//====================操作系统各线程优先级================================== +#define thread_SysMonitor_Prio 11 +#define thread_ModbusSlavePoll_Prio 10 +#define thread_ModbusMasterPoll_Prio 9 ALIGN(RT_ALIGN_SIZE) -//====================ϵͳ̶߳ջ==================================== +//====================操作系统各线程堆栈==================================== static rt_uint8_t thread_SysMonitor_stack[256]; static rt_uint8_t thread_ModbusSlavePoll_stack[512]; static rt_uint8_t thread_ModbusMasterPoll_stack[512]; @@ -25,170 +25,170 @@ struct rt_thread thread_SysMonitor; struct rt_thread thread_ModbusSlavePoll; struct rt_thread thread_ModbusMasterPoll; -//***************************ϵͳ߳*************************** -//: void thread_entry_SysRunLed(void* parameter) -//ڲ -//ڲ -// עEditorArmink 2013-08-02 Company: BXXJS +//***************************系统监控线程*************************** +//函数定义: void thread_entry_SysRunLed(void* parameter) +//入口参数:无 +//出口参数:无 +//备 注:Editor:Armink 2013-08-02 Company: BXXJS //****************************************************************** void thread_entry_SysMonitor(void* parameter) { - eMBMasterReqErrCode errorCode = MB_MRE_NO_ERR; - uint16_t errorCount = 0; - while (1) - { - cpu_usage_get(&CpuUsageMajor, &CpuUsageMinor); - usSRegHoldBuf[S_HD_CPU_USAGE_MAJOR] = CpuUsageMajor; - usSRegHoldBuf[S_HD_CPU_USAGE_MINOR] = CpuUsageMinor; - LED_LED1_ON; - LED_LED2_ON; - rt_thread_delay(DELAY_SYS_RUN_LED); - LED_LED1_OFF; - LED_LED2_OFF; - rt_thread_delay(DELAY_SYS_RUN_LED); - IWDG_Feed(); //feed the dog - //Test Modbus Master - usModbusUserData[0] = (USHORT)(rt_tick_get()/10); - usModbusUserData[1] = (USHORT)(rt_tick_get()%10); - ucModbusUserData[0] = 0x1F; -// errorCode = eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER); -// errorCode = eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER); - errorCode = eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER); -// errorCode = eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER); -// errorCode = eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER); -// errorCode = eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER); -// errorCode = eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER); -// errorCode = eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER); -// errorCode = eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER); - //¼ - if (errorCode != MB_MRE_NO_ERR) { - errorCount++; - } - } + eMBMasterReqErrCode errorCode = MB_MRE_NO_ERR; + uint16_t errorCount = 0; + while (1) + { + cpu_usage_get(&CpuUsageMajor, &CpuUsageMinor); + usSRegHoldBuf[S_HD_CPU_USAGE_MAJOR] = CpuUsageMajor; + usSRegHoldBuf[S_HD_CPU_USAGE_MINOR] = CpuUsageMinor; + LED_LED1_ON; + LED_LED2_ON; + rt_thread_delay(DELAY_SYS_RUN_LED); + LED_LED1_OFF; + LED_LED2_OFF; + rt_thread_delay(DELAY_SYS_RUN_LED); + IWDG_Feed(); //feed the dog + //Test Modbus Master + usModbusUserData[0] = (USHORT)(rt_tick_get()/10); + usModbusUserData[1] = (USHORT)(rt_tick_get()%10); + ucModbusUserData[0] = 0x1F; +// errorCode = eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER); +// errorCode = eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER); + errorCode = eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER); +// errorCode = eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER); +// errorCode = eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER); +// errorCode = eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER); +// errorCode = eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER); +// errorCode = eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER); +// errorCode = eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER); + //记录出错次数 + if (errorCode != MB_MRE_NO_ERR) { + errorCount++; + } + } } -//************************ Modbusӻѵ߳*************************** -//: void thread_entry_ModbusSlavePoll(void* parameter) -//ڲ -//ڲ -// עEditorArmink 2013-08-02 Company: BXXJS +//************************ Modbus从机轮训线程*************************** +//函数定义: void thread_entry_ModbusSlavePoll(void* parameter) +//入口参数:无 +//出口参数:无 +//备 注:Editor:Armink 2013-08-02 Company: BXXJS //****************************************************************** void thread_entry_ModbusSlavePoll(void* parameter) { - eMBInit(MB_RTU, 0x01, 1, 115200, MB_PAR_EVEN); - eMBEnable(); - while (1) - { - eMBPoll(); - } + eMBInit(MB_RTU, 0x01, 1, 115200, MB_PAR_EVEN); + eMBEnable(); + while (1) + { + eMBPoll(); + } } -//************************ Modbusѵ߳*************************** -//: void thread_entry_ModbusMasterPoll(void* parameter) -//ڲ -//ڲ -// עEditorArmink 2013-08-28 Company: BXXJS +//************************ Modbus主机轮训线程*************************** +//函数定义: void thread_entry_ModbusMasterPoll(void* parameter) +//入口参数:无 +//出口参数:无 +//备 注:Editor:Armink 2013-08-28 Company: BXXJS //****************************************************************** void thread_entry_ModbusMasterPoll(void* parameter) { - eMBMasterInit(MB_RTU, 2, 115200, MB_PAR_EVEN); - eMBMasterEnable(); - while (1) - { - eMBMasterPoll(); - } + eMBMasterInit(MB_RTU, 2, 115200, MB_PAR_EVEN); + eMBMasterEnable(); + while (1) + { + eMBMasterPoll(); + } } -//**********************ϵͳʼ******************************** -//: int rt_application_init(void) -//ڲ -//ڲ -// עEditorLiuqiuhu 2013-1-31 Company: BXXJS +//**********************系统初始化函数******************************** +//函数定义: int rt_application_init(void) +//入口参数:无 +//出口参数:无 +//备 注:Editor:Liuqiuhu 2013-1-31 Company: BXXJS //******************************************************************** int rt_application_init(void) { - rt_thread_init(&thread_SysMonitor, "SysMonitor", thread_entry_SysMonitor, - RT_NULL, thread_SysMonitor_stack, sizeof(thread_SysMonitor_stack), - thread_SysMonitor_Prio, 5); - rt_thread_startup(&thread_SysMonitor); + rt_thread_init(&thread_SysMonitor, "SysMonitor", thread_entry_SysMonitor, + RT_NULL, thread_SysMonitor_stack, sizeof(thread_SysMonitor_stack), + thread_SysMonitor_Prio, 5); + rt_thread_startup(&thread_SysMonitor); - rt_thread_init(&thread_ModbusSlavePoll, "MBSlavePoll", - thread_entry_ModbusSlavePoll, RT_NULL, thread_ModbusSlavePoll_stack, - sizeof(thread_ModbusSlavePoll_stack), thread_ModbusSlavePoll_Prio, - 5); - rt_thread_startup(&thread_ModbusSlavePoll); + rt_thread_init(&thread_ModbusSlavePoll, "MBSlavePoll", + thread_entry_ModbusSlavePoll, RT_NULL, thread_ModbusSlavePoll_stack, + sizeof(thread_ModbusSlavePoll_stack), thread_ModbusSlavePoll_Prio, + 5); + rt_thread_startup(&thread_ModbusSlavePoll); - rt_thread_init(&thread_ModbusMasterPoll, "MBMasterPoll", - thread_entry_ModbusMasterPoll, RT_NULL, thread_ModbusMasterPoll_stack, - sizeof(thread_ModbusMasterPoll_stack), thread_ModbusMasterPoll_Prio, - 5); - rt_thread_startup(&thread_ModbusMasterPoll); + rt_thread_init(&thread_ModbusMasterPoll, "MBMasterPoll", + thread_entry_ModbusMasterPoll, RT_NULL, thread_ModbusMasterPoll_stack, + sizeof(thread_ModbusMasterPoll_stack), thread_ModbusMasterPoll_Prio, + 5); + rt_thread_startup(&thread_ModbusMasterPoll); - return 0; + return 0; } -//**************************ʼRT-Thread************************************* -//: void rtthread_startup(void) -//ڲ -//ڲ -// עEditorArmink 2011-04-04 Company: BXXJS +//**************************初始化RT-Thread函数************************************* +//函数定义: void rtthread_startup(void) +//入口参数:无 +//出口参数:无 +//备 注:Editor:Armink 2011-04-04 Company: BXXJS //********************************************************************************** void rtthread_startup(void) { - /* init board */ - rt_hw_board_init(); + /* init board */ + rt_hw_board_init(); - /* show version */ - rt_show_version(); + /* show version */ + rt_show_version(); - /* init tick */ - rt_system_tick_init(); + /* init tick */ + rt_system_tick_init(); - /* init kernel object */ - rt_system_object_init(); + /* init kernel object */ + rt_system_object_init(); - /* init timer system */ - rt_system_timer_init(); + /* init timer system */ + rt_system_timer_init(); #ifdef RT_USING_HEAP #ifdef __CC_ARM - rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)STM32_SRAM_END); + rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)STM32_SRAM_END); #elif __ICCARM__ - rt_system_heap_init(__segment_end("HEAP"), (void*)STM32_SRAM_END); + rt_system_heap_init(__segment_end("HEAP"), (void*)STM32_SRAM_END); #else - /* init memory system */ - rt_system_heap_init((void*)&__bss_end, (void*)STM32_SRAM_END); + /* init memory system */ + rt_system_heap_init((void*)&__bss_end, (void*)STM32_SRAM_END); #endif #endif - /* init scheduler system */ - rt_system_scheduler_init(); + /* init scheduler system */ + rt_system_scheduler_init(); - /* init all device */ - rt_device_init_all(); + /* init all device */ + rt_device_init_all(); - /* init application */ - rt_application_init(); + /* init application */ + rt_application_init(); #ifdef RT_USING_FINSH - /* init finsh */ - finsh_system_init(); - finsh_set_device("uart1"); + /* init finsh */ + finsh_system_init(); + finsh_set_device("uart1"); #endif - /* init timer thread */ - rt_system_timer_thread_init(); + /* init timer thread */ + rt_system_timer_thread_init(); - /* init idle thread */ - rt_thread_idle_init(); + /* init idle thread */ + rt_thread_idle_init(); - /* Add CPU usage to system */ - cpu_usage_init(); + /* Add CPU usage to system */ + cpu_usage_init(); - /* start scheduler */ - rt_system_scheduler_start(); + /* start scheduler */ + rt_system_scheduler_start(); - /* never reach here */ - return; + /* never reach here */ + return; } diff --git a/BSP/inc/bsp.h b/BSP/inc/bsp.h index 51dea9d..98ca675 100644 --- a/BSP/inc/bsp.h +++ b/BSP/inc/bsp.h @@ -26,23 +26,23 @@ */ /* board configuration */ // SDCard Driver <1=>SDIO sdcard <0=>SPI MMC card -// Default: 1 -#define STM32_USE_SDIO 0 +// Default: 1 +#define STM32_USE_SDIO 0 /* whether use board external SRAM memory */ // Use external SRAM memory on the board -// Enable External SRAM memory +// Enable External SRAM memory #define STM32_EXT_SRAM 0 -// Begin Address of External SRAM -// Default: 0x68000000 +// Begin Address of External SRAM +// Default: 0x68000000 #define STM32_EXT_SRAM_BEGIN 0x68000000 /* the begining address of external SRAM */ -// End Address of External SRAM -// Default: 0x68080000 +// End Address of External SRAM +// Default: 0x68080000 #define STM32_EXT_SRAM_END 0x68080000 /* the end address of external SRAM */ // // Internal SRAM memory size[Kbytes] <8-64> -// Default: 64 +// Default: 64 #define STM32_SRAM_SIZE 20 #define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024) @@ -50,20 +50,20 @@ #define RT_USING_UART3 #define RT_USING_UART2 #define RT_USING_UART1 -#define RT_UART_RX_BUFFER_SIZE 64 +#define RT_UART_RX_BUFFER_SIZE 64 enum { - /* modbus slave 485 receive and transmit control pin index */ - MODBUS_SLAVE_RT_CONTROL_PIN_INDEX = 0, - /* modbus master 485 receive and transmit control pin index */ - MODBUS_MASTER_RT_CONTROL_PIN_INDEX = 1, + /* modbus slave 485 receive and transmit control pin index */ + MODBUS_SLAVE_RT_CONTROL_PIN_INDEX = 0, + /* modbus master 485 receive and transmit control pin index */ + MODBUS_MASTER_RT_CONTROL_PIN_INDEX = 1, }; -#define LED_LED1_ON GPIO_SetBits (GPIOA,GPIO_Pin_11) //LED1 -#define LED_LED1_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_11) //LED1 +#define LED_LED1_ON GPIO_SetBits (GPIOA,GPIO_Pin_11) //LED1 +#define LED_LED1_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_11) //LED1 -#define LED_LED2_ON GPIO_SetBits (GPIOA,GPIO_Pin_12) //LED1 -#define LED_LED2_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_12) //LED2 +#define LED_LED2_ON GPIO_SetBits (GPIOA,GPIO_Pin_12) //LED1 +#define LED_LED2_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_12) //LED2 void rt_hw_board_init(void); @@ -74,18 +74,18 @@ void IWDG_Feed(void); /*********************************************************************************************************/ -/** MACRO'S */ +/** MACRO'S */ /***********************************************************************************************************/ -//Ӳ汾Ŷ -#define VERSION_SOFTWARE_MAJOR 1 -#define VERSION_SOFTWARE_MINOR 0 -#define VERSION_HARDWARE_MAJOR 1 -#define VERSION_HARDWARE_MINOR 0 +//软、硬件版本号定义 +#define VERSION_SOFTWARE_MAJOR 1 +#define VERSION_SOFTWARE_MINOR 0 +#define VERSION_HARDWARE_MAJOR 1 +#define VERSION_HARDWARE_MINOR 0 /***********************************************************************************************************/ -/* DATA TYPES */ +/* DATA TYPES */ /***********************************************************************************************************/ diff --git a/BSP/src/bsp.c b/BSP/src/bsp.c index 9d426f6..221af3e 100644 --- a/BSP/src/bsp.c +++ b/BSP/src/bsp.c @@ -46,9 +46,9 @@ /** This function will initial STM32 board**/ void rt_hw_board_init() { - BSP_Init(); - stm32_hw_usart_init(); - stm32_hw_pin_init(); + BSP_Init(); + stm32_hw_usart_init(); + stm32_hw_pin_init(); } /******************************************************************************* @@ -60,20 +60,20 @@ void rt_hw_board_init() *******************************************************************************/ static void RCC_Configuration(void) { - //Ǹģ鿪ʱ - //GPIO + //下面是给各模块开启时钟 + //启动GPIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \ RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | \ - RCC_APB2Periph_GPIOE , + RCC_APB2Periph_GPIOE , ENABLE); - //AFIO + //启动AFIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); - //ADCתʱ - RCC_ADCCLKConfig(RCC_PCLK2_Div8); //9M - //DMAʱ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//ʹDMAʱ - /* Enable ADC1 and GPIOC clock */ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE); + //配置ADC转换时钟 + RCC_ADCCLKConfig(RCC_PCLK2_Div8); //9M + //启动DMA时钟 + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA时钟 + /* Enable ADC1 and GPIOC clock */ + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE); } /******************************************************************************* @@ -90,7 +90,7 @@ static void NVIC_Configuration(void) #ifdef VECT_TAB_RAM // Set the Vector Table base location at 0x20000000 NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); -#else // VECT_TAB_FLASH +#else // VECT_TAB_FLASH // Set the Vector Table base location at 0x08000000 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif @@ -104,55 +104,55 @@ static void NVIC_Configuration(void) *******************************************************************************/ static void GPIO_Configuration(void) { - GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitTypeDef GPIO_InitStructure; - /***************IOʼ*********************/ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + /***************数字输出IO初始化*********************/ + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12; //̵1 LED1 LED2 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12; //继电器1 LED1 LED2 GPIO_Init(GPIOA, &GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_14 | GPIO_Pin_15; // ̵3 ̵2 - GPIO_Init(GPIOB, &GPIO_InitStructure); + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_14 | GPIO_Pin_15; //蜂鸣器 继电器3 继电器2 + GPIO_Init(GPIOB, &GPIO_InitStructure); - /*************IOʼ*********************/ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + /*************数字输入IO初始化*********************/ + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_6; - GPIO_Init(GPIOG, &GPIO_InitStructure); + GPIO_Init(GPIOG, &GPIO_InitStructure); } - //*******************ʼŹ************************************* -//: void IWDG_Configuration(void) -// ʼŹ -//ڲ -//ڲ -// עƵ=4*2^prer.ֵֻ256!ʱ():Tout=40K/((4*2^prer)*rlr)ֵ 2Sʱ -//Editorliuqh 2013-1-16 Company: BXXJS + //*******************初始化独立看门狗************************************* +//函数定义: void IWDG_Configuration(void) +//描 述:初始化独立看门狗 +//入口参数:无 +//出口参数:无 +//备 注:分频因子=4*2^prer.但最大值只能是256!时间计算(大概):Tout=40K/((4*2^prer)*rlr)值 2S超时 +//Editor:liuqh 2013-1-16 Company: BXXJS //******************************************************************* -static void IWDG_Configuration(void) +static void IWDG_Configuration(void) { - IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//ʹܶIWDG->PRIWDG->RLRд - IWDG_SetPrescaler(IWDG_Prescaler_64);//64Ƶ - IWDG_SetReload(1300); - IWDG_ReloadCounter(); - IWDG_Enable(); + IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//使能对IWDG->PR和IWDG->RLR的写 + IWDG_SetPrescaler(IWDG_Prescaler_64);//64分频 + IWDG_SetReload(1300); + IWDG_ReloadCounter(); + IWDG_Enable(); } -//*******************ιŹ************************************* -//: void IWDG_Feed(void) -// ʼŹ -//ڲ -//ڲprer:Ƶ:0~7(ֻе3λЧ!)rlr:װؼĴֵ:11λЧ. -// עƵ=4*2^prer.ֵֻ256!ʱ():Tout=40K/((4*2^prer)*rlr)ֵ -//Editorliuqh 2013-1-16 Company: BXXJS +//*******************喂独立看门狗************************************* +//函数定义: void IWDG_Feed(void) +//描 述:初始化独立看门狗 +//入口参数:无 +//出口参数:prer:分频数:0~7(只有低3位有效!),rlr:重装载寄存器值:低11位有效. +//备 注:分频因子=4*2^prer.但最大值只能是256!时间计算(大概):Tout=40K/((4*2^prer)*rlr)值 +//Editor:liuqh 2013-1-16 Company: BXXJS //******************************************************************* void IWDG_Feed(void) { - IWDG_ReloadCounter();//reload + IWDG_ReloadCounter();//reload } @@ -165,15 +165,15 @@ void IWDG_Feed(void) *******************************************************************************/ void SysTick_Configuration(void) { - RCC_ClocksTypeDef rcc_clocks; - rt_uint32_t cnts; + RCC_ClocksTypeDef rcc_clocks; + rt_uint32_t cnts; - RCC_GetClocksFreq(&rcc_clocks); + RCC_GetClocksFreq(&rcc_clocks); - cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND; + cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND; - SysTick_Config(cnts); - SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); + SysTick_Config(cnts); + SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); } /** * This is the timer interrupt service routine. @@ -181,13 +181,13 @@ void SysTick_Configuration(void) */ void rt_hw_timer_handler(void) { - /* enter interrupt */ - rt_interrupt_enter(); + /* enter interrupt */ + rt_interrupt_enter(); - rt_tick_increase(); + rt_tick_increase(); - /* leave interrupt */ - rt_interrupt_leave(); + /* leave interrupt */ + rt_interrupt_leave(); } /* @@ -215,46 +215,46 @@ void rt_hw_timer_handler(void) void BSP_Init (void) { - RCC_Configuration(); - NVIC_Configuration(); - SysTick_Configuration(); - GPIO_Configuration(); -// TODO ԣʱעͿŹʽʱҪ -// IWDG_Configuration(); + RCC_Configuration(); + NVIC_Configuration(); + SysTick_Configuration(); + GPIO_Configuration(); +// TODO 方便调试,暂时注释看门狗,正式发布时需要打开 +// IWDG_Configuration(); } -//****************************ʱ******************************** -//: uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)()) -// TimeOfTimeoutʱڣÿPeriodʱһDetectCondition()صֵǷЧ -//ڲTimeOfTimeoutʱʱ䣨λsystick -// Period ÿPeriodʱһΣʱӣλsystick -// (*DetectCondition)()ConditionValue㣬ʱPeriodʱ -// ConditionValue ֵ -//ڲ0TimeOfTimeoutʱڣ⵽ -// 1TimeOfTimeoutʱڣûм⵽ -// עEditorArmink 2012-03-09 Company: BXXJS +//****************************防超时程序******************************** +//函数定义: uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)()) +//描 述:在TimeOfTimeout时间内,每Period时间检测一次DetectCondition()返回的值是否有效 +//入口参数:TimeOfTimeout:防超时总时间(单位:systick) +// Period :每Period时间检测一次,即时间因子(单位:systick) +// (*DetectCondition)():检测条件,等于ConditionValue则条件满足,检测结束,否则延时Period时间继续检测 +// ConditionValue ;条件成立的值 +//出口参数:0:在TimeOfTimeout时间内,检测到条件成立 +// 1:在TimeOfTimeout时间内,没有检测到条件成立 +//备 注:Editor:Armink 2012-03-09 Company: BXXJS //********************************************************************** uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)(),uint8_t ConditionValue) { - uint32_t LastTimeLocal, CurTimeLocal; - uint8_t ConditionValueLocal; - LastTimeLocal = rt_tick_get(); - CurTimeLocal = LastTimeLocal; - while(CurTimeLocal - LastTimeLocal < TimeOfTimeout) - { - CurTimeLocal = rt_tick_get(); - ConditionValueLocal = DetectCondition(); - if (ConditionValueLocal == ConditionValue) return 0; - rt_thread_delay(Period); - } - return 1; -} + uint32_t LastTimeLocal, CurTimeLocal; + uint8_t ConditionValueLocal; + LastTimeLocal = rt_tick_get(); + CurTimeLocal = LastTimeLocal; + while(CurTimeLocal - LastTimeLocal < TimeOfTimeout) + { + CurTimeLocal = rt_tick_get(); + ConditionValueLocal = DetectCondition(); + if (ConditionValueLocal == ConditionValue) return 0; + rt_thread_delay(Period); + } + return 1; +} -//************************************ʱ************************************** -//: void Delay(vu32 nCount) -//ڲnCount ʱУѭĴ -//ڲ -// עEditorArmink 2011-03-18 Company: BXXJS +//************************************延时函数************************************** +//函数定义: void Delay(vu32 nCount) +//入口参数:nCount :延时函数中,循环的次数 +//出口参数:无 +//备 注:Editor:Armink 2011-03-18 Company: BXXJS //********************************************************************************** void Delay(vu32 nCount) { diff --git a/BSP/src/usart.c b/BSP/src/usart.c index fa0c382..666d74c 100644 --- a/BSP/src/usart.c +++ b/BSP/src/usart.c @@ -1,7 +1,7 @@ /* * File : usart.c * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006-2013, RT-Thread Development Team + * COPYRIGHT (C) 2006-2021, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at diff --git a/FreeModbus/modbus/ascii/mbascii.c b/FreeModbus/modbus/ascii/mbascii.c index 9fab0f9..5ed4a26 100644 --- a/FreeModbus/modbus/ascii/mbascii.c +++ b/FreeModbus/modbus/ascii/mbascii.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -109,7 +109,7 @@ eMBASCIIInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eP { eMBErrorCode eStatus = MB_ENOERR; ( void )ucSlaveAddress; - + ENTER_CRITICAL_SECTION( ); ucMBLFCharacter = MB_ASCII_DEFAULT_LF; diff --git a/FreeModbus/modbus/ascii/mbascii.h b/FreeModbus/modbus/ascii/mbascii.h index 227f50d..d4607a9 100644 --- a/FreeModbus/modbus/ascii/mbascii.h +++ b/FreeModbus/modbus/ascii/mbascii.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/functions/mbfunccoils.c b/FreeModbus/modbus/functions/mbfunccoils.c index c9af92f..6aac666 100644 --- a/FreeModbus/modbus/functions/mbfunccoils.c +++ b/FreeModbus/modbus/functions/mbfunccoils.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -86,7 +86,7 @@ eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ) usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] ); /* Check if the number of registers to read is valid. If not - * return Modbus illegal data value exception. + * return Modbus illegal data value exception. */ if( ( usCoilCount >= 1 ) && ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) ) @@ -124,7 +124,7 @@ eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ) else { /* The response contains the function code, the starting address - * and the quantity of registers. We reuse the old values in the + * and the quantity of registers. We reuse the old values in the * buffer because they are still valid. */ *usLen += ucNBytes;; } @@ -247,7 +247,7 @@ eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen ) else { /* The response contains the function code, the starting address - * and the quantity of registers. We reuse the old values in the + * and the quantity of registers. We reuse the old values in the * buffer because they are still valid. */ *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF; } diff --git a/FreeModbus/modbus/functions/mbfunccoils_m.c b/FreeModbus/modbus/functions/mbfunccoils_m.c index 93801de..4366b49 100644 --- a/FreeModbus/modbus/functions/mbfunccoils_m.c +++ b/FreeModbus/modbus/functions/mbfunccoils_m.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (C) 2013 Armink * All rights reserved. @@ -94,16 +94,16 @@ eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils ,LONG else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_COILS; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usCoilAddr >> 8; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usCoilAddr; - ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8; - ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_COILS; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usCoilAddr >> 8; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usCoilAddr; + ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8; + ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; @@ -123,11 +123,11 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ) /* If this request is broadcast, and it's read mode. This request don't need execute. */ if ( xMBMasterRequestIsBroadcast() ) { - eStatus = MB_EX_NONE; + eStatus = MB_EX_NONE; } else if ( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress++; @@ -139,20 +139,20 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ) * byte is only partially field with unused coils set to zero. */ if( ( usCoilCount & 0x0007 ) != 0 ) { - ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 ); + ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 ); } else { - ucByteCount = ( UCHAR )( usCoilCount / 8 ); + ucByteCount = ( UCHAR )( usCoilCount / 8 ); } /* Check if the number of registers to read is valid. If not - * return Modbus illegal data value exception. + * return Modbus illegal data value exception. */ if( ( usCoilCount >= 1 ) && ( ucByteCount == pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] ) ) { - /* Make callback to fill the buffer. */ + /* Make callback to fill the buffer. */ eRegStatus = eMBMasterRegCoilsCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usCoilCount, MB_REG_READ ); /* If an error occured convert it into a Modbus exception. */ @@ -201,16 +201,16 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_SINGLE_COIL; - ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usCoilAddr >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usCoilAddr; - ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_SINGLE_COIL; + ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usCoilAddr >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usCoilAddr; + ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -285,7 +285,7 @@ eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen ) */ eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr, - USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut) + USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut) { UCHAR *ucMBFrame; USHORT usRegIndex = 0; @@ -297,30 +297,30 @@ eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr, else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_COILS; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usCoilAddr >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usCoilAddr; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF] = usNCoils >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF + 1] = usNCoils ; - if( ( usNCoils & 0x0007 ) != 0 ) + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_COILS; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usCoilAddr >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usCoilAddr; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF] = usNCoils >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF + 1] = usNCoils ; + if( ( usNCoils & 0x0007 ) != 0 ) { - ucByteCount = ( UCHAR )( usNCoils / 8 + 1 ); + ucByteCount = ( UCHAR )( usNCoils / 8 + 1 ); } else { - ucByteCount = ( UCHAR )( usNCoils / 8 ); + ucByteCount = ( UCHAR )( usNCoils / 8 ); } - ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = ucByteCount; - ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; - while( ucByteCount > usRegIndex) - { - *ucMBFrame++ = pucDataBuffer[usRegIndex++]; - } - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = ucByteCount; + ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; + while( ucByteCount > usRegIndex) + { + *ucMBFrame++ = pucDataBuffer[usRegIndex++]; + } + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -340,7 +340,7 @@ eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen ) /* If this request is broadcast, the *usLen is not need check. */ if( ( *usLen == MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] ); usRegAddress++; diff --git a/FreeModbus/modbus/functions/mbfuncdiag.c b/FreeModbus/modbus/functions/mbfuncdiag.c index 43d44e3..20be3fc 100644 --- a/FreeModbus/modbus/functions/mbfuncdiag.c +++ b/FreeModbus/modbus/functions/mbfuncdiag.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/functions/mbfuncdisc.c b/FreeModbus/modbus/functions/mbfuncdisc.c index d823025..faa0524 100644 --- a/FreeModbus/modbus/functions/mbfuncdisc.c +++ b/FreeModbus/modbus/functions/mbfuncdisc.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -74,7 +74,7 @@ eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ) usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] ); /* Check if the number of registers to read is valid. If not - * return Modbus illegal data value exception. + * return Modbus illegal data value exception. */ if( ( usDiscreteCnt >= 1 ) && ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) ) @@ -111,7 +111,7 @@ eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ) else { /* The response contains the function code, the starting address - * and the quantity of registers. We reuse the old values in the + * and the quantity of registers. We reuse the old values in the * buffer because they are still valid. */ *usLen += ucNBytes;; } diff --git a/FreeModbus/modbus/functions/mbfuncdisc_m.c b/FreeModbus/modbus/functions/mbfuncdisc_m.c index 6c8d6c4..ca666bb 100644 --- a/FreeModbus/modbus/functions/mbfuncdisc_m.c +++ b/FreeModbus/modbus/functions/mbfuncdisc_m.c @@ -79,16 +79,16 @@ eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT u else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr; - ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8; - ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr; + ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8; + ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -107,11 +107,11 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ) /* If this request is broadcast, and it's read mode. This request don't need execute. */ if ( xMBMasterRequestIsBroadcast() ) { - eStatus = MB_EX_NONE; + eStatus = MB_EX_NONE; } else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress++; @@ -123,26 +123,26 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ) * byte is only partially field with unused coils set to zero. */ if( ( usDiscreteCnt & 0x0007 ) != 0 ) { - ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 ); + ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 ); } else { - ucNBytes = ( UCHAR )( usDiscreteCnt / 8 ); + ucNBytes = ( UCHAR )( usDiscreteCnt / 8 ); } /* Check if the number of registers to read is valid. If not - * return Modbus illegal data value exception. + * return Modbus illegal data value exception. */ - if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF]) + if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF]) { - /* Make callback to fill the buffer. */ - eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt ); + /* Make callback to fill the buffer. */ + eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt ); - /* If an error occured convert it into a Modbus exception. */ - if( eRegStatus != MB_ENOERR ) - { - eStatus = prveMBError2Exception( eRegStatus ); - } + /* If an error occured convert it into a Modbus exception. */ + if( eRegStatus != MB_ENOERR ) + { + eStatus = prveMBError2Exception( eRegStatus ); + } } else { diff --git a/FreeModbus/modbus/functions/mbfuncholding.c b/FreeModbus/modbus/functions/mbfuncholding.c index ed8ad32..717a914 100644 --- a/FreeModbus/modbus/functions/mbfuncholding.c +++ b/FreeModbus/modbus/functions/mbfuncholding.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -186,7 +186,7 @@ eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); /* Check if the number of registers to read is valid. If not - * return Modbus illegal data value exception. + * return Modbus illegal data value exception. */ if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) ) { diff --git a/FreeModbus/modbus/functions/mbfuncholding_m.c b/FreeModbus/modbus/functions/mbfuncholding_m.c index e1fe69e..999c9a1 100644 --- a/FreeModbus/modbus/functions/mbfuncholding_m.c +++ b/FreeModbus/modbus/functions/mbfuncholding_m.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (C) 2013 Armink * All rights reserved. @@ -106,16 +106,16 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRe else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER; - ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER; + ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -167,7 +167,7 @@ eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) */ eMBMasterReqErrCode eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, - USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut ) + USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut ) { UCHAR *ucMBFrame; USHORT usRegIndex = 0; @@ -177,23 +177,23 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ; - ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2; - ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; - while( usNRegs > usRegIndex) - { - *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; - *ucMBFrame++ = pusDataBuffer[usRegIndex++] ; - } - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2; + ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; + while( usNRegs > usRegIndex) + { + *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; + *ucMBFrame++ = pusDataBuffer[usRegIndex++] ; + } + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -212,7 +212,7 @@ eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) /* If this request is broadcast, the *usLen is not need check. */ if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] ); usRegAddress++; @@ -271,16 +271,16 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; - ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; + ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -298,11 +298,11 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) /* If this request is broadcast, and it's read mode. This request don't need execute. */ if ( xMBMasterRequestIsBroadcast() ) { - eStatus = MB_EX_NONE; + eStatus = MB_EX_NONE; } else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress++; @@ -355,8 +355,8 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) */ eMBMasterReqErrCode eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr, - USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer, - USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut ) + USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer, + USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut ) { UCHAR *ucMBFrame; USHORT usRegIndex = 0; @@ -366,27 +366,27 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr, else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS; - ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8; - ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr; - ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8; - ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ; - ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8; - ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr; - ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8; - ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ; - ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2; - ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF; - while( usNWriteRegs > usRegIndex) - { - *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; - *ucMBFrame++ = pusDataBuffer[usRegIndex++] ; - } - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2; + ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF; + while( usNWriteRegs > usRegIndex) + { + *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; + *ucMBFrame++ = pusDataBuffer[usRegIndex++] ; + } + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -406,11 +406,11 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen /* If this request is broadcast, and it's read mode. This request don't need execute. */ if ( xMBMasterRequestIsBroadcast() ) { - eStatus = MB_EX_NONE; + eStatus = MB_EX_NONE; } else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U ); usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] ); usRegReadAddress++; @@ -434,8 +434,8 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen if( eRegStatus == MB_ENOERR ) { /* Make the read callback. */ - eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF], - usRegReadAddress, usRegReadCount, MB_REG_READ); + eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF], + usRegReadAddress, usRegReadCount, MB_REG_READ); } if( eRegStatus != MB_ENOERR ) { diff --git a/FreeModbus/modbus/functions/mbfuncinput.c b/FreeModbus/modbus/functions/mbfuncinput.c index 63d43eb..9260550 100644 --- a/FreeModbus/modbus/functions/mbfuncinput.c +++ b/FreeModbus/modbus/functions/mbfuncinput.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -75,7 +75,7 @@ eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen ) usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); /* Check if the number of registers to read is valid. If not - * return Modbus illegal data value exception. + * return Modbus illegal data value exception. */ if( ( usRegCount >= 1 ) && ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) ) diff --git a/FreeModbus/modbus/functions/mbfuncinput_m.c b/FreeModbus/modbus/functions/mbfuncinput_m.c index 2127efe..7e6cb19 100644 --- a/FreeModbus/modbus/functions/mbfuncinput_m.c +++ b/FreeModbus/modbus/functions/mbfuncinput_m.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (C) 2013 Armink * All rights reserved. @@ -79,16 +79,16 @@ eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else { - vMBMasterGetPDUSndBuf(&ucMBFrame); - vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; - ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); - ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); - eErrStatus = eMBMasterWaitRequestFinish( ); + vMBMasterGetPDUSndBuf(&ucMBFrame); + vMBMasterSetDestAddress(ucSndAddr); + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; + ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); + eErrStatus = eMBMasterWaitRequestFinish( ); } return eErrStatus; } @@ -104,13 +104,13 @@ eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen ) eMBErrorCode eRegStatus; /* If this request is broadcast, and it's read mode. This request don't need execute. */ - if ( xMBMasterRequestIsBroadcast() ) - { - eStatus = MB_EX_NONE; - } - else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) + if ( xMBMasterRequestIsBroadcast() ) { - vMBMasterGetPDUSndBuf(&ucMBFrame); + eStatus = MB_EX_NONE; + } + else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) + { + vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress++; diff --git a/FreeModbus/modbus/functions/mbfuncother.c b/FreeModbus/modbus/functions/mbfuncother.c index bf5e64e..6161894 100644 --- a/FreeModbus/modbus/functions/mbfuncother.c +++ b/FreeModbus/modbus/functions/mbfuncother.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/functions/mbutils.c b/FreeModbus/modbus/functions/mbutils.c index 64f8db1..ac0eb34 100644 --- a/FreeModbus/modbus/functions/mbutils.c +++ b/FreeModbus/modbus/functions/mbutils.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/include/mb.h b/FreeModbus/modbus/include/mb.h index fb4e28b..f451d65 100644 --- a/FreeModbus/modbus/include/mb.h +++ b/FreeModbus/modbus/include/mb.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -71,7 +71,7 @@ PR_BEGIN_EXTERN_C /*! \ingroup modbus * \brief Use the default Modbus TCP port (502) */ -#define MB_TCP_PORT_USE_DEFAULT 0 +#define MB_TCP_PORT_USE_DEFAULT 0 /* ----------------------- Type definitions ---------------------------------*/ @@ -97,7 +97,7 @@ PR_BEGIN_EXTERN_C * registers should be updated and reading means that the modbus protocol * stack needs to know the current register values. * - * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and + * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and * eMBRegInputCB( ). */ typedef enum @@ -142,7 +142,7 @@ typedef enum * * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR. * The protocol is then in the disabled state and ready for activation - * by calling eMBEnable( ). Otherwise one of the following error codes + * by calling eMBEnable( ). Otherwise one of the following error codes * is returned: * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid * slave addresses are in the range 1 - 247. @@ -171,10 +171,10 @@ eMBErrorCode eMBTCPInit( USHORT usTCPPort ); * \brief Release resources used by the protocol stack. * * This function disables the Modbus protocol stack and release all - * hardware resources. It must only be called when the protocol stack - * is disabled. + * hardware resources. It must only be called when the protocol stack + * is disabled. * - * \note Note all ports implement this function. A port which wants to + * \note Note all ports implement this function. A port which wants to * get an callback must define the macro MB_PORT_HAS_CLOSE to 1. * * \return If the resources where released it return eMBErrorCode::MB_ENOERR. @@ -189,8 +189,8 @@ eMBErrorCode eMBClose( void ); * This function enables processing of Modbus frames. Enabling the protocol * stack is only possible if it is in the disabled state. * - * \return If the protocol stack is now in the state enabled it returns - * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it + * \return If the protocol stack is now in the state enabled it returns + * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it * return eMBErrorCode::MB_EILLSTATE. */ eMBErrorCode eMBEnable( void ); @@ -200,7 +200,7 @@ eMBErrorCode eMBEnable( void ); * * This function disables processing of Modbus frames. * - * \return If the protocol stack has been disabled it returns + * \return If the protocol stack has been disabled it returns * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns * eMBErrorCode::MB_EILLSTATE. */ @@ -212,10 +212,10 @@ eMBErrorCode eMBDisable( void ); * This function must be called periodically. The timer interval required * is given by the application dependent Modbus slave timeout. Internally the * function calls xMBPortEventGet() and waits for an event from the receiver or - * transmitter state machines. + * transmitter state machines. * * \return If the protocol stack is not in the enabled state the function - * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns + * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns * eMBErrorCode::MB_ENOERR. */ eMBErrorCode eMBPoll( void ); @@ -249,7 +249,7 @@ eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning, * The callback handler supplied is responsible for interpreting the Modbus PDU and * the creation of an appropriate response. In case of an error it should return * one of the possible Modbus exceptions which results in a Modbus exception frame - * sent by the protocol stack. + * sent by the protocol stack. * * \param ucFunctionCode The Modbus function code for which this handler should * be registers. Valid function codes are in the range 1 to 127. @@ -262,7 +262,7 @@ eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning, * case the values in mbconfig.h should be adjusted. If the argument was not * valid it returns eMBErrorCode::MB_EINVAL. */ -eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode, +eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler ); /* ----------------------- Callback -----------------------------------------*/ @@ -300,7 +300,7 @@ eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode, * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal * Modbus response is sent. * - eMBErrorCode::MB_ENOREG If the application can not supply values - * for registers within this range. In this case a + * for registers within this range. In this case a * ILLEGAL DATA ADDRESS exception frame is sent as a response. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * currently not available and the application dependent response @@ -324,18 +324,18 @@ eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, * this buffer. * \param usAddress The starting address of the register. * \param usNRegs Number of registers to read or write. - * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register + * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register * values should be updated from the values in the buffer. For example - * this would be the case when the Modbus master has issued an + * this would be the case when the Modbus master has issued an * WRITE SINGLE REGISTER command. - * If the value eMBRegisterMode::MB_REG_READ the application should copy + * If the value eMBRegisterMode::MB_REG_READ the application should copy * the current values into the buffer \c pucRegBuffer. * * \return The function must return one of the following error codes: * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal * Modbus response is sent. * - eMBErrorCode::MB_ENOREG If the application can not supply values - * for registers within this range. In this case a + * for registers within this range. In this case a * ILLEGAL DATA ADDRESS exception frame is sent as a response. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * currently not available and the application dependent response @@ -370,7 +370,7 @@ eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal * Modbus response is sent. * - eMBErrorCode::MB_ENOREG If the application does not map an coils - * within the requested address range. In this case a + * within the requested address range. In this case a * ILLEGAL DATA ADDRESS is sent as a response. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * currently not available and the application dependent response @@ -399,7 +399,7 @@ eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal * Modbus response is sent. * - eMBErrorCode::MB_ENOREG If no such discrete inputs exists. - * In this case a ILLEGAL DATA ADDRESS exception frame is sent + * In this case a ILLEGAL DATA ADDRESS exception frame is sent * as a response. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * currently not available and the application dependent response diff --git a/FreeModbus/modbus/include/mb_m.h b/FreeModbus/modbus/include/mb_m.h index 6ae8878..0806eed 100644 --- a/FreeModbus/modbus/include/mb_m.h +++ b/FreeModbus/modbus/include/mb_m.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (C) 2013 Armink * All rights reserved. @@ -87,9 +87,9 @@ typedef enum */ typedef enum { - MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */ - MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */ - MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/ + MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */ + MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */ + MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/ }eMBMasterTimerMode; /* ----------------------- Function prototypes ------------------------------*/ @@ -115,7 +115,7 @@ typedef enum * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error. */ eMBErrorCode eMBMasterInit( eMBMode eMode, UCHAR ucPort, - ULONG ulBaudRate, eMBParity eParity ); + ULONG ulBaudRate, eMBParity eParity ); /*! \ingroup modbus * \brief Initialize the Modbus Master protocol stack for Modbus TCP. @@ -137,10 +137,10 @@ eMBErrorCode eMBMasterTCPInit( USHORT usTCPPort ); * \brief Release resources used by the protocol stack. * * This function disables the Modbus Master protocol stack and release all - * hardware resources. It must only be called when the protocol stack - * is disabled. + * hardware resources. It must only be called when the protocol stack + * is disabled. * - * \note Note all ports implement this function. A port which wants to + * \note Note all ports implement this function. A port which wants to * get an callback must define the macro MB_PORT_HAS_CLOSE to 1. * * \return If the resources where released it return eMBErrorCode::MB_ENOERR. @@ -155,8 +155,8 @@ eMBErrorCode eMBMasterClose( void ); * This function enables processing of Modbus Master frames. Enabling the protocol * stack is only possible if it is in the disabled state. * - * \return If the protocol stack is now in the state enabled it returns - * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it + * \return If the protocol stack is now in the state enabled it returns + * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it * return eMBErrorCode::MB_EILLSTATE. */ eMBErrorCode eMBMasterEnable( void ); @@ -166,7 +166,7 @@ eMBErrorCode eMBMasterEnable( void ); * * This function disables processing of Modbus frames. * - * \return If the protocol stack has been disabled it returns + * \return If the protocol stack has been disabled it returns * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns * eMBErrorCode::MB_EILLSTATE. */ @@ -190,10 +190,10 @@ BOOL eMBMasterIsEstablished( void ); * This function must be called periodically. The timer interval required * is given by the application dependent Modbus slave timeout. Internally the * function calls xMBMasterPortEventGet() and waits for an event from the receiver or - * transmitter state machines. + * transmitter state machines. * * \return If the protocol stack is not in the enabled state the function - * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns + * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns * eMBErrorCode::MB_ENOERR. */ eMBErrorCode eMBMasterPoll( void ); @@ -260,7 +260,7 @@ eMBErrorCode eMBMasterRegisterCB( UCHAR ucFunctionCode, * ILLEGAL DATA ADDRESS is sent as a response. */ eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, - USHORT usNRegs ); + USHORT usNRegs ); /*! \ingroup modbus_registers * \brief Callback function used if a Holding Register value is @@ -289,7 +289,7 @@ eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, * ILLEGAL DATA ADDRESS is sent as a response. */ eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, - USHORT usNRegs, eMBRegisterMode eMode ); + USHORT usNRegs, eMBRegisterMode eMode ); /*! \ingroup modbus_registers * \brief Callback function used if a Coil Register value is @@ -318,7 +318,7 @@ eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, * ILLEGAL DATA ADDRESS is sent as a response. */ eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, - USHORT usNCoils, eMBRegisterMode eMode ); + USHORT usNCoils, eMBRegisterMode eMode ); /*! \ingroup modbus_registers * \brief Callback function used if a Input Discrete Register value is @@ -341,7 +341,7 @@ eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, * ILLEGAL DATA ADDRESS is sent as a response. */ eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, - USHORT usNDiscrete ); + USHORT usNDiscrete ); /*! \ingroup modbus *\brief These Modbus functions are called for user when Modbus run in Master Mode. @@ -352,20 +352,20 @@ eMBMasterReqErrCode eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, - USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut ); + USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr, - USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer, - USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut ); + USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer, + USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr, - USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut ); + USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut ); eMBMasterReqErrCode eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut ); @@ -390,7 +390,7 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ); eMBException eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen ); -/* \ingroup modbus +/*! \ingroup modbus *\brief These functions are interface for Modbus Master */ void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame ); diff --git a/FreeModbus/modbus/include/mbconfig.h b/FreeModbus/modbus/include/mbconfig.h index f2ec4dc..4fdd24b 100644 --- a/FreeModbus/modbus/include/mbconfig.h +++ b/FreeModbus/modbus/include/mbconfig.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/include/mbframe.h b/FreeModbus/modbus/include/mbframe.h index 991e5bf..e20c573 100644 --- a/FreeModbus/modbus/include/mbframe.h +++ b/FreeModbus/modbus/include/mbframe.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/include/mbfunc.h b/FreeModbus/modbus/include/mbfunc.h index e607fd7..f252413 100644 --- a/FreeModbus/modbus/include/mbfunc.h +++ b/FreeModbus/modbus/include/mbfunc.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/include/mbport.h b/FreeModbus/modbus/include/mbport.h index 24a3905..dafbd3f 100644 --- a/FreeModbus/modbus/include/mbport.h +++ b/FreeModbus/modbus/include/mbport.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/include/mbproto.h b/FreeModbus/modbus/include/mbproto.h index 670384f..aef3198 100644 --- a/FreeModbus/modbus/include/mbproto.h +++ b/FreeModbus/modbus/include/mbproto.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/include/mbutils.h b/FreeModbus/modbus/include/mbutils.h index 6feff06..a504303 100644 --- a/FreeModbus/modbus/include/mbutils.h +++ b/FreeModbus/modbus/include/mbutils.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/mb.c b/FreeModbus/modbus/mb.c index 0d3ea8a..a2c5e81 100644 --- a/FreeModbus/modbus/mb.c +++ b/FreeModbus/modbus/mb.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index fa7f579..c624177 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (C) 2013 Armink * All rights reserved. @@ -102,7 +102,7 @@ BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void ); */ static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = { #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0 - //TODO Add Master function define + //TODO Add Master function define {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID}, #endif #if MB_FUNC_READ_INPUT_ENABLED > 0 @@ -140,56 +140,56 @@ eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity { eMBErrorCode eStatus = MB_ENOERR; - switch (eMode) - { + switch (eMode) + { #if MB_MASTER_RTU_ENABLED > 0 - case MB_RTU: - pvMBMasterFrameStartCur = eMBMasterRTUStart; - pvMBMasterFrameStopCur = eMBMasterRTUStop; - peMBMasterFrameSendCur = eMBMasterRTUSend; - peMBMasterFrameReceiveCur = eMBMasterRTUReceive; - pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL; - pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM; - pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM; - pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired; + case MB_RTU: + pvMBMasterFrameStartCur = eMBMasterRTUStart; + pvMBMasterFrameStopCur = eMBMasterRTUStop; + peMBMasterFrameSendCur = eMBMasterRTUSend; + peMBMasterFrameReceiveCur = eMBMasterRTUReceive; + pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL; + pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM; + pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM; + pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired; - eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity); - break; + eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity); + break; #endif #if MB_MASTER_ASCII_ENABLED > 0 - case MB_ASCII: - pvMBMasterFrameStartCur = eMBMasterASCIIStart; - pvMBMasterFrameStopCur = eMBMasterASCIIStop; - peMBMasterFrameSendCur = eMBMasterASCIISend; - peMBMasterFrameReceiveCur = eMBMasterASCIIReceive; - pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL; - pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM; - pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM; - pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired; + case MB_ASCII: + pvMBMasterFrameStartCur = eMBMasterASCIIStart; + pvMBMasterFrameStopCur = eMBMasterASCIIStop; + peMBMasterFrameSendCur = eMBMasterASCIISend; + peMBMasterFrameReceiveCur = eMBMasterASCIIReceive; + pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL; + pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM; + pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM; + pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired; - eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity ); - break; + eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity ); + break; #endif - default: - eStatus = MB_EINVAL; - break; - } + default: + eStatus = MB_EINVAL; + break; + } - if (eStatus == MB_ENOERR) - { - if (!xMBMasterPortEventInit()) - { - /* port dependent event module initalization failed. */ - eStatus = MB_EPORTERR; - } - else - { - eMBState = STATE_DISABLED; - } - /* initialize the OS resource for modbus master. */ - vMBMasterOsResInit(); - } - return eStatus; + if (eStatus == MB_ENOERR) + { + if (!xMBMasterPortEventInit()) + { + /* port dependent event module initalization failed. */ + eStatus = MB_EPORTERR; + } + else + { + eMBState = STATE_DISABLED; + } + /* initialize the OS resource for modbus master. */ + vMBMasterOsResInit(); + } + return eStatus; } eMBErrorCode @@ -314,74 +314,74 @@ eMBMasterPoll( void ) eException = MB_EX_ILLEGAL_FUNCTION; /* If receive frame has exception .The receive function code highest bit is 1.*/ if(ucFunctionCode >> 7) { - eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF]; + eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF]; + } + else + { + for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) + { + /* No more function handlers registered. Abort. */ + if (xMasterFuncHandlers[i].ucFunctionCode == 0) { + break; + } + else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) { + vMBMasterSetCBRunInMasterMode(TRUE); + /* If master request is broadcast, + * the master need execute function for all slave. + */ + if ( xMBMasterRequestIsBroadcast() ) { + usLength = usMBMasterGetPDUSndLength(); + for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){ + vMBMasterSetDestAddress(j); + eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); + } + } + else { + eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); + } + vMBMasterSetCBRunInMasterMode(FALSE); + break; + } + } } - else - { - for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) - { - /* No more function handlers registered. Abort. */ - if (xMasterFuncHandlers[i].ucFunctionCode == 0) { - break; - } - else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) { - vMBMasterSetCBRunInMasterMode(TRUE); - /* If master request is broadcast, - * the master need execute function for all slave. - */ - if ( xMBMasterRequestIsBroadcast() ) { - usLength = usMBMasterGetPDUSndLength(); - for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){ - vMBMasterSetDestAddress(j); - eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); - } - } - else { - eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); - } - vMBMasterSetCBRunInMasterMode(FALSE); - break; - } - } - } /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/ if (eException != MB_EX_NONE) { - vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION); - ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); + vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION); + ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); } else { - vMBMasterCBRequestScuuess( ); - vMBMasterRunResRelease( ); + vMBMasterCBRequestScuuess( ); + vMBMasterRunResRelease( ); } break; case EV_MASTER_FRAME_SENT: - /* Master is busy now. */ - vMBMasterGetPDUSndBuf( &ucMBFrame ); - eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() ); + /* Master is busy now. */ + vMBMasterGetPDUSndBuf( &ucMBFrame ); + eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() ); break; case EV_MASTER_ERROR_PROCESS: - /* Execute specified error process callback function. */ - errorType = eMBMasterGetErrorType(); - vMBMasterGetPDUSndBuf( &ucMBFrame ); - switch (errorType) { - case EV_ERROR_RESPOND_TIMEOUT: - vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(), - ucMBFrame, usMBMasterGetPDUSndLength()); - break; - case EV_ERROR_RECEIVE_DATA: - vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(), - ucMBFrame, usMBMasterGetPDUSndLength()); - break; - case EV_ERROR_EXECUTE_FUNCTION: - vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(), - ucMBFrame, usMBMasterGetPDUSndLength()); - break; - } - vMBMasterRunResRelease(); - break; - + /* Execute specified error process callback function. */ + errorType = eMBMasterGetErrorType(); + vMBMasterGetPDUSndBuf( &ucMBFrame ); + switch (errorType) { + case EV_ERROR_RESPOND_TIMEOUT: + vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(), + ucMBFrame, usMBMasterGetPDUSndLength()); + break; + case EV_ERROR_RECEIVE_DATA: + vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(), + ucMBFrame, usMBMasterGetPDUSndLength()); + break; + case EV_ERROR_EXECUTE_FUNCTION: + vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(), + ucMBFrame, usMBMasterGetPDUSndLength()); + break; + } + vMBMasterRunResRelease(); + break; + default: break; } @@ -393,32 +393,32 @@ eMBMasterPoll( void ) /* Get whether the Modbus Master is run in master mode.*/ BOOL xMBMasterGetCBRunInMasterMode( void ) { - return xMBRunInMasterMode; + return xMBRunInMasterMode; } /* Set whether the Modbus Master is run in master mode.*/ void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode ) { - xMBRunInMasterMode = IsMasterMode; + xMBRunInMasterMode = IsMasterMode; } /* Get Modbus Master send destination address. */ UCHAR ucMBMasterGetDestAddress( void ) { - return ucMBMasterDestAddress; + return ucMBMasterDestAddress; } /* Set Modbus Master send destination address. */ void vMBMasterSetDestAddress( UCHAR Address ) { - ucMBMasterDestAddress = Address; + ucMBMasterDestAddress = Address; } /* Get Modbus Master current error event type. */ eMBMasterErrorEventType eMBMasterGetErrorType( void ) { - return eMBMasterCurErrorType; + return eMBMasterCurErrorType; } /* Set Modbus Master current error event type. */ void vMBMasterSetErrorType( eMBMasterErrorEventType errorType ) { - eMBMasterCurErrorType = errorType; + eMBMasterCurErrorType = errorType; } diff --git a/FreeModbus/modbus/rtu/mbcrc.c b/FreeModbus/modbus/rtu/mbcrc.c index 465f721..5ed2a80 100644 --- a/FreeModbus/modbus/rtu/mbcrc.c +++ b/FreeModbus/modbus/rtu/mbcrc.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -43,12 +43,12 @@ static const UCHAR aucCRCHi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, @@ -64,12 +64,12 @@ static const UCHAR aucCRCLo[] = { 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, - 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, + 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, - 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, + 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, diff --git a/FreeModbus/modbus/rtu/mbcrc.h b/FreeModbus/modbus/rtu/mbcrc.h index d5d8aa3..92168c2 100644 --- a/FreeModbus/modbus/rtu/mbcrc.h +++ b/FreeModbus/modbus/rtu/mbcrc.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/rtu/mbrtu.c b/FreeModbus/modbus/rtu/mbrtu.c index 1fd60cd..14325a9 100644 --- a/FreeModbus/modbus/rtu/mbrtu.c +++ b/FreeModbus/modbus/rtu/mbrtu.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/modbus/rtu/mbrtu.h b/FreeModbus/modbus/rtu/mbrtu.h index a5e8c5e..da56acd 100644 --- a/FreeModbus/modbus/rtu/mbrtu.h +++ b/FreeModbus/modbus/rtu/mbrtu.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -37,7 +37,7 @@ PR_BEGIN_EXTERN_C #endif eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate, - eMBParity eParity ); + eMBParity eParity ); void eMBRTUStart( void ); void eMBRTUStop( void ); eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength ); diff --git a/FreeModbus/modbus/rtu/mbrtu_m.c b/FreeModbus/modbus/rtu/mbrtu_m.c index 3e96c27..b7719c8 100644 --- a/FreeModbus/modbus/rtu/mbrtu_m.c +++ b/FreeModbus/modbus/rtu/mbrtu_m.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2013 China Beijing Armink * All rights reserved. @@ -262,11 +262,11 @@ xMBMasterRTUReceiveFSM( void ) * the timer of respond timeout . */ case STATE_M_RX_IDLE: - /* In time of respond timeout,the receiver receive a frame. - * Disable timer of respond timeout and change the transmiter state to idle. - */ - vMBMasterPortTimersDisable( ); - eSndState = STATE_M_TX_IDLE; + /* In time of respond timeout,the receiver receive a frame. + * Disable timer of respond timeout and change the transmiter state to idle. + */ + vMBMasterPortTimersDisable( ); + eSndState = STATE_M_TX_IDLE; usMasterRcvBufferPos = 0; ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte; @@ -331,11 +331,11 @@ xMBMasterRTUTransmitFSM( void ) * else master will enable timer of respond timeout. */ if ( xFrameIsBroadcast == TRUE ) { - vMBMasterPortTimersConvertDelayEnable( ); + vMBMasterPortTimersConvertDelayEnable( ); } else { - vMBMasterPortTimersRespondTimeoutEnable( ); + vMBMasterPortTimersRespondTimeoutEnable( ); } } break; @@ -350,97 +350,97 @@ xMBMasterRTUTransmitFSM( void ) BOOL xMBMasterRTUTimerExpired(void) { - BOOL xNeedPoll = FALSE; + BOOL xNeedPoll = FALSE; - switch (eRcvState) - { - /* Timer t35 expired. Startup phase is finished. */ - case STATE_M_RX_INIT: - xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY); - break; + switch (eRcvState) + { + /* Timer t35 expired. Startup phase is finished. */ + case STATE_M_RX_INIT: + xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY); + break; - /* A frame was received and t35 expired. Notify the listener that - * a new frame was received. */ - case STATE_M_RX_RCV: - xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED); - break; + /* A frame was received and t35 expired. Notify the listener that + * a new frame was received. */ + case STATE_M_RX_RCV: + xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED); + break; - /* An error occured while receiving the frame. */ - case STATE_M_RX_ERROR: - vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); - xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); - break; + /* An error occured while receiving the frame. */ + case STATE_M_RX_ERROR: + vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); + xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); + break; - /* Function called in an illegal state. */ - default: - assert_param( - ( eRcvState == STATE_M_RX_INIT ) || ( eRcvState == STATE_M_RX_RCV ) || - ( eRcvState == STATE_M_RX_ERROR ) || ( eRcvState == STATE_M_RX_IDLE )); - break; - } - eRcvState = STATE_M_RX_IDLE; + /* Function called in an illegal state. */ + default: + assert_param( + ( eRcvState == STATE_M_RX_INIT ) || ( eRcvState == STATE_M_RX_RCV ) || + ( eRcvState == STATE_M_RX_ERROR ) || ( eRcvState == STATE_M_RX_IDLE )); + break; + } + eRcvState = STATE_M_RX_IDLE; - switch (eSndState) - { - /* A frame was send finish and convert delay or respond timeout expired. - * If the frame is broadcast,The master will idle,and if the frame is not - * broadcast.Notify the listener process error.*/ - case STATE_M_TX_XFWR: - if ( xFrameIsBroadcast == FALSE ) { - vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT); - xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS); - } - break; - /* Function called in an illegal state. */ - default: - assert_param( - ( eSndState == STATE_M_TX_XFWR ) || ( eSndState == STATE_M_TX_IDLE )); - break; - } - eSndState = STATE_M_TX_IDLE; + switch (eSndState) + { + /* A frame was send finish and convert delay or respond timeout expired. + * If the frame is broadcast,The master will idle,and if the frame is not + * broadcast.Notify the listener process error.*/ + case STATE_M_TX_XFWR: + if ( xFrameIsBroadcast == FALSE ) { + vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT); + xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS); + } + break; + /* Function called in an illegal state. */ + default: + assert_param( + ( eSndState == STATE_M_TX_XFWR ) || ( eSndState == STATE_M_TX_IDLE )); + break; + } + eSndState = STATE_M_TX_IDLE; - vMBMasterPortTimersDisable( ); - /* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */ - if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) { - xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE ); - } + vMBMasterPortTimersDisable( ); + /* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */ + if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) { + xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE ); + } - return xNeedPoll; + return xNeedPoll; } /* Get Modbus Master send RTU's buffer address pointer.*/ void vMBMasterGetRTUSndBuf( UCHAR ** pucFrame ) { - *pucFrame = ( UCHAR * ) ucMasterRTUSndBuf; + *pucFrame = ( UCHAR * ) ucMasterRTUSndBuf; } /* Get Modbus Master send PDU's buffer address pointer.*/ void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame ) { - *pucFrame = ( UCHAR * ) &ucMasterRTUSndBuf[MB_SER_PDU_PDU_OFF]; + *pucFrame = ( UCHAR * ) &ucMasterRTUSndBuf[MB_SER_PDU_PDU_OFF]; } /* Set Modbus Master send PDU's buffer length.*/ void vMBMasterSetPDUSndLength( USHORT SendPDULength ) { - usMasterSendPDULength = SendPDULength; + usMasterSendPDULength = SendPDULength; } /* Get Modbus Master send PDU's buffer length.*/ USHORT usMBMasterGetPDUSndLength( void ) { - return usMasterSendPDULength; + return usMasterSendPDULength; } /* Set Modbus Master current timer mode.*/ void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode ) { - eMasterCurTimerMode = eMBTimerMode; + eMasterCurTimerMode = eMBTimerMode; } /* The master request is broadcast? */ BOOL xMBMasterRequestIsBroadcast( void ){ - return xFrameIsBroadcast; + return xFrameIsBroadcast; } #endif diff --git a/FreeModbus/modbus/tcp/mbtcp.c b/FreeModbus/modbus/tcp/mbtcp.c index 1014cb4..e6ce040 100644 --- a/FreeModbus/modbus/tcp/mbtcp.c +++ b/FreeModbus/modbus/tcp/mbtcp.c @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. @@ -54,10 +54,10 @@ * +-----------+---------------+------------------------------------------+ * | TID | PID | Length | UID |Code | Data | * +-----------+---------------+------------------------------------------+ - * | | | | | - * (2) (3) (4) (5) (6) + * | | | | | + * (2) (3) (4) (5) (6) * - * (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte) + * (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte) * (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte) * (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte) * (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte) @@ -141,9 +141,9 @@ eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength ) USHORT usTCPLength = usLength + MB_TCP_FUNC; /* The MBAP header is already initialized because the caller calls this - * function with the buffer returned by the previous call. Therefore we - * only have to update the length in the header. Note that the length - * header includes the size of the Modbus PDU and the UID Byte. Therefore + * function with the buffer returned by the previous call. Therefore we + * only have to update the length in the header. Note that the length + * header includes the size of the Modbus PDU and the UID Byte. Therefore * the length is usLength plus one. */ pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U; diff --git a/FreeModbus/modbus/tcp/mbtcp.h b/FreeModbus/modbus/tcp/mbtcp.h index 353980c..7818b76 100644 --- a/FreeModbus/modbus/tcp/mbtcp.h +++ b/FreeModbus/modbus/tcp/mbtcp.h @@ -1,4 +1,4 @@ -/* +/* * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006-2018 Christian Walter * All rights reserved. diff --git a/FreeModbus/port/port.h b/FreeModbus/port/port.h index 9205611..73d2177 100644 --- a/FreeModbus/port/port.h +++ b/FreeModbus/port/port.h @@ -30,11 +30,11 @@ #include #include -#define INLINE +#define INLINE #define PR_BEGIN_EXTERN_C extern "C" { -#define PR_END_EXTERN_C } +#define PR_END_EXTERN_C } -#define ENTER_CRITICAL_SECTION() EnterCriticalSection() +#define ENTER_CRITICAL_SECTION() EnterCriticalSection() #define EXIT_CRITICAL_SECTION() ExitCriticalSection() typedef uint8_t BOOL; diff --git a/FreeModbus/port/rtt/portserial.c b/FreeModbus/port/rtt/portserial.c index 6f8e71d..135a86f 100644 --- a/FreeModbus/port/rtt/portserial.c +++ b/FreeModbus/port/rtt/portserial.c @@ -168,11 +168,11 @@ BOOL xMBPortSerialGetByte(CHAR * pucByte) return TRUE; } -/* +/* * Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that - * a new character can be sent. The protocol stack will then call + * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ void prvvUARTTxReadyISR(void) @@ -180,7 +180,7 @@ void prvvUARTTxReadyISR(void) pxMBFrameCBTransmitterEmpty(); } -/* +/* * Create an interrupt handler for the receive interrupt for your target * processor. This function should then call pxMBFrameCBByteReceived( ). The * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the diff --git a/FreeModbus/port/rtt/portserial_m.c b/FreeModbus/port/rtt/portserial_m.c index d19750c..f064b1c 100644 --- a/FreeModbus/port/rtt/portserial_m.c +++ b/FreeModbus/port/rtt/portserial_m.c @@ -169,11 +169,11 @@ BOOL xMBMasterPortSerialGetByte(CHAR * pucByte) return TRUE; } -/* +/* * Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that - * a new character can be sent. The protocol stack will then call + * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ void prvvUARTTxReadyISR(void) @@ -181,7 +181,7 @@ void prvvUARTTxReadyISR(void) pxMBMasterFrameCBTransmitterEmpty(); } -/* +/* * Create an interrupt handler for the receive interrupt for your target * processor. This function should then call pxMBFrameCBByteReceived( ). The * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the