Merge branch '2023_open_source_contest' of https://gitlink.org.cn/xuos/xiuos into workplace
| 
						 | 
					@ -29,3 +29,6 @@
 | 
				
			||||||
	path = APP_Framework/lib/lorawan/lorawan_devicenode
 | 
						path = APP_Framework/lib/lorawan/lorawan_devicenode
 | 
				
			||||||
	url = https://gitlink.org.cn/IACU/lorawan_devicenode.git
 | 
						url = https://gitlink.org.cn/IACU/lorawan_devicenode.git
 | 
				
			||||||
	branch = master
 | 
						branch = master
 | 
				
			||||||
 | 
					[submodule "APP_Framework/lib/lorawan/lorawan_gateway_single_channel"]
 | 
				
			||||||
 | 
						path = APP_Framework/lib/lorawan/lorawan_gateway_single_channel
 | 
				
			||||||
 | 
						url = https://gitlink.org.cn/IACU/lorawan_gateway_single_channel.git
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,13 +1,13 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS +=
 | 
					    CSRCS +=
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_DIR := general_functions app_test 
 | 
					    SRC_DIR := general_functions app_test 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    SRC_FILES := main.c
 | 
					    SRC_FILES := main.c
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,7 +8,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test adc"
 | 
					                bool "Config test adc"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_ADC
 | 
					                if USER_TEST_ADC
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config ADC_DEV_DRIVER
 | 
					                        config ADC_DEV_DRIVER
 | 
				
			||||||
                            string "Set ADC dev path"
 | 
					                            string "Set ADC dev path"
 | 
				
			||||||
                            default "/dev/adc1_dev"
 | 
					                            default "/dev/adc1_dev"
 | 
				
			||||||
| 
						 | 
					@ -19,7 +19,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test dac"
 | 
					                bool "Config test dac"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_DAC
 | 
					                if USER_TEST_DAC
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config DAC_DEV_DRIVER
 | 
					                        config DAC_DEV_DRIVER
 | 
				
			||||||
                            string "Set DAC dev path"
 | 
					                            string "Set DAC dev path"
 | 
				
			||||||
                            default "/dev/dac_dev"
 | 
					                            default "/dev/dac_dev"
 | 
				
			||||||
| 
						 | 
					@ -30,7 +30,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test fs with sd or usb"
 | 
					                bool "Config test fs with sd or usb"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_FS
 | 
					                if USER_TEST_FS
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config FPATH
 | 
					                        config FPATH
 | 
				
			||||||
                            string "Set test file path"
 | 
					                            string "Set test file path"
 | 
				
			||||||
                            default "/test_file"
 | 
					                            default "/test_file"
 | 
				
			||||||
| 
						 | 
					@ -46,7 +46,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test gpio with led and key"
 | 
					                bool "Config test gpio with led and key"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_GPIO
 | 
					                if USER_TEST_GPIO
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config GPIO_DEV_DRIVER
 | 
					                        config GPIO_DEV_DRIVER
 | 
				
			||||||
                            string "Set gpio dev path"
 | 
					                            string "Set gpio dev path"
 | 
				
			||||||
                            default "/dev/pin_dev"
 | 
					                            default "/dev/pin_dev"
 | 
				
			||||||
| 
						 | 
					@ -62,7 +62,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test uart(loraE220)"
 | 
					                bool "Config test uart(loraE220)"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_LORA
 | 
					                if USER_TEST_LORA
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config LORA_UART_DEV_DRIVER
 | 
					                        config LORA_UART_DEV_DRIVER
 | 
				
			||||||
                            string "Set uart dev path"
 | 
					                            string "Set uart dev path"
 | 
				
			||||||
                            default "/dev/uart2_dev2"
 | 
					                            default "/dev/uart2_dev2"
 | 
				
			||||||
| 
						 | 
					@ -80,7 +80,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test uart(RS485)"
 | 
					                bool "Config test uart(RS485)"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_RS485
 | 
					                if USER_TEST_RS485
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config RS485_UART_DEV_DRIVER
 | 
					                        config RS485_UART_DEV_DRIVER
 | 
				
			||||||
                            string "Set uart dev path"
 | 
					                            string "Set uart dev path"
 | 
				
			||||||
                            default "/dev/uart1_dev1"
 | 
					                            default "/dev/uart1_dev1"
 | 
				
			||||||
| 
						 | 
					@ -95,7 +95,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test rtc"
 | 
					                bool "Config test rtc"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_RTC
 | 
					                if USER_TEST_RTC
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config RTC_DEV_DRIVER
 | 
					                        config RTC_DEV_DRIVER
 | 
				
			||||||
                            string "Set rtc dev path"
 | 
					                            string "Set rtc dev path"
 | 
				
			||||||
                            default "/dev/rtc_dev"
 | 
					                            default "/dev/rtc_dev"
 | 
				
			||||||
| 
						 | 
					@ -110,7 +110,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test hwtimer"
 | 
					                bool "Config test hwtimer"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_HWTIMER
 | 
					                if USER_TEST_HWTIMER
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config HWTIMER_TIMER_DEV_DRIVER
 | 
					                        config HWTIMER_TIMER_DEV_DRIVER
 | 
				
			||||||
                            string "Set pin dev path"
 | 
					                            string "Set pin dev path"
 | 
				
			||||||
                            default "/dev/timer0_dev0"
 | 
					                            default "/dev/timer0_dev0"
 | 
				
			||||||
| 
						 | 
					@ -125,7 +125,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test watchdog"
 | 
					                bool "Config test watchdog"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_WDT
 | 
					                if USER_TEST_WDT
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config WDT0_DEV_DRIVER
 | 
					                        config WDT0_DEV_DRIVER
 | 
				
			||||||
                            string "Set wdt dev path"
 | 
					                            string "Set wdt dev path"
 | 
				
			||||||
                            default "/dev/wdt0_dev0"
 | 
					                            default "/dev/wdt0_dev0"
 | 
				
			||||||
| 
						 | 
					@ -137,7 +137,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test lcd in PrivOpen"
 | 
					                bool "Config test lcd in PrivOpen"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_LCD_EDU
 | 
					                if USER_TEST_LCD_EDU
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config EDU_LCD_DEV_DRIVER
 | 
					                        config EDU_LCD_DEV_DRIVER
 | 
				
			||||||
                            string "Set lcd dev path"
 | 
					                            string "Set lcd dev path"
 | 
				
			||||||
                            default "/dev/lcd_dev"
 | 
					                            default "/dev/lcd_dev"
 | 
				
			||||||
| 
						 | 
					@ -149,7 +149,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test touch"
 | 
					                bool "Config test touch"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_TOUCH
 | 
					                if USER_TEST_TOUCH
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config TOUCH_DEV_DRIVER
 | 
					                        config TOUCH_DEV_DRIVER
 | 
				
			||||||
                            string "Set touch dev path"
 | 
					                            string "Set touch dev path"
 | 
				
			||||||
                            default "/dev/touch_dev"
 | 
					                            default "/dev/touch_dev"
 | 
				
			||||||
| 
						 | 
					@ -164,7 +164,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test i2c"
 | 
					                bool "Config test i2c"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_I2C
 | 
					                if USER_TEST_I2C
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config I2C_DEV_DRIVER
 | 
					                        config I2C_DEV_DRIVER
 | 
				
			||||||
                            string "Set i2c dev path"
 | 
					                            string "Set i2c dev path"
 | 
				
			||||||
                            default "/dev/i2c1_dev0"
 | 
					                            default "/dev/i2c1_dev0"
 | 
				
			||||||
| 
						 | 
					@ -176,7 +176,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test can"
 | 
					                bool "Config test can"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_CAN
 | 
					                if USER_TEST_CAN
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config CAN_DEV_DRIVER
 | 
					                        config CAN_DEV_DRIVER
 | 
				
			||||||
                            string "Set can dev path"
 | 
					                            string "Set can dev path"
 | 
				
			||||||
                            default "/dev/can2_dev1"
 | 
					                            default "/dev/can2_dev1"
 | 
				
			||||||
| 
						 | 
					@ -189,7 +189,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test camera with lcd"
 | 
					                bool "Config test camera with lcd"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_CAMERA
 | 
					                if USER_TEST_CAMERA
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config CAMERA_DEV_DRIVER
 | 
					                        config CAMERA_DEV_DRIVER
 | 
				
			||||||
                            string "Set camera dev path"
 | 
					                            string "Set camera dev path"
 | 
				
			||||||
                            default "/dev/camera_dev"
 | 
					                            default "/dev/camera_dev"
 | 
				
			||||||
| 
						 | 
					@ -211,7 +211,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test ethernet only for edu-riscv64"
 | 
					                bool "Config test ethernet only for edu-riscv64"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_ETHERNET
 | 
					                if USER_TEST_ETHERNET
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        choice 
 | 
					                        choice 
 | 
				
			||||||
                            prompt "set ethernet role as client or server"
 | 
					                            prompt "set ethernet role as client or server"
 | 
				
			||||||
                            default ETHERNET_AS_SERVER
 | 
					                            default ETHERNET_AS_SERVER
 | 
				
			||||||
| 
						 | 
					@ -229,7 +229,7 @@ menu "test app"
 | 
				
			||||||
                bool "Config test w25q128 device"
 | 
					                bool "Config test w25q128 device"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
                if USER_TEST_FLASH
 | 
					                if USER_TEST_FLASH
 | 
				
			||||||
                    if ADD_XIZI_FETURES
 | 
					                    if ADD_XIZI_FEATURES
 | 
				
			||||||
                        config FLASH_DEV_DRIVER
 | 
					                        config FLASH_DEV_DRIVER
 | 
				
			||||||
                            string "Set flash dev path"
 | 
					                            string "Set flash dev path"
 | 
				
			||||||
                            default "/dev/qspi_W25Q128"
 | 
					                            default "/dev/qspi_W25Q128"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifeq ($(CONFIG_USER_TEST_SEMC),y)
 | 
					    ifeq ($(CONFIG_USER_TEST_SEMC),y)
 | 
				
			||||||
| 
						 | 
					@ -22,7 +22,7 @@ ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := test_shell.c
 | 
					    SRC_FILES := test_shell.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifeq ($(CONFIG_USER_TEST_ADC),y)
 | 
					    ifeq ($(CONFIG_USER_TEST_ADC),y)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,7 +21,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestAdc(void)
 | 
					void TestAdc(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,7 +21,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static uint16_t image_buff[384000];
 | 
					static uint16_t image_buff[384000];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestCAN(void)
 | 
					void TestCAN(void)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestDac(void)
 | 
					void TestDac(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <socket.h>
 | 
					#include <socket.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestFlash(void)
 | 
					void TestFlash(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define MAX_READ_LENGTH 1000
 | 
					#define MAX_READ_LENGTH 1000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef BOARD_EDU_RISCV64_EVB
 | 
					#ifdef BOARD_EDU_RISCV64_EVB
 | 
				
			||||||
#define BSP_LED_PIN 29
 | 
					#define BSP_LED_PIN 29
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
		 After Width: | Height: | Size: 75 KiB  | 
| 
		 After Width: | Height: | Size: 102 KiB  | 
| 
						 | 
					@ -0,0 +1,58 @@
 | 
				
			||||||
 | 
					# 基于cortex-m3-emulator实现哈希表并测试验证##
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 1. 简介
 | 
				
			||||||
 | 
					利用c语言实现了哈希表(HashMap),包括添加键值对(Put),获取键对应的值(Get), 删除健(Delete),清空哈希表(Clear), 迭代遍历哈希表(hashMapIterator)等功能
 | 
				
			||||||
 | 
					操作。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					利用数组(Entry)作为存储空间,利用链表(*next)解决冲突。当哈希表的大小超过数组大小后,为避免发生冲突过多的情况,可以对哈希表扩容。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 2. 数据结构设计说明
 | 
				
			||||||
 | 
					键值对结构
 | 
				
			||||||
 | 
					typedef struct entry {
 | 
				
			||||||
 | 
					    void * key;             // 键
 | 
				
			||||||
 | 
					    void * value;           // 值
 | 
				
			||||||
 | 
					    struct entry * next;    // 冲突链表
 | 
				
			||||||
 | 
					}*Entry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					哈希结构
 | 
				
			||||||
 | 
					typedef struct hashMap {
 | 
				
			||||||
 | 
					    int size;           // 当前大小
 | 
				
			||||||
 | 
					    int listSize;       // 有效空间大小
 | 
				
			||||||
 | 
					    HashCode hashCode;  // 哈希函数
 | 
				
			||||||
 | 
					    Equal equal;        // 判等函数
 | 
				
			||||||
 | 
					    Entry list;         // 存储区域
 | 
				
			||||||
 | 
					    Put put;            // 添加键的函数
 | 
				
			||||||
 | 
					    Get get;            // 获取键对应值的函数
 | 
				
			||||||
 | 
					    Remove remove;      // 删除键
 | 
				
			||||||
 | 
					    Clear clear;        // 清空Map
 | 
				
			||||||
 | 
					    Exists exists;      // 判断键是否存在
 | 
				
			||||||
 | 
					    Boolean autoAssign;	// 设定是否根据当前数据量动态调整内存大小,默认开启
 | 
				
			||||||
 | 
					}*HashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					包括以下函数功能,分别为:
 | 
				
			||||||
 | 
					`createHashMap`:创建一个哈希结构
 | 
				
			||||||
 | 
					`defaultPut`:添加键值对
 | 
				
			||||||
 | 
					`defaultGet`:获取键对应值
 | 
				
			||||||
 | 
					`defaultRemove`:删除指定键的键值对
 | 
				
			||||||
 | 
					`defaultExists`:判断键值是否存在
 | 
				
			||||||
 | 
					`defaultClear`:清空Map的函数类型
 | 
				
			||||||
 | 
					`resetHashMap`:重新构建哈希表
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 3. 测试程序说明
 | 
				
			||||||
 | 
					测试了哈希表的插入键值对(Put),判断键是否存在(Exist),获取键对应的值(Get), 删除健(Delete),迭代遍历哈希表(hashMapIterator),清空哈希表(Clear)等操作。
 | 
				
			||||||
 | 
					并展示了利用链地址法解决哈希冲突的示例, 两个不同的人(Bob和Li Ming)的hashcode相同。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 4. 运行结果(##需结合运行测试截图按步骤说明##)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					打开menuconfig之后,将test_hash_map开启(y),保存后退出
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					编译XiZi-cortex-m3-emulator.elf成功
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					启动qemu模拟Xiuos操作系统,验证TestHash注册Shell命令
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					执行TestHash命令,打印测试结果。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
		 After Width: | Height: | Size: 94 KiB  | 
| 
		 After Width: | Height: | Size: 95 KiB  | 
| 
						 | 
					@ -0,0 +1,323 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					* @file:    test_hash.c
 | 
				
			||||||
 | 
					* @brief:   a application of test hash function
 | 
				
			||||||
 | 
					* @version: 3.0
 | 
				
			||||||
 | 
					* @author:  Yao wenying
 | 
				
			||||||
 | 
					* @date:    2023/05/26
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					#include"test_hash.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int defaultHashCode(HashMap hashMap, let key) {
 | 
				
			||||||
 | 
						char * k = (char *)key;
 | 
				
			||||||
 | 
						unsigned long h = 0;
 | 
				
			||||||
 | 
						while (*k) {
 | 
				
			||||||
 | 
							h = (h << 4) + *k++;
 | 
				
			||||||
 | 
							unsigned long g = h & 0xF0000000L;
 | 
				
			||||||
 | 
							if (g) {
 | 
				
			||||||
 | 
								h ^= g >> 24;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							h &= ~g;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return h % hashMap->listSize;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Boolean defaultEqual(let key1, let key2) {
 | 
				
			||||||
 | 
						return strcmp((string)key1, (string)key2) ? False : True;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void resetHashMap(HashMap hashMap, int listSize) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (listSize < 8) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 键值对临时存储空间
 | 
				
			||||||
 | 
						Entry tempList = newEntryList(hashMap->size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						HashMapIterator iterator = createHashMapIterator(hashMap);
 | 
				
			||||||
 | 
						int length = hashMap->size;
 | 
				
			||||||
 | 
						for (int index = 0; hasNextHashMapIterator(iterator); index++) {
 | 
				
			||||||
 | 
							// 迭代取出所有键值对
 | 
				
			||||||
 | 
							iterator = nextHashMapIterator(iterator);
 | 
				
			||||||
 | 
							tempList[index].key = iterator->entry->key;
 | 
				
			||||||
 | 
							tempList[index].value = iterator->entry->value;
 | 
				
			||||||
 | 
							tempList[index].next = NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						freeHashMapIterator(&iterator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 清除原有键值对数据
 | 
				
			||||||
 | 
						hashMap->size = 0;
 | 
				
			||||||
 | 
						for (int i = 0; i < hashMap->listSize; i++) {
 | 
				
			||||||
 | 
							Entry current = &hashMap->list[i];
 | 
				
			||||||
 | 
							current->key = NULL;
 | 
				
			||||||
 | 
							current->value = NULL;
 | 
				
			||||||
 | 
							if (current->next != NULL) {
 | 
				
			||||||
 | 
								while (current->next != NULL) {
 | 
				
			||||||
 | 
									Entry temp = current->next->next;
 | 
				
			||||||
 | 
									free(current->next);
 | 
				
			||||||
 | 
									current->next = temp;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 更改内存大小
 | 
				
			||||||
 | 
						hashMap->listSize = listSize;
 | 
				
			||||||
 | 
						Entry relist = (Entry)realloc(hashMap->list, hashMap->listSize * sizeof(struct entry));
 | 
				
			||||||
 | 
						if (relist != NULL) {
 | 
				
			||||||
 | 
							hashMap->list = relist;
 | 
				
			||||||
 | 
							relist = NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 初始化数据
 | 
				
			||||||
 | 
						for (int i = 0; i < hashMap->listSize; i++) {
 | 
				
			||||||
 | 
							hashMap->list[i].key = NULL;
 | 
				
			||||||
 | 
							hashMap->list[i].value = NULL;
 | 
				
			||||||
 | 
							hashMap->list[i].next = NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 将所有键值对重新写入内存
 | 
				
			||||||
 | 
						for (int i = 0; i < length; i++) {
 | 
				
			||||||
 | 
							hashMap->put(hashMap, tempList[i].key, tempList[i].value);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						free(tempList);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void defaultPut(HashMap hashMap, let key, let value) {
 | 
				
			||||||
 | 
						// 获取哈希值
 | 
				
			||||||
 | 
						int index = hashMap->hashCode(hashMap, key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (hashMap->list[index].key == NULL) {
 | 
				
			||||||
 | 
							hashMap->size++;
 | 
				
			||||||
 | 
							// 该地址为空时直接存储
 | 
				
			||||||
 | 
							hashMap->list[index].key = key;
 | 
				
			||||||
 | 
							hashMap->list[index].value = value;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							Entry current = &hashMap->list[index];
 | 
				
			||||||
 | 
							while (current != NULL) {
 | 
				
			||||||
 | 
								if (hashMap->equal(key, current->key)) {
 | 
				
			||||||
 | 
									// 对于键值已经存在的直接覆盖
 | 
				
			||||||
 | 
									current->value = value;
 | 
				
			||||||
 | 
									return;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								current = current->next;
 | 
				
			||||||
 | 
							};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 发生冲突则创建节点挂到相应位置的next上
 | 
				
			||||||
 | 
							Entry entry = newEntry();
 | 
				
			||||||
 | 
							entry->key = key;
 | 
				
			||||||
 | 
							entry->value = value;
 | 
				
			||||||
 | 
							entry->next = hashMap->list[index].next;
 | 
				
			||||||
 | 
							hashMap->list[index].next = entry;
 | 
				
			||||||
 | 
							hashMap->size++;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (hashMap->autoAssign && hashMap->size >= hashMap->listSize) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 内存扩充至原来的两倍
 | 
				
			||||||
 | 
							// *注: 扩充时考虑的是当前存储元素数量与存储空间的大小关系,而不是存储空间是否已经存满,
 | 
				
			||||||
 | 
							// 例如: 存储空间为10,存入了10个键值对,但是全部冲突了,所以存储空间空着9个,其余的全部挂在一个上面,
 | 
				
			||||||
 | 
							// 这样检索的时候和遍历查询没有什么区别了,可以简单这样理解,当我存入第11个键值对的时候一定会发生冲突,
 | 
				
			||||||
 | 
							// 这是由哈希函数本身的特性(取模)决定的,冲突就会导致检索变慢,所以这时候扩充存储空间,对原有键值对进行
 | 
				
			||||||
 | 
							// 再次散列,会把冲突的数据再次分散开,加快索引定位速度。
 | 
				
			||||||
 | 
							resetHashMap(hashMap, hashMap->listSize * 2);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let defaultGet(HashMap hashMap, let key) {
 | 
				
			||||||
 | 
						if (hashMap->exists(hashMap, key)) {
 | 
				
			||||||
 | 
							int index = hashMap->hashCode(hashMap, key);
 | 
				
			||||||
 | 
							Entry entry = &hashMap->list[index];
 | 
				
			||||||
 | 
							while (entry != NULL) {
 | 
				
			||||||
 | 
								if (hashMap->equal(entry->key, key)) {
 | 
				
			||||||
 | 
									return entry->value;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								entry = entry->next;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let defaultRemove(HashMap hashMap, let key) {
 | 
				
			||||||
 | 
						int index = hashMap->hashCode(hashMap, key);
 | 
				
			||||||
 | 
						Entry entry = &hashMap->list[index];
 | 
				
			||||||
 | 
						if (entry->key == NULL) {
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					    let entryKey = entry->key;
 | 
				
			||||||
 | 
						Boolean result = False;
 | 
				
			||||||
 | 
						if (hashMap->equal(entry->key, key)) {
 | 
				
			||||||
 | 
							hashMap->size--;
 | 
				
			||||||
 | 
							if (entry->next != NULL) {
 | 
				
			||||||
 | 
								Entry temp = entry->next;
 | 
				
			||||||
 | 
								entry->key = temp->key;
 | 
				
			||||||
 | 
								entry->value = temp->value;
 | 
				
			||||||
 | 
								entry->next = temp->next;
 | 
				
			||||||
 | 
								free(temp);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            entry->key = NULL;
 | 
				
			||||||
 | 
					            entry->value = NULL;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
							result = True;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							Entry p = entry;
 | 
				
			||||||
 | 
							entry = entry->next;
 | 
				
			||||||
 | 
							while (entry != NULL) {
 | 
				
			||||||
 | 
								if (hashMap->equal(entry->key, key)) {
 | 
				
			||||||
 | 
									hashMap->size--;
 | 
				
			||||||
 | 
									p->next = entry->next;
 | 
				
			||||||
 | 
									free(entry);
 | 
				
			||||||
 | 
									result = True;
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								p = entry;
 | 
				
			||||||
 | 
								entry = entry->next;
 | 
				
			||||||
 | 
							};
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果空间占用不足一半,则释放多余内存
 | 
				
			||||||
 | 
						if (result && hashMap->autoAssign &&  hashMap->size < hashMap->listSize / 2) {
 | 
				
			||||||
 | 
							resetHashMap(hashMap, hashMap->listSize / 2);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return entryKey;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Boolean defaultExists(HashMap hashMap, let key) {
 | 
				
			||||||
 | 
						int index = hashMap->hashCode(hashMap, key);
 | 
				
			||||||
 | 
						Entry entry = &hashMap->list[index];
 | 
				
			||||||
 | 
						if (entry->key == NULL) {
 | 
				
			||||||
 | 
							return False;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							while (entry != NULL) {
 | 
				
			||||||
 | 
								if (hashMap->equal(entry->key, key)) {
 | 
				
			||||||
 | 
									return True;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								entry = entry->next;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return False;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void defaultClear(HashMap hashMap) {
 | 
				
			||||||
 | 
						for (int i = 0; i < hashMap->listSize; i++) {
 | 
				
			||||||
 | 
							// 释放冲突值内存
 | 
				
			||||||
 | 
							Entry entry = hashMap->list[i].next;
 | 
				
			||||||
 | 
							while (entry != NULL) {
 | 
				
			||||||
 | 
								Entry next = entry->next;
 | 
				
			||||||
 | 
								free(entry);
 | 
				
			||||||
 | 
								entry = next;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							hashMap->list[i].next = NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 释放存储空间
 | 
				
			||||||
 | 
						free(hashMap->list);
 | 
				
			||||||
 | 
						hashMap->list = NULL;
 | 
				
			||||||
 | 
						hashMap->size = -1;
 | 
				
			||||||
 | 
						hashMap->listSize = 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HashMap createHashMap(HashCode hashCode, Equal equal) {
 | 
				
			||||||
 | 
						HashMap hashMap = newHashMap();
 | 
				
			||||||
 | 
						if (hashMap == NULL) {
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						hashMap->size = 0;
 | 
				
			||||||
 | 
						hashMap->listSize = 8;
 | 
				
			||||||
 | 
						hashMap->hashCode = hashCode == NULL ? defaultHashCode : hashCode;
 | 
				
			||||||
 | 
						hashMap->equal = equal == NULL ? defaultEqual : equal;
 | 
				
			||||||
 | 
						hashMap->exists = defaultExists;
 | 
				
			||||||
 | 
						hashMap->get = defaultGet;
 | 
				
			||||||
 | 
						hashMap->put = defaultPut;
 | 
				
			||||||
 | 
						hashMap->remove = defaultRemove;
 | 
				
			||||||
 | 
						hashMap->clear = defaultClear;
 | 
				
			||||||
 | 
						hashMap->autoAssign = True;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						// 起始分配8个内存空间,溢出时会自动扩充
 | 
				
			||||||
 | 
						hashMap->list = newEntryList(hashMap->listSize);
 | 
				
			||||||
 | 
						if (hashMap->list == NULL) {
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						Entry p = hashMap->list;
 | 
				
			||||||
 | 
						for (int i = 0; i < hashMap->listSize; i++) {
 | 
				
			||||||
 | 
							p[i].key = p[i].value = p[i].next = NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return hashMap;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HashMapIterator createHashMapIterator(HashMap hashMap) {
 | 
				
			||||||
 | 
						HashMapIterator iterator = newHashMapIterator();
 | 
				
			||||||
 | 
						if (iterator == NULL) {
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						iterator->hashMap = hashMap;
 | 
				
			||||||
 | 
						iterator->count = 0;
 | 
				
			||||||
 | 
						iterator->hashCode = -1;
 | 
				
			||||||
 | 
						iterator->entry = NULL;
 | 
				
			||||||
 | 
						return iterator;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Boolean hasNextHashMapIterator(HashMapIterator iterator) {
 | 
				
			||||||
 | 
						return iterator->count < iterator->hashMap->size ? True : False;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HashMapIterator nextHashMapIterator(HashMapIterator iterator) {
 | 
				
			||||||
 | 
						if (hasNextHashMapIterator(iterator)) {
 | 
				
			||||||
 | 
							if (iterator->entry != NULL && iterator->entry->next != NULL) {
 | 
				
			||||||
 | 
								iterator->count++;
 | 
				
			||||||
 | 
								iterator->entry = iterator->entry->next;
 | 
				
			||||||
 | 
								return iterator;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							while (++iterator->hashCode < iterator->hashMap->listSize) {
 | 
				
			||||||
 | 
								Entry entry = &iterator->hashMap->list[iterator->hashCode];
 | 
				
			||||||
 | 
								if (entry->key != NULL) {
 | 
				
			||||||
 | 
									iterator->count++;
 | 
				
			||||||
 | 
									iterator->entry = entry;
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return iterator;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void freeHashMapIterator(HashMapIterator * iterator) {
 | 
				
			||||||
 | 
						free(*iterator);
 | 
				
			||||||
 | 
						*iterator = NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define Put(map, key, value) map->put(map, (void *)key, (void *)value);
 | 
				
			||||||
 | 
					#define Get(map, key) (char *)map->get(map, (void *)key)
 | 
				
			||||||
 | 
					#define Remove(map, key) map->remove(map, (void *)key)
 | 
				
			||||||
 | 
					#define Existe(map, key) map->exists(map, (void *)key)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void TestHash() {
 | 
				
			||||||
 | 
					    HashMap map = createHashMap(NULL, NULL);
 | 
				
			||||||
 | 
					    Put(map, "000123", "Annie");
 | 
				
			||||||
 | 
					    Put(map, "000245", "Bob");
 | 
				
			||||||
 | 
					    Put(map, "000284", "Daniel");
 | 
				
			||||||
 | 
					    Put(map, "000281", "Luna");
 | 
				
			||||||
 | 
					    Put(map, "000587", "Yao");
 | 
				
			||||||
 | 
					    Put(map, "000985", "Li Ming");
 | 
				
			||||||
 | 
					    Put(map, "000852", "Janne");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("print the key-values in hashmap:\n");
 | 
				
			||||||
 | 
					    HashMapIterator iterator = createHashMapIterator(map);
 | 
				
			||||||
 | 
					    while (hasNextHashMapIterator(iterator)) {
 | 
				
			||||||
 | 
					        iterator = nextHashMapIterator(iterator);
 | 
				
			||||||
 | 
					        printf("{ key: %s, key: %s, hashcode: %d }\n",
 | 
				
			||||||
 | 
					            (char *)iterator->entry->key, (char *)iterator->entry->value, iterator->hashCode);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    printf("key: 000852, exists: %s\n", Existe(map, "000852") ? "true" : "false");
 | 
				
			||||||
 | 
					    printf("000852: %s\n", Get(map, "000852"));
 | 
				
			||||||
 | 
					    printf("remove 000852 %s\n", Remove(map, "000852") ? "true" : "false");
 | 
				
			||||||
 | 
					    printf("key: 000852, exists: %s\n", Existe(map, "000852") ? "true" : "false");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    map->clear(map);
 | 
				
			||||||
 | 
					    freeHashMapIterator(&iterator);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PRIV_SHELL_CMD_FUNCTION(TestHash, Implement hash_map, PRIV_SHELL_CMD_MAIN_ATTR);
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,129 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					* @file:    test_hash.h
 | 
				
			||||||
 | 
					* @brief:   a application of test hash function
 | 
				
			||||||
 | 
					* @version: 3.0
 | 
				
			||||||
 | 
					* @author:  Yao wenying
 | 
				
			||||||
 | 
					* @date:    2023/05/26
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef __HASHMAP_H__
 | 
				
			||||||
 | 
					#define __HASHMAP_H__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include<stdlib.h>
 | 
				
			||||||
 | 
					#include<string.h>
 | 
				
			||||||
 | 
					#include<stdio.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 实现数据的基本类型
 | 
				
			||||||
 | 
					// 字符串类型
 | 
				
			||||||
 | 
					#define string char *
 | 
				
			||||||
 | 
					#define newString(str) strcpy((char *)malloc(strlen(str) + 1), str)
 | 
				
			||||||
 | 
					#define NEW(type) (type *)malloc(sizeof(type))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 布尔类型
 | 
				
			||||||
 | 
					enum _Boolean { True = 1, False = 0 };
 | 
				
			||||||
 | 
					typedef enum _Boolean  Boolean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define let void *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct entry {
 | 
				
			||||||
 | 
						let key;				// 键
 | 
				
			||||||
 | 
						let value;				// 值
 | 
				
			||||||
 | 
						struct entry * next;	// 冲突链表
 | 
				
			||||||
 | 
					}*Entry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define newEntry() NEW(struct entry)
 | 
				
			||||||
 | 
					#define newEntryList(length) (Entry)malloc(length * sizeof(struct entry))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 哈希结构
 | 
				
			||||||
 | 
					typedef struct hashMap *HashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define newHashMap() NEW(struct hashMap)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 哈希函数类型
 | 
				
			||||||
 | 
					typedef int(*HashCode)(HashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 判等函数类型
 | 
				
			||||||
 | 
					typedef Boolean(*Equal)(let key1, let key2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 添加键函数类型
 | 
				
			||||||
 | 
					typedef void(*Put)(HashMap hashMap, let key, let value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取键对应值的函数类型
 | 
				
			||||||
 | 
					typedef let(*Get)(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 删除键的函数类型
 | 
				
			||||||
 | 
					typedef let(*Remove)(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 清空Map的函数类型
 | 
				
			||||||
 | 
					typedef void(*Clear)(HashMap hashMap);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 判断键值是否存在的函数类型
 | 
				
			||||||
 | 
					typedef Boolean(*Exists)(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct hashMap {
 | 
				
			||||||
 | 
						int size;			// 当前大小
 | 
				
			||||||
 | 
						int listSize;		// 有效空间大小
 | 
				
			||||||
 | 
						HashCode hashCode;	// 哈希函数
 | 
				
			||||||
 | 
						Equal equal;		// 判等函数
 | 
				
			||||||
 | 
						Entry list;			// 存储区域
 | 
				
			||||||
 | 
						Put put;			// 添加键的函数
 | 
				
			||||||
 | 
						Get get;			// 获取键对应值的函数
 | 
				
			||||||
 | 
						Remove remove;		// 删除键
 | 
				
			||||||
 | 
						Clear clear;		// 清空Map
 | 
				
			||||||
 | 
						Exists exists;		// 判断键是否存在
 | 
				
			||||||
 | 
						Boolean autoAssign;	// 设定是否根据当前数据量动态调整内存大小,默认开启
 | 
				
			||||||
 | 
					}*HashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 迭代器结构
 | 
				
			||||||
 | 
					typedef struct hashMapIterator {
 | 
				
			||||||
 | 
						Entry entry;	// 迭代器当前指向
 | 
				
			||||||
 | 
						int count;		// 迭代次数
 | 
				
			||||||
 | 
						int hashCode;	// 键值对的哈希值
 | 
				
			||||||
 | 
						HashMap hashMap;
 | 
				
			||||||
 | 
					}*HashMapIterator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define newHashMapIterator() NEW(struct hashMapIterator)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认哈希函数
 | 
				
			||||||
 | 
					static int defaultHashCode(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认判断键值是否相等
 | 
				
			||||||
 | 
					static Boolean defaultEqual(let key1, let key2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认添加键值对
 | 
				
			||||||
 | 
					static void defaultPut(HashMap hashMap, let key, let value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认获取键对应值
 | 
				
			||||||
 | 
					static let defaultGet(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认删除键
 | 
				
			||||||
 | 
					static let defaultRemove(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认判断键是否存在
 | 
				
			||||||
 | 
					static Boolean defaultExists(HashMap hashMap, let key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 默认清空Map
 | 
				
			||||||
 | 
					static void defaultClear(HashMap hashMap);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 重新构建
 | 
				
			||||||
 | 
					static void resetHashMap(HashMap hashMap, int listSize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 创建一个哈希结构
 | 
				
			||||||
 | 
					HashMap createHashMap(HashCode hashCode, Equal equal);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 创建哈希结构迭代器
 | 
				
			||||||
 | 
					HashMapIterator createHashMapIterator(HashMap hashMap);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 迭代器是否有下一个
 | 
				
			||||||
 | 
					Boolean hasNextHashMapIterator(HashMapIterator iterator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 迭代到下一次
 | 
				
			||||||
 | 
					HashMapIterator nextHashMapIterator(HashMapIterator iterator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 释放迭代器内存
 | 
				
			||||||
 | 
					void freeHashMapIterator(HashMapIterator * iterator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // !__HASHMAP_H__
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define BSP_LED_PIN 134
 | 
					#define BSP_LED_PIN 134
 | 
				
			||||||
#define NULL_PARAMETER 0
 | 
					#define NULL_PARAMETER 0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define I2C_SLAVE_ADDRESS 0x0012U
 | 
					#define I2C_SLAVE_ADDRESS 0x0012U
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef CONFIG_K210_LCD
 | 
					#ifdef CONFIG_K210_LCD
 | 
				
			||||||
void LcdDemo(void)
 | 
					void LcdDemo(void)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define GRAPHIC_CTRL_RECT_UPDATE 0x00
 | 
					#define GRAPHIC_CTRL_RECT_UPDATE 0x00
 | 
				
			||||||
#define LCD_STRING_TYPE 0
 | 
					#define LCD_STRING_TYPE 0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define NULL_PARAMETER 0
 | 
					#define NULL_PARAMETER 0
 | 
				
			||||||
#define E220_CFG_LENGTH
 | 
					#define E220_CFG_LENGTH
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(CONFIG_MUSL_LIBC) && defined(CONFIG_FS_AUTOMOUNTER)
 | 
					#if defined(CONFIG_MUSL_LIBC) && defined(CONFIG_FS_AUTOMOUNTER)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,67 @@
 | 
				
			||||||
 | 
					# 基于k210-emulator实现基数树并测试验证
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 1. 简介
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					基于矽璓模拟器k210-emulator,实现基数树,并编写测试程序在shell终端打印结果。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 2. 数据结构设计说明
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					基数树节点设计为:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```c
 | 
				
			||||||
 | 
					typedef struct _node {
 | 
				
			||||||
 | 
					    void* value;
 | 
				
			||||||
 | 
					    struct _node* next[NODE_SIZE];
 | 
				
			||||||
 | 
					} node;
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					其中,节点在树中的路径即为键,`value` 存储值,`NODE_SIZE` 定义为 128,足以容纳所有 ASCII 值。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					一共实现了 5 个函数,分别为:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- `CreateNode`:创建一个基数树节点
 | 
				
			||||||
 | 
					- `InsertNode`:将一对键值对插入基数树
 | 
				
			||||||
 | 
					- `DeleteNode`:删除指定键的键值对
 | 
				
			||||||
 | 
					- `FindNode`:查找指定键对应的值
 | 
				
			||||||
 | 
					- `DestroyTree`:销毁整个基数树
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 3. 测试程序说明
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					测试程序 `TestRadix` 已经注册为 shell 命令,可以调用执行。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					测试程序定义了以下键值对:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```c
 | 
				
			||||||
 | 
					char keys[][MAX_WORD_LEN] = {
 | 
				
			||||||
 | 
					    "what",
 | 
				
			||||||
 | 
					    "where",
 | 
				
			||||||
 | 
					    "why",
 | 
				
			||||||
 | 
					    "how",
 | 
				
			||||||
 | 
					    "hello!",
 | 
				
			||||||
 | 
					    "apple",
 | 
				
			||||||
 | 
					    "12345"
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					int values[] = {1, 2, 3, 4, 5, 6, 7};
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. 程序的第一部分创建了基数树,并且将定义的 7 个键值对的前 6 个插入了基数树,然后分别查找 7 个键,前 6 个均可以找到对应的值,最后一个未插入,因此无法找到
 | 
				
			||||||
 | 
					2. 程序的第二部分从基数树中删除了 `where` 和 `how` 两个键,再次分别查找 7 个键,删除的键值对和未插入的键值对均无法找到
 | 
				
			||||||
 | 
					3. 程序的第三部分重新插入了已删除的 `where` 和未插入过的 `12345` ,再次分别查找 7 个键,新插入的值可以检索到
 | 
				
			||||||
 | 
					4. 程序的第四部分将基数树销毁,再次分别查找 7 个键,所有的键值对均无法找到
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 4. 运行结果(##需结合运行测试截图按步骤说明##)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. 在工作区终端中输入命令:`make BOARD=k210-emulator menuconfig`,进入配置页面
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					2. 依次进入 `APP_Framework` -> `Applications` -> `test app` 目录,将 `Enable application test function` 选项置为 `Y`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					3. 进入 `Enable application test function` 将 `Config test radix tree` 选项置为 `Y`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					4. 一直选择 `Exit` 退出配置,在最后需要确认的页面选择 `Yes` 保存配置
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					5. 执行编译命令:`make BOARD=k210-emulator`,正常情况下应当编译无误
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					6. 在 `qemu` 中运行:`qemu-system-riscv64 -nographic -machine sifive_u -bios build/XiZi-k210-emulator.elf`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					7. 在 shell 中运行命令 `TestRadix`,执行结果与预期一致,验证完成。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
		 After Width: | Height: | Size: 89 KiB  | 
| 
		 After Width: | Height: | Size: 71 KiB  | 
| 
		 After Width: | Height: | Size: 131 KiB  | 
| 
		 After Width: | Height: | Size: 32 KiB  | 
| 
		 After Width: | Height: | Size: 23 KiB  | 
| 
		 After Width: | Height: | Size: 61 KiB  | 
| 
		 After Width: | Height: | Size: 238 KiB  | 
| 
						 | 
					@ -0,0 +1,187 @@
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					* @file:    test_radix_tree.c
 | 
				
			||||||
 | 
					* @brief:   Implement a simple radix tree
 | 
				
			||||||
 | 
					* @version: 1.0
 | 
				
			||||||
 | 
					* @date:    2023/5/24
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					#include "test_radix_tree.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @description: Create a radix tree node
 | 
				
			||||||
 | 
					 * @return node pointer
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					node* CreateNode()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    node* n = (node*)malloc(sizeof(node));
 | 
				
			||||||
 | 
					    n->value = NULL;
 | 
				
			||||||
 | 
					    for (int i = 0; i < NODE_SIZE; i++) {
 | 
				
			||||||
 | 
					        n->next[i] = NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return n;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @description: Insert a new node to radix tree
 | 
				
			||||||
 | 
					 * @param root - radix tree root
 | 
				
			||||||
 | 
					 * @param key - new node key
 | 
				
			||||||
 | 
					 * @param value - new node value
 | 
				
			||||||
 | 
					 * @return void
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void InsertNode(node* root, const char* key, void* value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (root == NULL) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    node* cur = root;
 | 
				
			||||||
 | 
					    size_t len = strlen(key);
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < len; i++) {
 | 
				
			||||||
 | 
					        uint8_t b = (uint8_t)key[i];
 | 
				
			||||||
 | 
					        if (cur->next[b] == NULL) {
 | 
				
			||||||
 | 
					            cur->next[b] = CreateNode();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        cur = cur->next[b];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    cur->value = value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @description: Delete a node from radix tree
 | 
				
			||||||
 | 
					 * @param root - radix tree root
 | 
				
			||||||
 | 
					 * @param key - key which is needed to delete
 | 
				
			||||||
 | 
					 * @return void
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void DeleteNode(node* root, const char* key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (root == NULL) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    node** cur = &root;
 | 
				
			||||||
 | 
					    size_t len = strlen(key);
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < len; i++) {
 | 
				
			||||||
 | 
					        uint8_t b = (uint8_t)key[i];
 | 
				
			||||||
 | 
					        if ((*cur)->next[b] == NULL) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        cur = &((*cur)->next[b]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ((*cur)->value == NULL) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    (*cur)->value = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int has_children = 0;
 | 
				
			||||||
 | 
					    for (int i = 0; i < NODE_SIZE; i++) {
 | 
				
			||||||
 | 
					        if ((*cur)->next[i] != NULL) {
 | 
				
			||||||
 | 
					            has_children = 1;
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!has_children) {
 | 
				
			||||||
 | 
					        free(*cur);
 | 
				
			||||||
 | 
					        (*cur) = NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @description: find a node by key
 | 
				
			||||||
 | 
					 * @param root - radix tree root
 | 
				
			||||||
 | 
					 * @param key - key which is needed to find
 | 
				
			||||||
 | 
					 * @return value pointer corresponding to key
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void* FindNode(node* root, const char* key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (root == NULL) {
 | 
				
			||||||
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    node* cur = root;
 | 
				
			||||||
 | 
					    size_t len = strlen(key);
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < len; i++) {
 | 
				
			||||||
 | 
					        uint8_t b = (uint8_t)key[i];
 | 
				
			||||||
 | 
					        if (cur->next[b] == NULL) {
 | 
				
			||||||
 | 
					            return NULL;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        cur = cur->next[b];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return cur->value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @description: Destroy the radix tree
 | 
				
			||||||
 | 
					 * @param root - radix tree root
 | 
				
			||||||
 | 
					 * @return void
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void DestroyTree(node* root)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (root == NULL) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    for (int i = 0; i < NODE_SIZE; i++) {
 | 
				
			||||||
 | 
					        DestroyTree(root->next[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    free(root);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void TestRadix()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    char keys[][MAX_WORD_LEN] = {
 | 
				
			||||||
 | 
					        "what",
 | 
				
			||||||
 | 
					        "where",
 | 
				
			||||||
 | 
					        "why",
 | 
				
			||||||
 | 
					        "how",
 | 
				
			||||||
 | 
					        "hello!",
 | 
				
			||||||
 | 
					        "apple",
 | 
				
			||||||
 | 
					        "12345"
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    int values[] = {1, 2, 3, 4, 5, 6, 7};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("\nCreate tree and add key & value:\n");
 | 
				
			||||||
 | 
					    node* root = CreateNode();
 | 
				
			||||||
 | 
					    if (!root) printf("Create node failed.\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int num = sizeof(keys) / sizeof(keys[0]);
 | 
				
			||||||
 | 
					    for (int i = 0; i < num - 1; ++i) {
 | 
				
			||||||
 | 
					        InsertNode(root, keys[i], &values[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (int i = 0; i < num; ++i) {
 | 
				
			||||||
 | 
					        int* v = (int*)FindNode(root, keys[i]);
 | 
				
			||||||
 | 
					        if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
 | 
				
			||||||
 | 
					        else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("\nDelete \"where\" and \"how\":\n");
 | 
				
			||||||
 | 
					    DeleteNode(root, keys[1]);
 | 
				
			||||||
 | 
					    DeleteNode(root, keys[3]);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for (int i = 0; i < num; ++i) {
 | 
				
			||||||
 | 
					        int* v = (int*)FindNode(root, keys[i]);
 | 
				
			||||||
 | 
					        if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
 | 
				
			||||||
 | 
					        else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("\nInsert \"where\" and \"12345\":\n");
 | 
				
			||||||
 | 
					    InsertNode(root, keys[1], &values[1]);
 | 
				
			||||||
 | 
					    InsertNode(root, keys[6], &values[6]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (int i = 0; i < num; ++i) {
 | 
				
			||||||
 | 
					        int* v = (int*)FindNode(root, keys[i]);
 | 
				
			||||||
 | 
					        if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
 | 
				
			||||||
 | 
					        else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("\nDestroy tree:\n");
 | 
				
			||||||
 | 
					    DestroyTree(root);
 | 
				
			||||||
 | 
					    root = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (int i = 0; i < num; ++i) {
 | 
				
			||||||
 | 
					        int* v = (int*)FindNode(root, keys[i]);
 | 
				
			||||||
 | 
					        if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
 | 
				
			||||||
 | 
					        else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					PRIV_SHELL_CMD_FUNCTION(TestRadix, Implement a simple radix tree, PRIV_SHELL_CMD_MAIN_ATTR);
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,20 @@
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					* @file:    test_radix_tree.h
 | 
				
			||||||
 | 
					* @brief:   Implement a simple radix tree
 | 
				
			||||||
 | 
					* @version: 1.0
 | 
				
			||||||
 | 
					* @date:    2023/5/24
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define NODE_SIZE 128
 | 
				
			||||||
 | 
					#define MAX_WORD_LEN 128
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct _node {
 | 
				
			||||||
 | 
					    void* value;
 | 
				
			||||||
 | 
					    struct _node* next[NODE_SIZE];
 | 
				
			||||||
 | 
					} node;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					node* CreateNode();
 | 
				
			||||||
 | 
					void InsertNode(node* root, const char* key, void* value);
 | 
				
			||||||
 | 
					void DeleteNode(node* root, const char* key);
 | 
				
			||||||
 | 
					void* FindNode(node* root, const char* key);
 | 
				
			||||||
 | 
					void DestroyTree(node* root);
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define BSP_485_DIR_PIN 24
 | 
					#define BSP_485_DIR_PIN 24
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestRTC(int argc,char *argv[])
 | 
					void TestRTC(int argc,char *argv[])
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef CONFIG_BSP_USING_TOUCH
 | 
					#ifdef CONFIG_BSP_USING_TOUCH
 | 
				
			||||||
void TestTouch(void)
 | 
					void TestTouch(void)
 | 
				
			||||||
| 
						 | 
					@ -48,7 +48,7 @@ void TestTouch(void)
 | 
				
			||||||
#define LCD_DOT_TYPE 1
 | 
					#define LCD_DOT_TYPE 1
 | 
				
			||||||
#define LCD_SIZE 320
 | 
					#define LCD_SIZE 320
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#elif defined ADD_XIZI_FETURES
 | 
					#elif defined ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestTouch(void)
 | 
					void TestTouch(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,7 @@
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TestWDT(int argc, char *agrv[])
 | 
					void TestWDT(int argc, char *agrv[])
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,11 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ifeq ($(CONFIG_CONNECTION_ADAPTER_4G),y)
 | 
						ifeq ($(CONFIG_CONNECTION_ADAPTER_4G),y)
 | 
				
			||||||
		SRC_DIR += 4g_app
 | 
							SRC_DIR += 4g_app
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,141 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (c) 2022 AIIT XUOS Lab
 | 
				
			||||||
 | 
					 * XiUOS is licensed under Mulan PSL v2.
 | 
				
			||||||
 | 
					 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 | 
				
			||||||
 | 
					 * You may obtain a copy of Mulan PSL v2 at:
 | 
				
			||||||
 | 
					 *        http://license.coscl.org.cn/MulanPSL2
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 | 
				
			||||||
 | 
					 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 | 
				
			||||||
 | 
					 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 | 
				
			||||||
 | 
					 * See the Mulan PSL v2 for more details.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @file modbus_rtu_app.c
 | 
				
			||||||
 | 
					 * @brief support get data from and send data to Modbus rtu server
 | 
				
			||||||
 | 
					 * @version 3.0
 | 
				
			||||||
 | 
					 * @author AIIT XUOS Lab
 | 
				
			||||||
 | 
					 * @date 2022.12.12
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					#include <adapter.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define ADAPTER_RTU_DATA_LENGTH 112
 | 
				
			||||||
 | 
					#define ADAPTER_RTU_TRANSFER_DATA_LENGTH ADAPTER_RTU_DATA_LENGTH + 16
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					uint8_t client_join_data[ADAPTER_RTU_TRANSFER_DATA_LENGTH];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define REG_INPUT_START 1000
 | 
				
			||||||
 | 
					#define REG_INPUT_NREGS 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static variables ---------------------------------*/
 | 
				
			||||||
 | 
					static USHORT   usRegInputStart = REG_INPUT_START;
 | 
				
			||||||
 | 
					static USHORT   usRegInputBuf[REG_INPUT_NREGS];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    int             iRegIndex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( ( usAddress >= REG_INPUT_START )
 | 
				
			||||||
 | 
					        && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        iRegIndex = ( int )( usAddress - usRegInputStart );
 | 
				
			||||||
 | 
					        while( usNRegs > 0 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            *pucRegBuffer++ =
 | 
				
			||||||
 | 
					                ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
 | 
				
			||||||
 | 
					            *pucRegBuffer++ =
 | 
				
			||||||
 | 
					                ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
 | 
				
			||||||
 | 
					            iRegIndex++;
 | 
				
			||||||
 | 
					            usNRegs--;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_ENOREG;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
 | 
				
			||||||
 | 
					                 eMBRegisterMode eMode )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return MB_ENOREG;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
 | 
				
			||||||
 | 
					               eMBRegisterMode eMode )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return MB_ENOREG;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return MB_ENOREG;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/******************Modbus RTU TEST*********************/
 | 
				
			||||||
 | 
					int AdapterModbusRtuTest(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    const char *send_msg = "Adapter_Modbus_Rtu Test";
 | 
				
			||||||
 | 
					    char recv_msg[256] = {0};
 | 
				
			||||||
 | 
					    int baud_rate = BAUD_RATE_115200;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    struct Adapter* adapter =  AdapterDeviceFindByName(ADAPTER_MODBUS_RTU_NAME);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    struct AdapterData priv_rtu_net;
 | 
				
			||||||
 | 
					    priv_rtu_net.len = ADAPTER_RTU_TRANSFER_DATA_LENGTH;
 | 
				
			||||||
 | 
					    priv_rtu_net.buffer = client_join_data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    AdapterDeviceOpen(adapter);
 | 
				
			||||||
 | 
					    AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    AdapterDeviceJoin(adapter,(uint8_t *)&priv_rtu_net);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Enable the Modbus Protocol Stack. */
 | 
				
			||||||
 | 
					    eStatus = eMBEnable(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for( ;; )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ( void )eMBPoll(  );//判断事件的发生,并执行对应的回调函数
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Here we simply count the number of poll cycles. */
 | 
				
			||||||
 | 
					        usRegInputBuf[0]++;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // while (1) {
 | 
				
			||||||
 | 
					    //     AdapterDeviceSend(adapter, send_msg, strlen(send_msg));
 | 
				
			||||||
 | 
					    //     AdapterDeviceRecv(adapter, recv_msg, 256);
 | 
				
			||||||
 | 
					    //     printf("4G recv msg %s\n", recv_msg);
 | 
				
			||||||
 | 
					    //     memset(recv_msg, 0, 256);
 | 
				
			||||||
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;    
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					PRIV_SHELL_CMD_FUNCTION(AdapterModbusRtuTest, a Modbus Rtu adpter sample, PRIV_SHELL_CMD_FUNC_ATTR);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,10 +1,10 @@
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
SRC_FILES := lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
 | 
					SRC_FILES := lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
 | 
				
			||||||
include $(KERNEL_ROOT)/compiler.mk
 | 
					include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
 | 
					    CSRCS += lwip_tcp_socket_demo.c lwip_udp_socket_demo.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,13 +20,13 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
#include <sys_arch.h>
 | 
					#include <sys_arch.h>
 | 
				
			||||||
#include <lwip/sockets.h>
 | 
					#include <lwip/sockets.h>
 | 
				
			||||||
#include "lwip/sys.h"
 | 
					#include "lwip/sys.h"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
#include <sys/socket.h>
 | 
					#include <sys/socket.h>
 | 
				
			||||||
#include <netinet/in.h>
 | 
					#include <netinet/in.h>
 | 
				
			||||||
#include <arpa/inet.h>
 | 
					#include <arpa/inet.h>
 | 
				
			||||||
| 
						 | 
					@ -45,7 +45,7 @@ static char tcp_demo_ipaddr[] = {192, 168, 131, 77};
 | 
				
			||||||
static char tcp_demo_netmask[] = {255, 255, 254, 0};
 | 
					static char tcp_demo_netmask[] = {255, 255, 254, 0};
 | 
				
			||||||
static char tcp_demo_gwaddr[] = {192, 168, 131, 1};
 | 
					static char tcp_demo_gwaddr[] = {192, 168, 131, 1};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
#define lw_print printf
 | 
					#define lw_print printf
 | 
				
			||||||
#define lw_notice printf
 | 
					#define lw_notice printf
 | 
				
			||||||
#define lw_error printf
 | 
					#define lw_error printf
 | 
				
			||||||
| 
						 | 
					@ -151,7 +151,7 @@ void TcpSocketRecvTest(int argc, char *argv[])
 | 
				
			||||||
        TcpSocketConfigParam(argv[1]);
 | 
					        TcpSocketConfigParam(argv[1]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
    lwip_config_tcp(0, tcp_demo_ipaddr, tcp_demo_netmask, tcp_demo_gwaddr);
 | 
					    lwip_config_tcp(0, tcp_demo_ipaddr, tcp_demo_netmask, tcp_demo_gwaddr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pthread_attr_t attr;
 | 
					    pthread_attr_t attr;
 | 
				
			||||||
| 
						 | 
					@ -159,7 +159,7 @@ void TcpSocketRecvTest(int argc, char *argv[])
 | 
				
			||||||
    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
					    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
				
			||||||
    attr.priority = LWIP_TCP_DEMO_TASK_PRIO;
 | 
					    attr.priority = LWIP_TCP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
| 
						 | 
					@ -222,14 +222,14 @@ void TcpSocketSendTest(int argc, char *argv[])
 | 
				
			||||||
        TcpSocketConfigParam(argv[1]);
 | 
					        TcpSocketConfigParam(argv[1]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
    lwip_config_tcp(0, tcp_demo_ipaddr, tcp_demo_netmask, tcp_demo_gwaddr);
 | 
					    lwip_config_tcp(0, tcp_demo_ipaddr, tcp_demo_netmask, tcp_demo_gwaddr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pthread_attr_t attr;
 | 
					    pthread_attr_t attr;
 | 
				
			||||||
    attr.schedparam.sched_priority = LWIP_TCP_DEMO_TASK_PRIO;
 | 
					    attr.schedparam.sched_priority = LWIP_TCP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
					    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
				
			||||||
    attr.priority = LWIP_TCP_DEMO_TASK_PRIO;
 | 
					    attr.priority = LWIP_TCP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_TCP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,12 +19,12 @@
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
#include <transform.h>
 | 
					#include <transform.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
#include <sys_arch.h>
 | 
					#include <sys_arch.h>
 | 
				
			||||||
#include "lwip/sockets.h"
 | 
					#include "lwip/sockets.h"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
#include <sys/socket.h>
 | 
					#include <sys/socket.h>
 | 
				
			||||||
#include <netinet/in.h>
 | 
					#include <netinet/in.h>
 | 
				
			||||||
#include <arpa/inet.h>
 | 
					#include <arpa/inet.h>
 | 
				
			||||||
| 
						 | 
					@ -133,14 +133,14 @@ void UdpSocketRecvTest(int argc, char *argv[])
 | 
				
			||||||
        UdpSocketConfigParam(argv[1]);
 | 
					        UdpSocketConfigParam(argv[1]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
    lwip_config_tcp(0, udp_demo_ipaddr, udp_demo_netmask, udp_demo_gwaddr);
 | 
					    lwip_config_tcp(0, udp_demo_ipaddr, udp_demo_netmask, udp_demo_gwaddr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pthread_attr_t attr;
 | 
					    pthread_attr_t attr;
 | 
				
			||||||
    attr.schedparam.sched_priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
					    attr.schedparam.sched_priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
					    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
				
			||||||
    attr.priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
					    attr.priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
| 
						 | 
					@ -199,14 +199,14 @@ void UdpSocketSendTest(int argc, char *argv[])
 | 
				
			||||||
        UdpSocketConfigParam(argv[1]);
 | 
					        UdpSocketConfigParam(argv[1]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
    lwip_config_tcp(0, udp_demo_ipaddr, udp_demo_netmask, udp_demo_gwaddr);
 | 
					    lwip_config_tcp(0, udp_demo_ipaddr, udp_demo_netmask, udp_demo_gwaddr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pthread_attr_t attr;
 | 
					    pthread_attr_t attr;
 | 
				
			||||||
    attr.schedparam.sched_priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
					    attr.schedparam.sched_priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
					    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
				
			||||||
    attr.priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
					    attr.priority = LWIP_UDP_DEMO_TASK_PRIO;
 | 
				
			||||||
    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
					    attr.stacksize = LWIP_UDP_DEMO_TASK_STACK_SIZE;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,3 @@
 | 
				
			||||||
SRC_DIR := advantech beckhoff br delta mitsubishi omron schneider siemens ge xinje inovance
 | 
					SRC_DIR := advantech beckhoff br delta mitsubishi omron schneider siemens ge xinje inovance keyence
 | 
				
			||||||
 | 
					
 | 
				
			||||||
include $(KERNEL_ROOT)/compiler.mk
 | 
					include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,3 @@
 | 
				
			||||||
SRC_FILES := inovance_am401_cpu1608tn_ethernet.c
 | 
					SRC_FILES := inovance_am401_cpu1608tn_ethernet.c  inovance_am401_cpu1608tn_uart.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
include $(KERNEL_ROOT)/compiler.mk
 | 
					include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,64 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (c) 2022 AIIT XUOS Lab
 | 
				
			||||||
 | 
					 * XiUOS is licensed under Mulan PSL v2.
 | 
				
			||||||
 | 
					 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 | 
				
			||||||
 | 
					 * You may obtain a copy of Mulan PSL v2 at:
 | 
				
			||||||
 | 
					 *        http://license.coscl.org.cn/MulanPSL2
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 | 
				
			||||||
 | 
					 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 | 
				
			||||||
 | 
					 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 | 
				
			||||||
 | 
					 * See the Mulan PSL v2 for more details.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @file inovance_am401_cpu1608tn_ethernet.c
 | 
				
			||||||
 | 
					 * @brief PLC inovance am401 app
 | 
				
			||||||
 | 
					 * @version 3.0
 | 
				
			||||||
 | 
					 * @author AIIT XUOS Lab
 | 
				
			||||||
 | 
					 * @date 2023.5.15
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <control.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern int Adapter4GActive(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void ControlInovanceam401UartTest(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int i, j = 0;
 | 
				
			||||||
 | 
					    int read_data_length = 0;
 | 
				
			||||||
 | 
					    uint8_t read_data[128] = {0};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef CONNECTION_ADAPTER_4G
 | 
				
			||||||
 | 
					    Adapter4GActive();
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ControlProtocolType modbus_rtu_protocol = ControlProtocolFind();
 | 
				
			||||||
 | 
					    if (NULL == modbus_rtu_protocol) {
 | 
				
			||||||
 | 
					        printf("%s get modbus rtu protocol %p failed\n", __func__, modbus_rtu_protocol);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("%s get modbus rtu protocol %p successfull\n", __func__, modbus_rtu_protocol);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (CONTROL_REGISTERED == modbus_rtu_protocol->protocol_status) {
 | 
				
			||||||
 | 
					        ControlProtocolOpen(modbus_rtu_protocol);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (;;) {
 | 
				
			||||||
 | 
					            read_data_length = ControlProtocolRead(modbus_rtu_protocol, read_data, sizeof(read_data));
 | 
				
			||||||
 | 
					            printf("%s read [%d] modbus rtu data %d using receipe file\n", __func__, i, read_data_length);
 | 
				
			||||||
 | 
					            if (read_data_length) {
 | 
				
			||||||
 | 
					                for (j = 0; j < read_data_length; j ++) {
 | 
				
			||||||
 | 
					                    printf("j %d data 0x%x\n", j, read_data[j]);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            i++;
 | 
				
			||||||
 | 
					            memset(read_data, 0, sizeof(read_data));
 | 
				
			||||||
 | 
					            PrivTaskDelay(10000);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //ControlProtocolClose(modbus_rtu_protocol);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					PRIV_SHELL_CMD_FUNCTION(ControlInovanceam401UartTest, inovance am401 uart Demo, PRIV_SHELL_CMD_MAIN_ATTR);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,80 @@
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					     "device_id": 1,
 | 
				
			||||||
 | 
					    "device_name": "INOVANCE_am401_RTU",
 | 
				
			||||||
 | 
					    "communication_type": 1,
 | 
				
			||||||
 | 
					    "serial_config": {
 | 
				
			||||||
 | 
					        "station": 1,
 | 
				
			||||||
 | 
					        "baud_rate": 9600,
 | 
				
			||||||
 | 
					        "data_bits": 8,
 | 
				
			||||||
 | 
					        "stop_bits": 1,
 | 
				
			||||||
 | 
					        "check_mode":3
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    "protocol_type": 3,
 | 
				
			||||||
 | 
					    "read_period": 2000,
 | 
				
			||||||
 | 
					    "read_item_list": [
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "q00",
 | 
				
			||||||
 | 
					            "value_type": 1,
 | 
				
			||||||
 | 
					            "function_code": 1,
 | 
				
			||||||
 | 
					            "start_address": 0,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "q01",
 | 
				
			||||||
 | 
					            "value_type": 1,
 | 
				
			||||||
 | 
					            "function_code": 1,
 | 
				
			||||||
 | 
					            "start_address":1,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "q10",
 | 
				
			||||||
 | 
					            "value_type": 1,
 | 
				
			||||||
 | 
					            "function_code": 1,
 | 
				
			||||||
 | 
					            "start_address": 8,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "mw100",
 | 
				
			||||||
 | 
					            "value_type": 3,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 100,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "mw101",
 | 
				
			||||||
 | 
					            "value_type": 3,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 101,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "md200",
 | 
				
			||||||
 | 
					            "value_type": 4,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 400,
 | 
				
			||||||
 | 
					            "quantity": 2
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "md201",
 | 
				
			||||||
 | 
					            "value_type": 4,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 402,
 | 
				
			||||||
 | 
					            "quantity": 2
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "md300",
 | 
				
			||||||
 | 
					            "value_type": 9,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 600,
 | 
				
			||||||
 | 
					            "quantity": 2
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "md301",
 | 
				
			||||||
 | 
					            "value_type": 9,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 602,
 | 
				
			||||||
 | 
					            "quantity": 2
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					SRC_FILES := kv_8000.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,22 @@
 | 
				
			||||||
 | 
					# KEYENCE 通信测试
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[TOC]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 通信接线及参数设置
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* 网口
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  *Mosbus TCP协议,IP:192.168.250.40,Port:502
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 存储区
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- 存储区ZF区。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 通信测试
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(1)共测试INT16类型数据。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(2)测试ZF区数据。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(3)D区数据测试,用功能码03和06,以字为单位读写。如读写ZF1,则配方文件中起始地址则直接写1即可。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,53 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (c) 2022 AIIT XUOS Lab
 | 
				
			||||||
 | 
					 * XiUOS is licensed under Mulan PSL v2.
 | 
				
			||||||
 | 
					 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 | 
				
			||||||
 | 
					 * You may obtain a copy of Mulan PSL v2 at:
 | 
				
			||||||
 | 
					 *        http://license.coscl.org.cn/MulanPSL2
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 | 
				
			||||||
 | 
					 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 | 
				
			||||||
 | 
					 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 | 
				
			||||||
 | 
					 * See the Mulan PSL v2 for more details.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @file kv_8000.c
 | 
				
			||||||
 | 
					 * @brief PLC KEYENCE KV-8000 app
 | 
				
			||||||
 | 
					 * @version 3.0
 | 
				
			||||||
 | 
					 * @author AIIT XUOS Lab
 | 
				
			||||||
 | 
					 * @date 2023.4.12
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <control.h>
 | 
				
			||||||
 | 
					void ControlKV8000Test(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int i, j = 0;
 | 
				
			||||||
 | 
					    int read_data_length = 0;
 | 
				
			||||||
 | 
					    uint8_t read_data[128] = {0};
 | 
				
			||||||
 | 
					    ControlProtocolType modbus_tcp_protocol = ControlProtocolFind();
 | 
				
			||||||
 | 
					    if (NULL == modbus_tcp_protocol) {
 | 
				
			||||||
 | 
					        printf("%s get modbus tcp protocol %p failed\n", __func__, modbus_tcp_protocol);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    printf("%s get modbus tcp protocol %p successfull\n", __func__, modbus_tcp_protocol);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (CONTROL_REGISTERED == modbus_tcp_protocol->protocol_status) {
 | 
				
			||||||
 | 
					        ControlProtocolOpen(modbus_tcp_protocol);
 | 
				
			||||||
 | 
					        for (;;) {
 | 
				
			||||||
 | 
					            read_data_length = ControlProtocolRead(modbus_tcp_protocol, read_data, sizeof(read_data));
 | 
				
			||||||
 | 
					            printf("%s read [%d] modbus tcp data %d using receipe file\n", __func__, i, read_data_length);
 | 
				
			||||||
 | 
					            if (read_data_length) {
 | 
				
			||||||
 | 
					                for (j = 0; j < read_data_length; j ++) {
 | 
				
			||||||
 | 
					                    printf("j %d data 0x%x\n", j, read_data[j]);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            i++;
 | 
				
			||||||
 | 
					            memset(read_data, 0, sizeof(read_data));          
 | 
				
			||||||
 | 
					            PrivTaskDelay(10000);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        //ControlProtocolClose(modbus_tcp_protocol);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					PRIV_SHELL_CMD_FUNCTION(ControlKV8000Test, KEYENCE Plc KV_8000 Demo, PRIV_SHELL_CMD_MAIN_ATTR);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    "device_id": 1,
 | 
				
			||||||
 | 
					    "device_name": "GJ2",
 | 
				
			||||||
 | 
					    "communication_type": 0,
 | 
				
			||||||
 | 
					    "socket_config": {
 | 
				
			||||||
 | 
					        "plc_ip": "192.168.250.40",
 | 
				
			||||||
 | 
					        "local_ip": "192.168.250.233",
 | 
				
			||||||
 | 
					        "gateway": "192.168.250.1",
 | 
				
			||||||
 | 
					        "netmask": "255.255.255.0",
 | 
				
			||||||
 | 
					        "port": 502
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    "protocol_type": 2,
 | 
				
			||||||
 | 
					    "read_period": 100,
 | 
				
			||||||
 | 
					    "read_item_list": [
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "ZF0",
 | 
				
			||||||
 | 
					            "value_type": 3,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 0,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "ZF1",
 | 
				
			||||||
 | 
					            "value_type": 3,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 1,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            "value_name": "ZF2",
 | 
				
			||||||
 | 
					            "value_type": 3,
 | 
				
			||||||
 | 
					            "function_code": 3,
 | 
				
			||||||
 | 
					            "start_address": 2,
 | 
				
			||||||
 | 
					            "quantity": 1
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,21 @@
 | 
				
			||||||
 | 
					# SCHNEIDER M241通信测试
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[TOC]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 通信接线及参数设置
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* 串口
 | 
				
			||||||
 | 
					  * M241支持2路485串口,本次采用的是serial2。波特率:9600,数据位:8位,停止位:1位,校验:偶校验
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 存储区
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- 存储区MW区。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 通信测试
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(1)共测试INT16共1种类型数据。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(2)测试MW区数据。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(3)MW区数据测试,用功能码03,以字为单位读取。如读MW100,则配方文件中起始地址则直接写100即可。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					# XINJIE 通信测试
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[TOC]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 通信接线及参数设置
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* 网口
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  *Mosbus TCP协议,IP:192.168.250.32,Port:502
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 存储区
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- 存储区M、D区。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## 通信测试
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(1)共测试BOOL,INT16共2种类型数据。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(2)测试M区及D区数据。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(3)D区数据测试,用功能码03和06,以字为单位读写。如读写D500,则配方文件中起始地址则直接写500即可。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(4)M区数据测试,用功能码01和05,以位为单位读写。如读写M19,则配方文件中起始地址则直接写19即可。
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,11 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += circular_area_app.c
 | 
					    CSRCS += circular_area_app.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := circular_area_app.c
 | 
					    SRC_FILES := circular_area_app.c
 | 
				
			||||||
    include $(KERNEL_ROOT)/compiler.mk
 | 
					    include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,11 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += double_list.c single_list.c
 | 
					    CSRCS += double_list.c single_list.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := double_list.c single_list.c
 | 
					    SRC_FILES := double_list.c single_list.c
 | 
				
			||||||
    include $(KERNEL_ROOT)/compiler.mk
 | 
					    include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
    ifeq ($(CONFIG_K210_FFT_TEST), y)
 | 
					    ifeq ($(CONFIG_K210_FFT_TEST), y)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -155,7 +155,7 @@ void k210_fft_test(void)
 | 
				
			||||||
            cycle[FFT_SOFT][FFT_DIR_BACKWARD]/(sysctl_clock_get_freq(SYSCTL_CLOCK_CPU)/1000000));
 | 
					            cycle[FFT_SOFT][FFT_DIR_BACKWARD]/(sysctl_clock_get_freq(SYSCTL_CLOCK_CPU)/1000000));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
void nuttx_k210_fft_test(void)
 | 
					void nuttx_k210_fft_test(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    pthread_t thread;
 | 
					    pthread_t thread;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -71,7 +71,7 @@ void mnist_nnom(int argc, char **argv)
 | 
				
			||||||
	printf("Predicted label: %d\n", predic_label);
 | 
						printf("Predicted label: %d\n", predic_label);
 | 
				
			||||||
	printf("Probability: %d%%\n", (int)(prob * 100));
 | 
						printf("Probability: %d%%\n", (int)(prob * 100));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
PRIV_SHELL_CMD_FUNCTION(mnist_nnom, a mnist_nnom test sample, PRIV_SHELL_CMD_MAIN_ATTR);
 | 
					PRIV_SHELL_CMD_FUNCTION(mnist_nnom, a mnist_nnom test sample, PRIV_SHELL_CMD_MAIN_ATTR);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef __RT_THREAD_H__
 | 
					#ifdef __RT_THREAD_H__
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -108,7 +108,7 @@ menu "sensor app"
 | 
				
			||||||
                    bool "Using sensor HS300x apps"
 | 
					                    bool "Using sensor HS300x apps"
 | 
				
			||||||
                    default n
 | 
					                    default n
 | 
				
			||||||
                    
 | 
					                    
 | 
				
			||||||
                    if ADD_NUTTX_FETURES
 | 
					                    if ADD_NUTTX_FEATURES
 | 
				
			||||||
                    endif
 | 
					                    endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                endif
 | 
					                endif
 | 
				
			||||||
| 
						 | 
					@ -122,7 +122,7 @@ menu "sensor app"
 | 
				
			||||||
                    bool "Using sensor HS300x apps"
 | 
					                    bool "Using sensor HS300x apps"
 | 
				
			||||||
                    default n
 | 
					                    default n
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if ADD_NUTTX_FETURES
 | 
					                    if ADD_NUTTX_FEATURES
 | 
				
			||||||
                    endif
 | 
					                    endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                endif
 | 
					                endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
    ifeq ($(CONFIG_APPLICATION_SENSOR_HCHO_TB600B_WQ_HCHO1OS), y)
 | 
					    ifeq ($(CONFIG_APPLICATION_SENSOR_HCHO_TB600B_WQ_HCHO1OS), y)
 | 
				
			||||||
| 
						 | 
					@ -64,7 +64,7 @@ ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := 
 | 
					    SRC_FILES := 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifeq ($(CONFIG_APPLICATION_SENSOR_HCHO_TB600B_WQ_HCHO1OS), y)
 | 
					    ifeq ($(CONFIG_APPLICATION_SENSOR_HCHO_TB600B_WQ_HCHO1OS), y)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,16 +5,16 @@ menu "Framework"
 | 
				
			||||||
        default y
 | 
					        default y
 | 
				
			||||||
        choice
 | 
					        choice
 | 
				
			||||||
            prompt "select os features"
 | 
					            prompt "select os features"
 | 
				
			||||||
            default ADD_XIZI_FETURES
 | 
					            default ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            config ADD_XIZI_FETURES
 | 
					            config ADD_XIZI_FEATURES
 | 
				
			||||||
                bool "add xizi fetures"
 | 
					                bool "add xizi features"
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            config ADD_NUTTX_FETURES
 | 
					            config ADD_NUTTX_FEATURES
 | 
				
			||||||
                bool "add nuttx fetures"
 | 
					                bool "add nuttx features"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            config ADD_RTTHREAD_FETURES
 | 
					            config ADD_RTTHREAD_FEATURES
 | 
				
			||||||
                bool "add rt_thread fetures"
 | 
					                bool "add rt_thread features"
 | 
				
			||||||
        endchoice
 | 
					        endchoice
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,12 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += framework_init.c
 | 
					    CSRCS += framework_init.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := framework_init.c
 | 
					    SRC_FILES := framework_init.c
 | 
				
			||||||
    SRC_DIR :=  transform_layer 
 | 
					    SRC_DIR :=  transform_layer 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,12 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += adapter_4g.c
 | 
					    CSRCS += adapter_4g.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := adapter_4g.c
 | 
					    SRC_FILES := adapter_4g.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifeq ($(CONFIG_ADAPTER_EC200T),y)
 | 
					    ifeq ($(CONFIG_ADAPTER_EC200T),y)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@ config ADAPTER_4G_EC200T
 | 
				
			||||||
        string "EC200T adapter name"
 | 
					        string "EC200T adapter name"
 | 
				
			||||||
        default "ec200t"
 | 
					        default "ec200t"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_XIZI_FETURES
 | 
					if ADD_XIZI_FEATURES
 | 
				
			||||||
        config ADAPTER_EC200T_USING_PWRKEY
 | 
					        config ADAPTER_EC200T_USING_PWRKEY
 | 
				
			||||||
                bool "EC200T using PWRKEY pin number"
 | 
					                bool "EC200T using PWRKEY pin number"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
| 
						 | 
					@ -37,7 +37,7 @@ if ADD_XIZI_FETURES
 | 
				
			||||||
        endif
 | 
					        endif
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_NUTTX_FETURES
 | 
					if ADD_NUTTX_FEATURES
 | 
				
			||||||
        config ADAPTER_EC200T_USING_PWRKEY
 | 
					        config ADAPTER_EC200T_USING_PWRKEY
 | 
				
			||||||
                bool "EC200T using PWRKEY pin number"
 | 
					                bool "EC200T using PWRKEY pin number"
 | 
				
			||||||
                default n
 | 
					                default n
 | 
				
			||||||
| 
						 | 
					@ -72,7 +72,7 @@ if ADD_NUTTX_FETURES
 | 
				
			||||||
        endif
 | 
					        endif
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_RTTHREAD_FETURES
 | 
					if ADD_RTTHREAD_FEATURES
 | 
				
			||||||
        config ADAPTER_EC200T_PWRKEY
 | 
					        config ADAPTER_EC200T_PWRKEY
 | 
				
			||||||
                int "EC200T PWRKEY pin number"
 | 
					                int "EC200T PWRKEY pin number"
 | 
				
			||||||
                default "97"
 | 
					                default "97"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,12 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += ec200t.c
 | 
					    CSRCS += ec200t.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := ec200t.c
 | 
					    SRC_FILES := ec200t.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    include $(KERNEL_ROOT)/compiler.mk
 | 
					    include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -142,7 +142,7 @@ out:
 | 
				
			||||||
    return ret;
 | 
					    return ret;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_RTTHREAD_FETURES
 | 
					#ifdef ADD_RTTHREAD_FEATURES
 | 
				
			||||||
static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args){ return 0;}
 | 
					static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args){ return 0;}
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args)
 | 
					static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args)
 | 
				
			||||||
| 
						 | 
					@ -163,6 +163,8 @@ static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args)
 | 
				
			||||||
    serial_cfg.serial_parity_mode = PARITY_NONE;
 | 
					    serial_cfg.serial_parity_mode = PARITY_NONE;
 | 
				
			||||||
    serial_cfg.serial_bit_order = STOP_BITS_1;
 | 
					    serial_cfg.serial_bit_order = STOP_BITS_1;
 | 
				
			||||||
    serial_cfg.serial_invert_mode = NRZ_NORMAL;
 | 
					    serial_cfg.serial_invert_mode = NRZ_NORMAL;
 | 
				
			||||||
 | 
					    //serial receive timeout 10s
 | 
				
			||||||
 | 
					    serial_cfg.serial_timeout = 10000;
 | 
				
			||||||
    serial_cfg.is_ext_uart = 0;
 | 
					    serial_cfg.is_ext_uart = 0;
 | 
				
			||||||
#ifdef ADAPTER_EC200T_DRIVER_EXT_PORT
 | 
					#ifdef ADAPTER_EC200T_DRIVER_EXT_PORT
 | 
				
			||||||
    serial_cfg.is_ext_uart = 1;
 | 
					    serial_cfg.is_ext_uart = 1;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,12 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += adapter.c adapter_agent.c
 | 
					    CSRCS += adapter.c adapter_agent.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := adapter.c adapter_agent.c
 | 
					    SRC_FILES := adapter.c adapter_agent.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifeq ($(CONFIG_CONNECTION_INDUSTRIAL_NETWORK),y)
 | 
					    ifeq ($(CONFIG_CONNECTION_INDUSTRIAL_NETWORK),y)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,6 +53,7 @@ extern "C" {
 | 
				
			||||||
#define ADAPTER_NBIOT_NAME "nbiot"
 | 
					#define ADAPTER_NBIOT_NAME "nbiot"
 | 
				
			||||||
#define ADAPTER_WIFI_NAME "wifi"
 | 
					#define ADAPTER_WIFI_NAME "wifi"
 | 
				
			||||||
#define ADAPTER_ZIGBEE_NAME "zigbee"
 | 
					#define ADAPTER_ZIGBEE_NAME "zigbee"
 | 
				
			||||||
 | 
					#define ADAPTER_MODBUS_RTU_NAME "modbus_rtu"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Adapter;
 | 
					struct Adapter;
 | 
				
			||||||
struct AdapterProductInfo;
 | 
					struct AdapterProductInfo;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,10 +26,10 @@
 | 
				
			||||||
#include <math.h>
 | 
					#include <math.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
# include <user_api.h>
 | 
					# include <user_api.h>
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef ADD_RTTHREAD_FETURES
 | 
					#ifdef ADD_RTTHREAD_FEATURES
 | 
				
			||||||
#include <rtthread.h>
 | 
					#include <rtthread.h>
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#define AT_CMD_MAX_LEN 128
 | 
					#define AT_CMD_MAX_LEN 128
 | 
				
			||||||
| 
						 | 
					@ -424,26 +424,26 @@ int DeleteATAgent(ATAgentType agent)
 | 
				
			||||||
        PrivClose(agent->fd);
 | 
					        PrivClose(agent->fd);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
    if (agent->lock.sem.semcount > 0) {
 | 
					    if (agent->lock.sem.semcount > 0) {
 | 
				
			||||||
        printf("delete agent lock = %d\n",agent->lock.sem.semcount);
 | 
					        printf("delete agent lock = %d\n",agent->lock.sem.semcount);
 | 
				
			||||||
        PrivMutexDelete(&agent->lock);
 | 
					        PrivMutexDelete(&agent->lock);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
#elif defined ADD_RTTHREAD_FETURES
 | 
					#elif defined ADD_RTTHREAD_FEATURES
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    if (agent->lock) {
 | 
					    if (agent->lock) {
 | 
				
			||||||
        printf("delete agent lock = %d\n",agent->lock);
 | 
					        printf("delete agent lock = %d\n",agent->lock);
 | 
				
			||||||
        PrivMutexDelete(&agent->lock);
 | 
					        PrivMutexDelete(&agent->lock);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
    if (agent->entm_rx_notice) {
 | 
					    if (agent->entm_rx_notice) {
 | 
				
			||||||
        printf("delete agent entm_rx_notice = %d\n",agent->entm_rx_notice);
 | 
					        printf("delete agent entm_rx_notice = %d\n",agent->entm_rx_notice);
 | 
				
			||||||
        PrivSemaphoreDelete(&agent->entm_rx_notice);
 | 
					        PrivSemaphoreDelete(&agent->entm_rx_notice);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
     if (agent->rsp_sem) {
 | 
					     if (agent->rsp_sem) {
 | 
				
			||||||
         printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
 | 
					         printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
 | 
				
			||||||
         PrivSemaphoreDelete(&agent->rsp_sem);
 | 
					         PrivSemaphoreDelete(&agent->rsp_sem);
 | 
				
			||||||
| 
						 | 
					@ -518,7 +518,7 @@ static int ATAgentInit(ATAgentType agent)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    agent->receive_mode = DEFAULT_MODE;
 | 
					    agent->receive_mode = DEFAULT_MODE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_NUTTX_FETURES
 | 
					#ifdef ADD_NUTTX_FEATURES
 | 
				
			||||||
    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
					    pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
 | 
				
			||||||
    attr.priority = 18;
 | 
					    attr.priority = 18;
 | 
				
			||||||
    attr.stacksize = 8192;
 | 
					    attr.stacksize = 8192;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,7 +52,7 @@ struct ATAgent
 | 
				
			||||||
    uint32 maintain_len;
 | 
					    uint32 maintain_len;
 | 
				
			||||||
    uint32 maintain_max;
 | 
					    uint32 maintain_max;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES   
 | 
					#ifdef ADD_XIZI_FEATURES   
 | 
				
			||||||
    int lock;
 | 
					    int lock;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    pthread_mutex_t lock;
 | 
					    pthread_mutex_t lock;
 | 
				
			||||||
| 
						 | 
					@ -63,7 +63,7 @@ struct ATAgent
 | 
				
			||||||
    char reply_end_last_char;
 | 
					    char reply_end_last_char;
 | 
				
			||||||
    char reply_end_char;
 | 
					    char reply_end_char;
 | 
				
			||||||
    uint32 reply_char_num;
 | 
					    uint32 reply_char_num;
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES  
 | 
					#ifdef ADD_XIZI_FEATURES  
 | 
				
			||||||
	int rsp_sem;
 | 
						int rsp_sem;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    sem_t rsp_sem;
 | 
					    sem_t rsp_sem;
 | 
				
			||||||
| 
						 | 
					@ -74,7 +74,7 @@ struct ATAgent
 | 
				
			||||||
    char entm_recv_buf[ENTM_RECV_MAX];
 | 
					    char entm_recv_buf[ENTM_RECV_MAX];
 | 
				
			||||||
    uint32 entm_recv_len;
 | 
					    uint32 entm_recv_len;
 | 
				
			||||||
    enum ReceiveMode receive_mode;
 | 
					    enum ReceiveMode receive_mode;
 | 
				
			||||||
#ifdef ADD_XIZI_FETURES
 | 
					#ifdef ADD_XIZI_FEATURES
 | 
				
			||||||
    int entm_rx_notice;
 | 
					    int entm_rx_notice;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    sem_t entm_rx_notice;
 | 
					    sem_t entm_rx_notice;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,12 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += adapter_bluetooth.c
 | 
					    CSRCS += adapter_bluetooth.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := adapter_bluetooth.c
 | 
					    SRC_FILES := adapter_bluetooth.c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifeq ($(CONFIG_ADAPTER_HC08),y)
 | 
					    ifeq ($(CONFIG_ADAPTER_HC08),y)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@ config ADAPTER_BLUETOOTH_HC08
 | 
				
			||||||
        string "HC08 adapter name"
 | 
					        string "HC08 adapter name"
 | 
				
			||||||
        default "hc08"
 | 
					        default "hc08"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_XIZI_FETURES
 | 
					if ADD_XIZI_FEATURES
 | 
				
			||||||
        config ADAPTER_HC08_RECV_BUFFER_SIZE
 | 
					        config ADAPTER_HC08_RECV_BUFFER_SIZE
 | 
				
			||||||
                int "HC08 recv data buffer size"
 | 
					                int "HC08 recv data buffer size"
 | 
				
			||||||
                default "128"
 | 
					                default "128"
 | 
				
			||||||
| 
						 | 
					@ -31,7 +31,7 @@ if ADD_XIZI_FETURES
 | 
				
			||||||
        endif
 | 
					        endif
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_NUTTX_FETURES
 | 
					if ADD_NUTTX_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        config ADAPTER_HC08_WORK_ROLE
 | 
					        config ADAPTER_HC08_WORK_ROLE
 | 
				
			||||||
                string "HC08 work role M(MASTER) or S(SLAVER)"
 | 
					                string "HC08 work role M(MASTER) or S(SLAVER)"
 | 
				
			||||||
| 
						 | 
					@ -58,7 +58,7 @@ if ADD_NUTTX_FETURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_RTTHREAD_FETURES
 | 
					if ADD_RTTHREAD_FEATURES
 | 
				
			||||||
         config ADAPTER_HC08_RECV_BUFFER_SIZE
 | 
					         config ADAPTER_HC08_RECV_BUFFER_SIZE
 | 
				
			||||||
                int "HC08 recv data buffer size"
 | 
					                int "HC08 recv data buffer size"
 | 
				
			||||||
                default "128"
 | 
					                default "128"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,12 @@
 | 
				
			||||||
include $(KERNEL_ROOT)/.config
 | 
					include $(KERNEL_ROOT)/.config
 | 
				
			||||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
 | 
				
			||||||
    include $(APPDIR)/Make.defs
 | 
					    include $(APPDIR)/Make.defs
 | 
				
			||||||
    CSRCS += hc08.c
 | 
					    CSRCS += hc08.c
 | 
				
			||||||
    include $(APPDIR)/Application.mk
 | 
					    include $(APPDIR)/Application.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
 | 
					ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
 | 
				
			||||||
    SRC_FILES := hc08.c
 | 
					    SRC_FILES := hc08.c
 | 
				
			||||||
    include $(KERNEL_ROOT)/compiler.mk
 | 
					    include $(KERNEL_ROOT)/compiler.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -353,7 +353,7 @@ static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PrivTaskDelay(500);
 | 
					    PrivTaskDelay(500);
 | 
				
			||||||
    #ifdef ADD_RTTHREAD_FETURES
 | 
					    #ifdef ADD_RTTHREAD_FEATURES
 | 
				
			||||||
    //Step3 : show hc08 device info, hc08_get send "AT+RX" response device info
 | 
					    //Step3 : show hc08 device info, hc08_get send "AT+RX" response device info
 | 
				
			||||||
    char device_info[HC08_RESP_DEFAULT_SIZE * 2] = {0};
 | 
					    char device_info[HC08_RESP_DEFAULT_SIZE * 2] = {0};
 | 
				
			||||||
    if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_DEVICE_INFO, NULL, device_info) < 0) {
 | 
					    if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_DEVICE_INFO, NULL, device_info) < 0) {
 | 
				
			||||||
| 
						 | 
					@ -369,7 +369,7 @@ static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args)
 | 
				
			||||||
    if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_LUUID, NULL, NULL) < 0) {
 | 
					    if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_LUUID, NULL, NULL) < 0) {
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    #ifdef ADD_RTTHREAD_FETURES
 | 
					    #ifdef ADD_RTTHREAD_FEATURES
 | 
				
			||||||
		uint32_t suuid=1234;
 | 
							uint32_t suuid=1234;
 | 
				
			||||||
		if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_SUUID, &luuid, NULL) < 0) {
 | 
							if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_SUUID, &luuid, NULL) < 0) {
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@ config ADAPTER_ETHERNET_HFA21
 | 
				
			||||||
        string "HFA21 ETHERNET adapter name"
 | 
					        string "HFA21 ETHERNET adapter name"
 | 
				
			||||||
        default "hfa21_ethernet"
 | 
					        default "hfa21_ethernet"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_XIZI_FETURES
 | 
					if ADD_XIZI_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        config ADAPTER_HFA21_DRIVER_EXTUART
 | 
					        config ADAPTER_HFA21_DRIVER_EXTUART
 | 
				
			||||||
                bool "Using extra uart to support ethernet"
 | 
					                bool "Using extra uart to support ethernet"
 | 
				
			||||||
| 
						 | 
					@ -24,11 +24,11 @@ if ADD_XIZI_FETURES
 | 
				
			||||||
        endif
 | 
					        endif
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_NUTTX_FETURES
 | 
					if ADD_NUTTX_FEATURES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ADD_RTTHREAD_FETURES
 | 
					if ADD_RTTHREAD_FEATURES
 | 
				
			||||||
        config ADAPTER_HFA21_DRIVER
 | 
					        config ADAPTER_HFA21_DRIVER
 | 
				
			||||||
                string "HFA21 device uart driver path"
 | 
					                string "HFA21 device uart driver path"
 | 
				
			||||||
                default "/dev/uart3"
 | 
					                default "/dev/uart3"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,101 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * FreeModbus Libary: BARE Demo Application
 | 
				
			||||||
 | 
					 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software; you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation; either version 2 of the License, or
 | 
				
			||||||
 | 
					 * (at your option) any later version.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					 * GNU General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with this program; if not, write to the Free Software
 | 
				
			||||||
 | 
					 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * File: $Id$
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define REG_INPUT_START 1000
 | 
				
			||||||
 | 
					#define REG_INPUT_NREGS 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static variables ---------------------------------*/
 | 
				
			||||||
 | 
					static USHORT   usRegInputStart = REG_INPUT_START;
 | 
				
			||||||
 | 
					static USHORT   usRegInputBuf[REG_INPUT_NREGS];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					int
 | 
				
			||||||
 | 
					main( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Enable the Modbus Protocol Stack. */
 | 
				
			||||||
 | 
					    eStatus = eMBEnable(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for( ;; )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ( void )eMBPoll(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Here we simply count the number of poll cycles. */
 | 
				
			||||||
 | 
					        usRegInputBuf[0]++;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    int             iRegIndex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( ( usAddress >= REG_INPUT_START )
 | 
				
			||||||
 | 
					        && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        iRegIndex = ( int )( usAddress - usRegInputStart );
 | 
				
			||||||
 | 
					        while( usNRegs > 0 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            *pucRegBuffer++ =
 | 
				
			||||||
 | 
					                ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
 | 
				
			||||||
 | 
					            *pucRegBuffer++ =
 | 
				
			||||||
 | 
					                ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
 | 
				
			||||||
 | 
					            iRegIndex++;
 | 
				
			||||||
 | 
					            usNRegs--;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_ENOREG;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
 | 
				
			||||||
 | 
					                 eMBRegisterMode eMode )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return MB_ENOREG;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
 | 
				
			||||||
 | 
					               eMBRegisterMode eMode )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return MB_ENOREG;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return MB_ENOREG;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,485 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					#include "mbascii.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mbcrc.h"
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_ASCII_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_ASCII_DEFAULT_CR     '\r'    /*!< Default CR character for Modbus ASCII. */
 | 
				
			||||||
 | 
					#define MB_ASCII_DEFAULT_LF     '\n'    /*!< Default LF character for Modbus ASCII. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_SIZE_MIN     3       /*!< Minimum size of a Modbus ASCII frame. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_SIZE_MAX     256     /*!< Maximum size of a Modbus ASCII frame. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_SIZE_LRC     1       /*!< Size of LRC field in PDU. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_ADDR_OFF     0       /*!< Offset of slave address in Ser-PDU. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_PDU_OFF      1       /*!< Offset of Modbus-PDU in Ser-PDU. */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Type definitions ---------------------------------*/
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    STATE_RX_IDLE,              /*!< Receiver is in idle state. */
 | 
				
			||||||
 | 
					    STATE_RX_RCV,               /*!< Frame is beeing received. */
 | 
				
			||||||
 | 
					    STATE_RX_WAIT_EOF           /*!< Wait for End of Frame. */
 | 
				
			||||||
 | 
					} eMBRcvState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    STATE_TX_IDLE,              /*!< Transmitter is in idle state. */
 | 
				
			||||||
 | 
					    STATE_TX_START,             /*!< Starting transmission (':' sent). */
 | 
				
			||||||
 | 
					    STATE_TX_DATA,              /*!< Sending of data (Address, Data, LRC). */
 | 
				
			||||||
 | 
					    STATE_TX_END,               /*!< End of transmission. */
 | 
				
			||||||
 | 
					    STATE_TX_NOTIFY             /*!< Notify sender that the frame has been sent. */
 | 
				
			||||||
 | 
					} eMBSndState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BYTE_HIGH_NIBBLE,           /*!< Character for high nibble of byte. */
 | 
				
			||||||
 | 
					    BYTE_LOW_NIBBLE             /*!< Character for low nibble of byte. */
 | 
				
			||||||
 | 
					} eMBBytePos;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static functions ---------------------------------*/
 | 
				
			||||||
 | 
					static UCHAR    prvucMBCHAR2BIN( UCHAR ucCharacter );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static UCHAR    prvucMBBIN2CHAR( UCHAR ucByte );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static UCHAR    prvucMBLRC( UCHAR * pucFrame, USHORT usLen );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static variables ---------------------------------*/
 | 
				
			||||||
 | 
					static volatile eMBSndState eSndState;
 | 
				
			||||||
 | 
					static volatile eMBRcvState eRcvState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* We reuse the Modbus RTU buffer because only one buffer is needed and the
 | 
				
			||||||
 | 
					 * RTU buffer is bigger. */
 | 
				
			||||||
 | 
					extern volatile UCHAR ucRTUBuf[];
 | 
				
			||||||
 | 
					static volatile UCHAR *ucASCIIBuf = ucRTUBuf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static volatile USHORT usRcvBufferPos;
 | 
				
			||||||
 | 
					static volatile eMBBytePos eBytePos;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static volatile UCHAR *pucSndBufferCur;
 | 
				
			||||||
 | 
					static volatile USHORT usSndBufferCount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static volatile UCHAR ucLRC;
 | 
				
			||||||
 | 
					static volatile UCHAR ucMBLFCharacter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBASCIIInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    ( void )ucSlaveAddress;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( xMBPortSerialInit( ucPort, ulBaudRate, 7, eParity ) != TRUE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if( xMBPortTimersInit( MB_ASCII_TIMEOUT_SEC * 20000UL ) != TRUE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					eMBASCIIStart( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    vMBPortSerialEnable( TRUE, FALSE );
 | 
				
			||||||
 | 
					    eRcvState = STATE_RX_IDLE;
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* No special startup required for ASCII. */
 | 
				
			||||||
 | 
					    ( void )xMBPortEventPost( EV_READY );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					eMBASCIIStop( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    vMBPortSerialEnable( FALSE, FALSE );
 | 
				
			||||||
 | 
					    vMBPortTimersDisable(  );
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Length and CRC check */
 | 
				
			||||||
 | 
					    if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
 | 
				
			||||||
 | 
					        && ( prvucMBLRC( ( UCHAR * ) ucASCIIBuf, usRcvBufferPos ) == 0 ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Save the address field. All frames are passed to the upper layed
 | 
				
			||||||
 | 
					         * and the decision if a frame is used is done there.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        *pucRcvAddress = ucASCIIBuf[MB_SER_PDU_ADDR_OFF];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
 | 
				
			||||||
 | 
					         * size of address field and CRC checksum.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        *pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_LRC );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Return the start of the Modbus PDU to the caller. */
 | 
				
			||||||
 | 
					        *pucFrame = ( UCHAR * ) & ucASCIIBuf[MB_SER_PDU_PDU_OFF];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    UCHAR           usLRC;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    /* Check if the receiver is still in idle state. If not we where too
 | 
				
			||||||
 | 
					     * slow with processing the received frame and the master sent another
 | 
				
			||||||
 | 
					     * frame on the network. We have to abort sending the frame.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    if( eRcvState == STATE_RX_IDLE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* First byte before the Modbus-PDU is the slave address. */
 | 
				
			||||||
 | 
					        pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
 | 
				
			||||||
 | 
					        usSndBufferCount = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
 | 
				
			||||||
 | 
					        pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
 | 
				
			||||||
 | 
					        usSndBufferCount += usLength;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
 | 
				
			||||||
 | 
					        usLRC = prvucMBLRC( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
 | 
				
			||||||
 | 
					        ucASCIIBuf[usSndBufferCount++] = usLRC;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Activate the transmitter. */
 | 
				
			||||||
 | 
					        eSndState = STATE_TX_START;
 | 
				
			||||||
 | 
					        vMBPortSerialEnable( FALSE, TRUE );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL
 | 
				
			||||||
 | 
					xMBASCIIReceiveFSM( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BOOL            xNeedPoll = FALSE;
 | 
				
			||||||
 | 
					    UCHAR           ucByte;
 | 
				
			||||||
 | 
					    UCHAR           ucResult;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert( eSndState == STATE_TX_IDLE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
 | 
				
			||||||
 | 
					    switch ( eRcvState )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* A new character is received. If the character is a ':' the input
 | 
				
			||||||
 | 
					         * buffer is cleared. A CR-character signals the end of the data
 | 
				
			||||||
 | 
					         * block. Other characters are part of the data block and their
 | 
				
			||||||
 | 
					         * ASCII value is converted back to a binary representation.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    case STATE_RX_RCV:
 | 
				
			||||||
 | 
					        /* Enable timer for character timeout. */
 | 
				
			||||||
 | 
					        vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					        if( ucByte == ':' )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Empty receive buffer. */
 | 
				
			||||||
 | 
					            eBytePos = BYTE_HIGH_NIBBLE;
 | 
				
			||||||
 | 
					            usRcvBufferPos = 0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if( ucByte == MB_ASCII_DEFAULT_CR )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eRcvState = STATE_RX_WAIT_EOF;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            ucResult = prvucMBCHAR2BIN( ucByte );
 | 
				
			||||||
 | 
					            switch ( eBytePos )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* High nibble of the byte comes first. We check for
 | 
				
			||||||
 | 
					                 * a buffer overflow here. */
 | 
				
			||||||
 | 
					            case BYTE_HIGH_NIBBLE:
 | 
				
			||||||
 | 
					                if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    ucASCIIBuf[usRcvBufferPos] = ( UCHAR )( ucResult << 4 );
 | 
				
			||||||
 | 
					                    eBytePos = BYTE_LOW_NIBBLE;
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    /* not handled in Modbus specification but seems
 | 
				
			||||||
 | 
					                     * a resonable implementation. */
 | 
				
			||||||
 | 
					                    eRcvState = STATE_RX_IDLE;
 | 
				
			||||||
 | 
					                    /* Disable previously activated timer because of error state. */
 | 
				
			||||||
 | 
					                    vMBPortTimersDisable(  );
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            case BYTE_LOW_NIBBLE:
 | 
				
			||||||
 | 
					                ucASCIIBuf[usRcvBufferPos] |= ucResult;
 | 
				
			||||||
 | 
					                usRcvBufferPos++;
 | 
				
			||||||
 | 
					                eBytePos = BYTE_HIGH_NIBBLE;
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    case STATE_RX_WAIT_EOF:
 | 
				
			||||||
 | 
					        if( ucByte == ucMBLFCharacter )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Disable character timeout timer because all characters are
 | 
				
			||||||
 | 
					             * received. */
 | 
				
			||||||
 | 
					            vMBPortTimersDisable(  );
 | 
				
			||||||
 | 
					            /* Receiver is again in idle state. */
 | 
				
			||||||
 | 
					            eRcvState = STATE_RX_IDLE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Notify the caller of eMBASCIIReceive that a new frame
 | 
				
			||||||
 | 
					             * was received. */
 | 
				
			||||||
 | 
					            xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if( ucByte == ':' )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Empty receive buffer and back to receive state. */
 | 
				
			||||||
 | 
					            eBytePos = BYTE_HIGH_NIBBLE;
 | 
				
			||||||
 | 
					            usRcvBufferPos = 0;
 | 
				
			||||||
 | 
					            eRcvState = STATE_RX_RCV;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Enable timer for character timeout. */
 | 
				
			||||||
 | 
					            vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Frame is not okay. Delete entire frame. */
 | 
				
			||||||
 | 
					            eRcvState = STATE_RX_IDLE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    case STATE_RX_IDLE:
 | 
				
			||||||
 | 
					        if( ucByte == ':' )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Enable timer for character timeout. */
 | 
				
			||||||
 | 
					            vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					            /* Reset the input buffers to store the frame. */
 | 
				
			||||||
 | 
					            usRcvBufferPos = 0;;
 | 
				
			||||||
 | 
					            eBytePos = BYTE_HIGH_NIBBLE;
 | 
				
			||||||
 | 
					            eRcvState = STATE_RX_RCV;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return xNeedPoll;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL
 | 
				
			||||||
 | 
					xMBASCIITransmitFSM( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BOOL            xNeedPoll = FALSE;
 | 
				
			||||||
 | 
					    UCHAR           ucByte;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert( eRcvState == STATE_RX_IDLE );
 | 
				
			||||||
 | 
					    switch ( eSndState )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Start of transmission. The start of a frame is defined by sending
 | 
				
			||||||
 | 
					         * the character ':'. */
 | 
				
			||||||
 | 
					    case STATE_TX_START:
 | 
				
			||||||
 | 
					        ucByte = ':';
 | 
				
			||||||
 | 
					        xMBPortSerialPutByte( ( CHAR )ucByte );
 | 
				
			||||||
 | 
					        eSndState = STATE_TX_DATA;
 | 
				
			||||||
 | 
					        eBytePos = BYTE_HIGH_NIBBLE;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Send the data block. Each data byte is encoded as a character hex
 | 
				
			||||||
 | 
					         * stream with the high nibble sent first and the low nibble sent
 | 
				
			||||||
 | 
					         * last. If all data bytes are exhausted we send a '\r' character
 | 
				
			||||||
 | 
					         * to end the transmission. */
 | 
				
			||||||
 | 
					    case STATE_TX_DATA:
 | 
				
			||||||
 | 
					        if( usSndBufferCount > 0 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            switch ( eBytePos )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					            case BYTE_HIGH_NIBBLE:
 | 
				
			||||||
 | 
					                ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur >> 4 ) );
 | 
				
			||||||
 | 
					                xMBPortSerialPutByte( ( CHAR ) ucByte );
 | 
				
			||||||
 | 
					                eBytePos = BYTE_LOW_NIBBLE;
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            case BYTE_LOW_NIBBLE:
 | 
				
			||||||
 | 
					                ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur & 0x0F ) );
 | 
				
			||||||
 | 
					                xMBPortSerialPutByte( ( CHAR )ucByte );
 | 
				
			||||||
 | 
					                pucSndBufferCur++;
 | 
				
			||||||
 | 
					                eBytePos = BYTE_HIGH_NIBBLE;
 | 
				
			||||||
 | 
					                usSndBufferCount--;
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            xMBPortSerialPutByte( MB_ASCII_DEFAULT_CR );
 | 
				
			||||||
 | 
					            eSndState = STATE_TX_END;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Finish the frame by sending a LF character. */
 | 
				
			||||||
 | 
					    case STATE_TX_END:
 | 
				
			||||||
 | 
					        xMBPortSerialPutByte( ( CHAR )ucMBLFCharacter );
 | 
				
			||||||
 | 
					        /* We need another state to make sure that the CR character has
 | 
				
			||||||
 | 
					         * been sent. */
 | 
				
			||||||
 | 
					        eSndState = STATE_TX_NOTIFY;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Notify the task which called eMBASCIISend that the frame has
 | 
				
			||||||
 | 
					         * been sent. */
 | 
				
			||||||
 | 
					    case STATE_TX_NOTIFY:
 | 
				
			||||||
 | 
					        eSndState = STATE_TX_IDLE;
 | 
				
			||||||
 | 
					        xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Disable transmitter. This prevents another transmit buffer
 | 
				
			||||||
 | 
					         * empty interrupt. */
 | 
				
			||||||
 | 
					        vMBPortSerialEnable( TRUE, FALSE );
 | 
				
			||||||
 | 
					        eSndState = STATE_TX_IDLE;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* We should not get a transmitter event if the transmitter is in
 | 
				
			||||||
 | 
					         * idle state.  */
 | 
				
			||||||
 | 
					    case STATE_TX_IDLE:
 | 
				
			||||||
 | 
					        /* enable receiver/disable transmitter. */
 | 
				
			||||||
 | 
					        vMBPortSerialEnable( TRUE, FALSE );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return xNeedPoll;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL
 | 
				
			||||||
 | 
					xMBASCIITimerT1SExpired( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    switch ( eRcvState )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* If we have a timeout we go back to the idle state and wait for
 | 
				
			||||||
 | 
					         * the next frame.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    case STATE_RX_RCV:
 | 
				
			||||||
 | 
					    case STATE_RX_WAIT_EOF:
 | 
				
			||||||
 | 
					        eRcvState = STATE_RX_IDLE;
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    default:
 | 
				
			||||||
 | 
					        assert( ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_WAIT_EOF ) );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    vMBPortTimersDisable(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* no context switch required. */
 | 
				
			||||||
 | 
					    return FALSE;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static          UCHAR
 | 
				
			||||||
 | 
					prvucMBCHAR2BIN( UCHAR ucCharacter )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return ( UCHAR )( ucCharacter - '0' );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return ( UCHAR )( ucCharacter - 'A' + 0x0A );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return 0xFF;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static          UCHAR
 | 
				
			||||||
 | 
					prvucMBBIN2CHAR( UCHAR ucByte )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if( ucByte <= 0x09 )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return ( UCHAR )( '0' + ucByte );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if( ( ucByte >= 0x0A ) && ( ucByte <= 0x0F ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return ( UCHAR )( ucByte - 0x0A + 'A' );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Programming error. */
 | 
				
			||||||
 | 
					        assert( 0 );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return '0';
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static          UCHAR
 | 
				
			||||||
 | 
					prvucMBLRC( UCHAR * pucFrame, USHORT usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    UCHAR           ucLRC = 0;  /* LRC char initialized */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while( usLen-- )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ucLRC += *pucFrame++;   /* Add buffer byte without carry */
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Return twos complement */
 | 
				
			||||||
 | 
					    ucLRC = ( UCHAR ) ( -( ( CHAR ) ucLRC ) );
 | 
				
			||||||
 | 
					    return ucLRC;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,55 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_ASCII_H
 | 
				
			||||||
 | 
					#define _MB_ASCII_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_ASCII_ENABLED > 0
 | 
				
			||||||
 | 
					eMBErrorCode    eMBASCIIInit( UCHAR slaveAddress, UCHAR ucPort,
 | 
				
			||||||
 | 
					                              ULONG ulBaudRate, eMBParity eParity );
 | 
				
			||||||
 | 
					void            eMBASCIIStart( void );
 | 
				
			||||||
 | 
					void            eMBASCIIStop( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode    eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
 | 
				
			||||||
 | 
					                                 USHORT * pusLength );
 | 
				
			||||||
 | 
					eMBErrorCode    eMBASCIISend( UCHAR slaveAddress, const UCHAR * pucFrame,
 | 
				
			||||||
 | 
					                              USHORT usLength );
 | 
				
			||||||
 | 
					BOOL            xMBASCIIReceiveFSM( void );
 | 
				
			||||||
 | 
					BOOL            xMBASCIITransmitFSM( void );
 | 
				
			||||||
 | 
					BOOL            xMBASCIITimerT1SExpired( void );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,269 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_ADDR_OFF           ( MB_PDU_DATA_OFF )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_COILCNT_OFF        ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_SIZE               ( 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_COILCNT_MAX        ( 0x07D0 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_ADDR_OFF          ( MB_PDU_DATA_OFF )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_VALUE_OFF         ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_SIZE              ( 4 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF      ( MB_PDU_DATA_OFF )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF   ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF   ( MB_PDU_DATA_OFF + 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF    ( MB_PDU_DATA_OFF + 5 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN      ( 5 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX   ( 0x07B0 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static functions ---------------------------------*/
 | 
				
			||||||
 | 
					eMBException    prveMBError2Exception( eMBErrorCode eErrorCode );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    USHORT          usCoilCount;
 | 
				
			||||||
 | 
					    UCHAR           ucNBytes;
 | 
				
			||||||
 | 
					    UCHAR          *pucFrameCur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
 | 
				
			||||||
 | 
					        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. 
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        if( ( usCoilCount >= 1 ) &&
 | 
				
			||||||
 | 
					            ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Set the current PDU data pointer to the beginning. */
 | 
				
			||||||
 | 
					            pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
 | 
				
			||||||
 | 
					            *usLen = MB_PDU_FUNC_OFF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* First byte contains the function code. */
 | 
				
			||||||
 | 
					            *pucFrameCur++ = MB_FUNC_READ_COILS;
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Test if the quantity of coils is a multiple of 8. If not last
 | 
				
			||||||
 | 
					             * byte is only partially field with unused coils set to zero. */
 | 
				
			||||||
 | 
					            if( ( usCoilCount & 0x0007 ) != 0 )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ucNBytes = ( UCHAR )( usCoilCount / 8 );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            *pucFrameCur++ = ucNBytes;
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            eRegStatus =
 | 
				
			||||||
 | 
					                eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
 | 
				
			||||||
 | 
					                               MB_REG_READ );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* The response contains the function code, the starting address
 | 
				
			||||||
 | 
					                 * and the quantity of registers. We reuse the old values in the 
 | 
				
			||||||
 | 
					                 * buffer because they are still valid. */
 | 
				
			||||||
 | 
					                *usLen += ucNBytes;;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid read coil register request because the length
 | 
				
			||||||
 | 
					         * is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_COIL_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    UCHAR           ucBuf[2];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
 | 
				
			||||||
 | 
					            ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
 | 
				
			||||||
 | 
					              ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            ucBuf[1] = 0;
 | 
				
			||||||
 | 
					            if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ucBuf[0] = 1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ucBuf[0] = 0;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            eRegStatus =
 | 
				
			||||||
 | 
					                eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid write coil register request because the length
 | 
				
			||||||
 | 
					         * is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    USHORT          usCoilCnt;
 | 
				
			||||||
 | 
					    UCHAR           ucByteCount;
 | 
				
			||||||
 | 
					    UCHAR           ucByteCountVerify;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
 | 
				
			||||||
 | 
					        usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Compute the number of expected bytes in the request. */
 | 
				
			||||||
 | 
					        if( ( usCoilCnt & 0x0007 ) != 0 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if( ( usCoilCnt >= 1 ) &&
 | 
				
			||||||
 | 
					            ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
 | 
				
			||||||
 | 
					            ( ucByteCountVerify == ucByteCount ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eRegStatus =
 | 
				
			||||||
 | 
					                eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
 | 
				
			||||||
 | 
					                               usRegAddress, usCoilCnt, MB_REG_WRITE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* The response contains the function code, the starting address
 | 
				
			||||||
 | 
					                 * 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;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid write coil register request because the length
 | 
				
			||||||
 | 
					         * is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,28 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,133 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_ADDR_OFF           ( MB_PDU_DATA_OFF )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_DISCCNT_OFF        ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_SIZE               ( 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_DISCCNT_MAX        ( 0x07D0 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static functions ---------------------------------*/
 | 
				
			||||||
 | 
					eMBException    prveMBError2Exception( eMBErrorCode eErrorCode );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    USHORT          usDiscreteCnt;
 | 
				
			||||||
 | 
					    UCHAR           ucNBytes;
 | 
				
			||||||
 | 
					    UCHAR          *pucFrameCur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
 | 
				
			||||||
 | 
					        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. 
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        if( ( usDiscreteCnt >= 1 ) &&
 | 
				
			||||||
 | 
					            ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Set the current PDU data pointer to the beginning. */
 | 
				
			||||||
 | 
					            pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
 | 
				
			||||||
 | 
					            *usLen = MB_PDU_FUNC_OFF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* First byte contains the function code. */
 | 
				
			||||||
 | 
					            *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Test if the quantity of coils is a multiple of 8. If not last
 | 
				
			||||||
 | 
					             * byte is only partially field with unused coils set to zero. */
 | 
				
			||||||
 | 
					            if( ( usDiscreteCnt & 0x0007 ) != 0 )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            *pucFrameCur++ = ucNBytes;
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            eRegStatus =
 | 
				
			||||||
 | 
					                eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* The response contains the function code, the starting address
 | 
				
			||||||
 | 
					                 * and the quantity of registers. We reuse the old values in the 
 | 
				
			||||||
 | 
					                 * buffer because they are still valid. */
 | 
				
			||||||
 | 
					                *usLen += ucNBytes;;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid read coil register request because the length
 | 
				
			||||||
 | 
					         * is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,307 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_ADDR_OFF               ( MB_PDU_DATA_OFF + 0)
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_REGCNT_OFF             ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_SIZE                   ( 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_REGCNT_MAX             ( 0x007D )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_ADDR_OFF              ( MB_PDU_DATA_OFF + 0)
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_VALUE_OFF             ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_SIZE                  ( 4 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF          ( MB_PDU_DATA_OFF + 0 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF        ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF       ( MB_PDU_DATA_OFF + 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF        ( MB_PDU_DATA_OFF + 5 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN          ( 5 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX        ( 0x0078 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF     ( MB_PDU_DATA_OFF + 0 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF   ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF    ( MB_PDU_DATA_OFF + 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF  ( MB_PDU_DATA_OFF + 6 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF       ( MB_PDU_DATA_OFF + 8 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF  ( MB_PDU_DATA_OFF + 9 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READWRITE_SIZE_MIN          ( 9 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static functions ---------------------------------*/
 | 
				
			||||||
 | 
					eMBException    prveMBError2Exception( eMBErrorCode eErrorCode );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Make callback to update the value. */
 | 
				
			||||||
 | 
					        eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
 | 
				
			||||||
 | 
					                                      usRegAddress, 1, MB_REG_WRITE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					        if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid request because the length is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    USHORT          usRegCount;
 | 
				
			||||||
 | 
					    UCHAR           ucRegByteCount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if( ( usRegCount >= 1 ) &&
 | 
				
			||||||
 | 
					            ( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
 | 
				
			||||||
 | 
					            ( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Make callback to update the register values. */
 | 
				
			||||||
 | 
					            eRegStatus =
 | 
				
			||||||
 | 
					                eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
 | 
				
			||||||
 | 
					                                 usRegAddress, usRegCount, MB_REG_WRITE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* The response contains the function code, the starting
 | 
				
			||||||
 | 
					                 * address 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;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid request because the length is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    USHORT          usRegCount;
 | 
				
			||||||
 | 
					    UCHAR          *pucFrameCur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
 | 
				
			||||||
 | 
					        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. 
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Set the current PDU data pointer to the beginning. */
 | 
				
			||||||
 | 
					            pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
 | 
				
			||||||
 | 
					            *usLen = MB_PDU_FUNC_OFF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* First byte contains the function code. */
 | 
				
			||||||
 | 
					            *pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Second byte in the response contain the number of bytes. */
 | 
				
			||||||
 | 
					            *pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Make callback to fill the buffer. */
 | 
				
			||||||
 | 
					            eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                *usLen += usRegCount * 2;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid request because the length is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegReadAddress;
 | 
				
			||||||
 | 
					    USHORT          usRegReadCount;
 | 
				
			||||||
 | 
					    USHORT          usRegWriteAddress;
 | 
				
			||||||
 | 
					    USHORT          usRegWriteCount;
 | 
				
			||||||
 | 
					    UCHAR           ucRegWriteByteCount;
 | 
				
			||||||
 | 
					    UCHAR          *pucFrameCur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
 | 
				
			||||||
 | 
					        usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegReadAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
 | 
				
			||||||
 | 
					        usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
 | 
				
			||||||
 | 
					        usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegWriteAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
 | 
				
			||||||
 | 
					        usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
 | 
				
			||||||
 | 
					            ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
 | 
				
			||||||
 | 
					            ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Make callback to update the register values. */
 | 
				
			||||||
 | 
					            eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
 | 
				
			||||||
 | 
					                                          usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if( eRegStatus == MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* Set the current PDU data pointer to the beginning. */
 | 
				
			||||||
 | 
					                pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
 | 
				
			||||||
 | 
					                *usLen = MB_PDU_FUNC_OFF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                /* First byte contains the function code. */
 | 
				
			||||||
 | 
					                *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
 | 
				
			||||||
 | 
					                *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                /* Second byte in the response contain the number of bytes. */
 | 
				
			||||||
 | 
					                *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
 | 
				
			||||||
 | 
					                *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                /* Make the read callback. */
 | 
				
			||||||
 | 
					                eRegStatus =
 | 
				
			||||||
 | 
					                    eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
 | 
				
			||||||
 | 
					                if( eRegStatus == MB_ENOERR )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    *usLen += 2 * usRegReadCount;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,121 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_ADDR_OFF           ( MB_PDU_DATA_OFF )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_REGCNT_OFF         ( MB_PDU_DATA_OFF + 2 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_SIZE               ( 4 )
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_REGCNT_MAX         ( 0x007D )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF    ( MB_PDU_DATA_OFF )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static functions ---------------------------------*/
 | 
				
			||||||
 | 
					eMBException    prveMBError2Exception( eMBErrorCode eErrorCode );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_INPUT_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usRegAddress;
 | 
				
			||||||
 | 
					    USHORT          usRegCount;
 | 
				
			||||||
 | 
					    UCHAR          *pucFrameCur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eMBException    eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eRegStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
 | 
				
			||||||
 | 
					        usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
 | 
				
			||||||
 | 
					        usRegAddress++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
 | 
				
			||||||
 | 
					        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. 
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        if( ( usRegCount >= 1 )
 | 
				
			||||||
 | 
					            && ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* Set the current PDU data pointer to the beginning. */
 | 
				
			||||||
 | 
					            pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
 | 
				
			||||||
 | 
					            *usLen = MB_PDU_FUNC_OFF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* First byte contains the function code. */
 | 
				
			||||||
 | 
					            *pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Second byte in the response contain the number of bytes. */
 | 
				
			||||||
 | 
					            *pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
 | 
				
			||||||
 | 
					            *usLen += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            eRegStatus =
 | 
				
			||||||
 | 
					                eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If an error occured convert it into a Modbus exception. */
 | 
				
			||||||
 | 
					            if( eRegStatus != MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eStatus = prveMBError2Exception( eRegStatus );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                *usLen += usRegCount * 2;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Can't be a valid read input register request because the length
 | 
				
			||||||
 | 
					         * is incorrect. */
 | 
				
			||||||
 | 
					        eStatus = MB_EX_ILLEGAL_DATA_VALUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,87 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static variables ---------------------------------*/
 | 
				
			||||||
 | 
					static UCHAR    ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
 | 
				
			||||||
 | 
					static USHORT   usMBSlaveIDLen;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
 | 
				
			||||||
 | 
					               UCHAR const *pucAdditional, USHORT usAdditionalLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* the first byte and second byte in the buffer is reserved for
 | 
				
			||||||
 | 
					     * the parameter ucSlaveID and the running flag. The rest of
 | 
				
			||||||
 | 
					     * the buffer is available for additional data. */
 | 
				
			||||||
 | 
					    if( usAdditionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usMBSlaveIDLen = 0;
 | 
				
			||||||
 | 
					        ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
 | 
				
			||||||
 | 
					        ucMBSlaveID[usMBSlaveIDLen++] = ( UCHAR )( xIsRunning ? 0xFF : 0x00 );
 | 
				
			||||||
 | 
					        if( usAdditionalLen > 0 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional,
 | 
				
			||||||
 | 
					                    ( size_t )usAdditionalLen );
 | 
				
			||||||
 | 
					            usMBSlaveIDLen += usAdditionalLen;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_ENORES;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
 | 
				
			||||||
 | 
					    *usLen = ( USHORT )( MB_PDU_DATA_OFF + usMBSlaveIDLen );
 | 
				
			||||||
 | 
					    return MB_EX_NONE;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,140 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define BITS_UCHAR      8U
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
 | 
				
			||||||
 | 
					                UCHAR ucValue )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usWordBuf;
 | 
				
			||||||
 | 
					    USHORT          usMask;
 | 
				
			||||||
 | 
					    USHORT          usByteOffset;
 | 
				
			||||||
 | 
					    USHORT          usNPreBits;
 | 
				
			||||||
 | 
					    USHORT          usValue = ucValue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert( ucNBits <= 8 );
 | 
				
			||||||
 | 
					    assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Calculate byte offset for first byte containing the bit values starting
 | 
				
			||||||
 | 
					     * at usBitOffset. */
 | 
				
			||||||
 | 
					    usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* How many bits precede our bits to set. */
 | 
				
			||||||
 | 
					    usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Move bit field into position over bits to set */
 | 
				
			||||||
 | 
					    usValue <<= usNPreBits;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Prepare a mask for setting the new bits. */
 | 
				
			||||||
 | 
					    usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
 | 
				
			||||||
 | 
					    usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* copy bits into temporary storage. */
 | 
				
			||||||
 | 
					    usWordBuf = ucByteBuf[usByteOffset];
 | 
				
			||||||
 | 
					    usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Zero out bit field bits and then or value bits into them. */
 | 
				
			||||||
 | 
					    usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* move bits back into storage */
 | 
				
			||||||
 | 
					    ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
 | 
				
			||||||
 | 
					    ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					UCHAR
 | 
				
			||||||
 | 
					xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    USHORT          usWordBuf;
 | 
				
			||||||
 | 
					    USHORT          usMask;
 | 
				
			||||||
 | 
					    USHORT          usByteOffset;
 | 
				
			||||||
 | 
					    USHORT          usNPreBits;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Calculate byte offset for first byte containing the bit values starting
 | 
				
			||||||
 | 
					     * at usBitOffset. */
 | 
				
			||||||
 | 
					    usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* How many bits precede our bits to set. */
 | 
				
			||||||
 | 
					    usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Prepare a mask for setting the new bits. */
 | 
				
			||||||
 | 
					    usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* copy bits into temporary storage. */
 | 
				
			||||||
 | 
					    usWordBuf = ucByteBuf[usByteOffset];
 | 
				
			||||||
 | 
					    usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* throw away unneeded bits. */
 | 
				
			||||||
 | 
					    usWordBuf >>= usNPreBits;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* mask away bits above the requested bitfield. */
 | 
				
			||||||
 | 
					    usWordBuf &= usMask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return ( UCHAR ) usWordBuf;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBException
 | 
				
			||||||
 | 
					prveMBError2Exception( eMBErrorCode eErrorCode )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBException    eStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch ( eErrorCode )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        case MB_ENOERR:
 | 
				
			||||||
 | 
					            eStatus = MB_EX_NONE;
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        case MB_ENOREG:
 | 
				
			||||||
 | 
					            eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        case MB_ETIMEDOUT:
 | 
				
			||||||
 | 
					            eStatus = MB_EX_SLAVE_BUSY;
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,416 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_H
 | 
				
			||||||
 | 
					#define _MB_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \defgroup modbus Modbus
 | 
				
			||||||
 | 
					 * \code #include "mb.h" \endcode
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This module defines the interface for the application. It contains
 | 
				
			||||||
 | 
					 * the basic functions and types required to use the Modbus protocol stack.
 | 
				
			||||||
 | 
					 * A typical application will want to call eMBInit() first. If the device
 | 
				
			||||||
 | 
					 * is ready to answer network requests it must then call eMBEnable() to activate
 | 
				
			||||||
 | 
					 * the protocol stack. In the main loop the function eMBPoll() must be called
 | 
				
			||||||
 | 
					 * periodically. The time interval between pooling depends on the configured
 | 
				
			||||||
 | 
					 * Modbus timeout. If an RTOS is available a separate task should be created
 | 
				
			||||||
 | 
					 * and the task should always call the function eMBPoll().
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \code
 | 
				
			||||||
 | 
					 * // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
 | 
				
			||||||
 | 
					 * eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
 | 
				
			||||||
 | 
					 * // Enable the Modbus Protocol Stack.
 | 
				
			||||||
 | 
					 * eMBEnable(  );
 | 
				
			||||||
 | 
					 * for( ;; )
 | 
				
			||||||
 | 
					 * {
 | 
				
			||||||
 | 
					 *     // Call the main polling loop of the Modbus protocol stack.
 | 
				
			||||||
 | 
					 *     eMBPoll(  );
 | 
				
			||||||
 | 
					 *     ...
 | 
				
			||||||
 | 
					 * }
 | 
				
			||||||
 | 
					 * \endcode
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Use the default Modbus TCP port (502)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define MB_TCP_PORT_USE_DEFAULT 0   
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Type definitions ---------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Modbus serial transmission modes (RTU/ASCII).
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
 | 
				
			||||||
 | 
					 * is faster but has more hardware requirements and requires a network with
 | 
				
			||||||
 | 
					 * a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					    typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    MB_RTU,                     /*!< RTU transmission mode. */
 | 
				
			||||||
 | 
					    MB_ASCII,                   /*!< ASCII transmission mode. */
 | 
				
			||||||
 | 
					    MB_TCP                      /*!< TCP mode. */
 | 
				
			||||||
 | 
					} eMBMode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief If register should be written or read.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This value is passed to the callback functions which support either
 | 
				
			||||||
 | 
					 * reading or writing register values. Writing means that the application
 | 
				
			||||||
 | 
					 * registers should be updated and reading means that the modbus protocol
 | 
				
			||||||
 | 
					 * stack needs to know the current register values.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and 
 | 
				
			||||||
 | 
					 *   eMBRegInputCB( ).
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    MB_REG_READ,                /*!< Read register values and pass to protocol stack. */
 | 
				
			||||||
 | 
					    MB_REG_WRITE                /*!< Update register values. */
 | 
				
			||||||
 | 
					} eMBRegisterMode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Errorcodes used by all function in the protocol stack.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    MB_ENOERR,                  /*!< no error. */
 | 
				
			||||||
 | 
					    MB_ENOREG,                  /*!< illegal register address. */
 | 
				
			||||||
 | 
					    MB_EINVAL,                  /*!< illegal argument. */
 | 
				
			||||||
 | 
					    MB_EPORTERR,                /*!< porting layer error. */
 | 
				
			||||||
 | 
					    MB_ENORES,                  /*!< insufficient resources. */
 | 
				
			||||||
 | 
					    MB_EIO,                     /*!< I/O error. */
 | 
				
			||||||
 | 
					    MB_EILLSTATE,               /*!< protocol stack in illegal state. */
 | 
				
			||||||
 | 
					    MB_ETIMEDOUT                /*!< timeout error occurred. */
 | 
				
			||||||
 | 
					} eMBErrorCode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Function prototypes ------------------------------*/
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Initialize the Modbus protocol stack.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This functions initializes the ASCII or RTU module and calls the
 | 
				
			||||||
 | 
					 * init functions of the porting layer to prepare the hardware. Please
 | 
				
			||||||
 | 
					 * note that the receiver is still disabled and no Modbus frames are
 | 
				
			||||||
 | 
					 * processed until eMBEnable( ) has been called.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param eMode If ASCII or RTU mode should be used.
 | 
				
			||||||
 | 
					 * \param ucSlaveAddress The slave address. Only frames sent to this
 | 
				
			||||||
 | 
					 *   address or to the broadcast address are processed.
 | 
				
			||||||
 | 
					 * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
 | 
				
			||||||
 | 
					 *   is platform dependent and some ports simply choose to ignore it.
 | 
				
			||||||
 | 
					 * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
 | 
				
			||||||
 | 
					 *   on the porting layer.
 | 
				
			||||||
 | 
					 * \param eParity Parity used for serial transmission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \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 
 | 
				
			||||||
 | 
					 *   is returned:
 | 
				
			||||||
 | 
					 *    - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
 | 
				
			||||||
 | 
					 *        slave addresses are in the range 1 - 247.
 | 
				
			||||||
 | 
					 *    - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
 | 
				
			||||||
 | 
					                         UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Initialize the Modbus protocol stack for Modbus TCP.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function initializes the Modbus TCP Module. Please note that
 | 
				
			||||||
 | 
					 * frame processing is still disabled until eMBEnable( ) is called.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param usTCPPort The TCP port to listen on.
 | 
				
			||||||
 | 
					 * \return If the protocol stack has been initialized correctly the function
 | 
				
			||||||
 | 
					 *   returns eMBErrorCode::MB_ENOERR. 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.
 | 
				
			||||||
 | 
					 *    - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBTCPInit( USHORT usTCPPort );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \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. 
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \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.
 | 
				
			||||||
 | 
					 *   If the protocol stack is not in the disabled state it returns
 | 
				
			||||||
 | 
					 *   eMBErrorCode::MB_EILLSTATE.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBClose( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Enable the Modbus protocol stack.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * 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 eMBErrorCode::MB_EILLSTATE.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBEnable( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Disable the Modbus protocol stack.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function disables processing of Modbus frames.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \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.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBDisable( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief The main pooling loop of the Modbus protocol stack.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * 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. 
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \return If the protocol stack is not in the enabled state the function
 | 
				
			||||||
 | 
					 *   returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns 
 | 
				
			||||||
 | 
					 *   eMBErrorCode::MB_ENOERR.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBPoll( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Configure the slave id of the device.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function should be called when the Modbus function <em>Report Slave ID</em>
 | 
				
			||||||
 | 
					 * is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
 | 
				
			||||||
 | 
					 *   <em>Report Slave ID</em> response.
 | 
				
			||||||
 | 
					 * \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
 | 
				
			||||||
 | 
					 *   otherwise the <em>Run Indicator Status</em> is 0x00.
 | 
				
			||||||
 | 
					 * \param pucAdditional Values which should be returned in the <em>Additional</em>
 | 
				
			||||||
 | 
					 *   bytes of the <em> Report Slave ID</em> response.
 | 
				
			||||||
 | 
					 * \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
 | 
				
			||||||
 | 
					 *   mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
 | 
				
			||||||
 | 
					 *   it returns eMBErrorCode::MB_ENOERR.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
 | 
				
			||||||
 | 
					                               UCHAR const *pucAdditional,
 | 
				
			||||||
 | 
					                               USHORT usAdditionalLen );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Registers a callback handler for a given function code.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function registers a new callback handler for a given function code.
 | 
				
			||||||
 | 
					 * 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. 
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param ucFunctionCode The Modbus function code for which this handler should
 | 
				
			||||||
 | 
					 *   be registers. Valid function codes are in the range 1 to 127.
 | 
				
			||||||
 | 
					 * \param pxHandler The function handler which should be called in case
 | 
				
			||||||
 | 
					 *   such a frame is received. If \c NULL a previously registered function handler
 | 
				
			||||||
 | 
					 *   for this function code is removed.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
 | 
				
			||||||
 | 
					 *   more resources are available it returns eMBErrorCode::MB_ENORES. In this
 | 
				
			||||||
 | 
					 *   case the values in mbconfig.h should be adjusted. If the argument was not
 | 
				
			||||||
 | 
					 *   valid it returns eMBErrorCode::MB_EINVAL.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRegisterCB( UCHAR ucFunctionCode, 
 | 
				
			||||||
 | 
					                               pxMBFunctionHandler pxHandler );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Callback -----------------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \defgroup modbus_registers Modbus Registers
 | 
				
			||||||
 | 
					 * \code #include "mb.h" \endcode
 | 
				
			||||||
 | 
					 * The protocol stack does not internally allocate any memory for the
 | 
				
			||||||
 | 
					 * registers. This makes the protocol stack very small and also usable on
 | 
				
			||||||
 | 
					 * low end targets. In addition the values don't have to be in the memory
 | 
				
			||||||
 | 
					 * and could for example be stored in a flash.<br>
 | 
				
			||||||
 | 
					 * Whenever the protocol stack requires a value it calls one of the callback
 | 
				
			||||||
 | 
					 * function with the register address and the number of registers to read
 | 
				
			||||||
 | 
					 * as an argument. The application should then read the actual register values
 | 
				
			||||||
 | 
					 * (for example the ADC voltage) and should store the result in the supplied
 | 
				
			||||||
 | 
					 * buffer.<br>
 | 
				
			||||||
 | 
					 * If the protocol stack wants to update a register value because a write
 | 
				
			||||||
 | 
					 * register function was received a buffer with the new register values is
 | 
				
			||||||
 | 
					 * passed to the callback function. The function should then use these values
 | 
				
			||||||
 | 
					 * to update the application register values.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus_registers
 | 
				
			||||||
 | 
					 * \brief Callback function used if the value of a <em>Input Register</em>
 | 
				
			||||||
 | 
					 *   is required by the protocol stack. The starting register address is given
 | 
				
			||||||
 | 
					 *   by \c usAddress and the last register is given by <tt>usAddress +
 | 
				
			||||||
 | 
					 *   usNRegs - 1</tt>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param pucRegBuffer A buffer where the callback function should write
 | 
				
			||||||
 | 
					 *   the current value of the modbus registers to.
 | 
				
			||||||
 | 
					 * \param usAddress The starting address of the register. Input registers
 | 
				
			||||||
 | 
					 *   are in the range 1 - 65535.
 | 
				
			||||||
 | 
					 * \param usNRegs Number of registers the callback function must supply.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \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 
 | 
				
			||||||
 | 
					 *       <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
 | 
				
			||||||
 | 
					 *       currently not available and the application dependent response
 | 
				
			||||||
 | 
					 *       timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
 | 
				
			||||||
 | 
					 *       exception is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
 | 
				
			||||||
 | 
					 *       a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
 | 
				
			||||||
 | 
					                               USHORT usNRegs );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus_registers
 | 
				
			||||||
 | 
					 * \brief Callback function used if a <em>Holding Register</em> value is
 | 
				
			||||||
 | 
					 *   read or written by the protocol stack. The starting register address
 | 
				
			||||||
 | 
					 *   is given by \c usAddress and the last register is given by
 | 
				
			||||||
 | 
					 *   <tt>usAddress + usNRegs - 1</tt>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param pucRegBuffer If the application registers values should be updated the
 | 
				
			||||||
 | 
					 *   buffer points to the new registers values. If the protocol stack needs
 | 
				
			||||||
 | 
					 *   to now the current values the callback function should write them into
 | 
				
			||||||
 | 
					 *   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 
 | 
				
			||||||
 | 
					 *   values should be updated from the values in the buffer. For example
 | 
				
			||||||
 | 
					 *   this would be the case when the Modbus master has issued an 
 | 
				
			||||||
 | 
					 *   <b>WRITE SINGLE REGISTER</b> command.
 | 
				
			||||||
 | 
					 *   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 
 | 
				
			||||||
 | 
					 *       <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
 | 
				
			||||||
 | 
					 *       currently not available and the application dependent response
 | 
				
			||||||
 | 
					 *       timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
 | 
				
			||||||
 | 
					 *       exception is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
 | 
				
			||||||
 | 
					 *       a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
 | 
				
			||||||
 | 
					                                 USHORT usNRegs, eMBRegisterMode eMode );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus_registers
 | 
				
			||||||
 | 
					 * \brief Callback function used if a <em>Coil Register</em> value is
 | 
				
			||||||
 | 
					 *   read or written by the protocol stack. If you are going to use
 | 
				
			||||||
 | 
					 *   this function you might use the functions xMBUtilSetBits(  ) and
 | 
				
			||||||
 | 
					 *   xMBUtilGetBits(  ) for working with bitfields.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param pucRegBuffer The bits are packed in bytes where the first coil
 | 
				
			||||||
 | 
					 *   starting at address \c usAddress is stored in the LSB of the
 | 
				
			||||||
 | 
					 *   first byte in the buffer <code>pucRegBuffer</code>.
 | 
				
			||||||
 | 
					 *   If the buffer should be written by the callback function unused
 | 
				
			||||||
 | 
					 *   coil values (I.e. if not a multiple of eight coils is used) should be set
 | 
				
			||||||
 | 
					 *   to zero.
 | 
				
			||||||
 | 
					 * \param usAddress The first coil number.
 | 
				
			||||||
 | 
					 * \param usNCoils Number of coil values requested.
 | 
				
			||||||
 | 
					 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
 | 
				
			||||||
 | 
					 *   be updated from the values supplied in the buffer \c pucRegBuffer.
 | 
				
			||||||
 | 
					 *   If eMBRegisterMode::MB_REG_READ the application should store the current
 | 
				
			||||||
 | 
					 *   values in 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 does not map an coils
 | 
				
			||||||
 | 
					 *       within the requested address range. In this case a 
 | 
				
			||||||
 | 
					 *       <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
 | 
				
			||||||
 | 
					 *       currently not available and the application dependent response
 | 
				
			||||||
 | 
					 *       timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
 | 
				
			||||||
 | 
					 *       exception is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
 | 
				
			||||||
 | 
					 *       a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
 | 
				
			||||||
 | 
					                               USHORT usNCoils, eMBRegisterMode eMode );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus_registers
 | 
				
			||||||
 | 
					 * \brief Callback function used if a <em>Input Discrete Register</em> value is
 | 
				
			||||||
 | 
					 *   read by the protocol stack.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * If you are going to use his function you might use the functions
 | 
				
			||||||
 | 
					 * xMBUtilSetBits(  ) and xMBUtilGetBits(  ) for working with bitfields.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param pucRegBuffer The buffer should be updated with the current
 | 
				
			||||||
 | 
					 *   coil values. The first discrete input starting at \c usAddress must be
 | 
				
			||||||
 | 
					 *   stored at the LSB of the first byte in the buffer. If the requested number
 | 
				
			||||||
 | 
					 *   is not a multiple of eight the remaining bits should be set to zero.
 | 
				
			||||||
 | 
					 * \param usAddress The starting address of the first discrete input.
 | 
				
			||||||
 | 
					 * \param usNDiscrete Number of discrete input values.
 | 
				
			||||||
 | 
					 * \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 no such discrete inputs exists.
 | 
				
			||||||
 | 
					 *       In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent 
 | 
				
			||||||
 | 
					 *       as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
 | 
				
			||||||
 | 
					 *       currently not available and the application dependent response
 | 
				
			||||||
 | 
					 *       timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
 | 
				
			||||||
 | 
					 *       exception is sent as a response.
 | 
				
			||||||
 | 
					 *   - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
 | 
				
			||||||
 | 
					 *       a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
 | 
				
			||||||
 | 
					                                  USHORT usNDiscrete );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,131 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_CONFIG_H
 | 
				
			||||||
 | 
					#define _MB_CONFIG_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					/*! \defgroup modbus_cfg Modbus Configuration
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Most modules in the protocol stack are completly optional and can be
 | 
				
			||||||
 | 
					 * excluded. This is specially important if target resources are very small
 | 
				
			||||||
 | 
					 * and program memory space should be saved.<br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * All of these settings are available in the file <code>mbconfig.h</code>
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/*! \addtogroup modbus_cfg
 | 
				
			||||||
 | 
					 *  @{
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/*! \brief If Modbus ASCII support is enabled. */
 | 
				
			||||||
 | 
					#define MB_ASCII_ENABLED                        (  0 )//gai
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If Modbus RTU support is enabled. */
 | 
				
			||||||
 | 
					#define MB_RTU_ENABLED                          (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If Modbus TCP support is enabled. */
 | 
				
			||||||
 | 
					#define MB_TCP_ENABLED                          (  0 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief The character timeout value for Modbus ASCII.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The character timeout value is not fixed for Modbus ASCII and is therefore
 | 
				
			||||||
 | 
					 * a configuration option. It should be set to the maximum expected delay
 | 
				
			||||||
 | 
					 * time of the network.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define MB_ASCII_TIMEOUT_SEC                    (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief Timeout to wait in ASCII prior to enabling transmitter.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * If defined the function calls vMBPortSerialDelay with the argument
 | 
				
			||||||
 | 
					 * MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS to allow for a delay before
 | 
				
			||||||
 | 
					 * the serial transmitter is enabled. This is required because some
 | 
				
			||||||
 | 
					 * targets are so fast that there is no time between receiving and
 | 
				
			||||||
 | 
					 * transmitting the frame. If the master is to slow with enabling its 
 | 
				
			||||||
 | 
					 * receiver then he will not receive the response correctly.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#ifndef MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
 | 
				
			||||||
 | 
					#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS    ( 0 )
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief Maximum number of Modbus functions codes the protocol stack
 | 
				
			||||||
 | 
					 *    should support.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The maximum number of supported Modbus functions must be greater than
 | 
				
			||||||
 | 
					 * the sum of all enabled functions in this file and custom function
 | 
				
			||||||
 | 
					 * handlers. If set to small adding more functions will fail.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define MB_FUNC_HANDLERS_MAX                    ( 16 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief Number of bytes which should be allocated for the <em>Report Slave ID
 | 
				
			||||||
 | 
					 *    </em>command.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This number limits the maximum size of the additional segment in the
 | 
				
			||||||
 | 
					 * report slave id function. See eMBSetSlaveID(  ) for more information on
 | 
				
			||||||
 | 
					 * how to set this value. It is only used if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
 | 
				
			||||||
 | 
					 * is set to <code>1</code>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define MB_FUNC_OTHER_REP_SLAVEID_BUF           ( 32 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Report Slave ID</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_OTHER_REP_SLAVEID_ENABLED       (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Read Input Registers</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_INPUT_ENABLED              (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Read Holding Registers</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_HOLDING_ENABLED            (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Write Single Register</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_HOLDING_ENABLED           (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Write Multiple registers</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED  (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Read Coils</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_COILS_ENABLED              (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Write Coils</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_COIL_ENABLED              (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Write Multiple Coils</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED    (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Read Discrete Inputs</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED    (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief If the <em>Read/Write Multiple Registers</em> function should be enabled. */
 | 
				
			||||||
 | 
					#define MB_FUNC_READWRITE_HOLDING_ENABLED       (  1 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! @} */
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					    PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,86 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_FRAME_H
 | 
				
			||||||
 | 
					#define _MB_FRAME_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*!
 | 
				
			||||||
 | 
					 * Constants which defines the format of a modbus frame. The example is
 | 
				
			||||||
 | 
					 * shown for a Modbus RTU/ASCII frame. Note that the Modbus PDU is not
 | 
				
			||||||
 | 
					 * dependent on the underlying transport.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * <code>
 | 
				
			||||||
 | 
					 * <------------------------ MODBUS SERIAL LINE PDU (1) ------------------->
 | 
				
			||||||
 | 
					 *              <----------- MODBUS PDU (1') ---------------->
 | 
				
			||||||
 | 
					 *  +-----------+---------------+----------------------------+-------------+
 | 
				
			||||||
 | 
					 *  | Address   | Function Code | Data                       | CRC/LRC     |
 | 
				
			||||||
 | 
					 *  +-----------+---------------+----------------------------+-------------+
 | 
				
			||||||
 | 
					 *  |           |               |                                   |
 | 
				
			||||||
 | 
					 * (2)        (3/2')           (3')                                (4)
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * (1)  ... MB_SER_PDU_SIZE_MAX = 256
 | 
				
			||||||
 | 
					 * (2)  ... MB_SER_PDU_ADDR_OFF = 0
 | 
				
			||||||
 | 
					 * (3)  ... MB_SER_PDU_PDU_OFF  = 1
 | 
				
			||||||
 | 
					 * (4)  ... MB_SER_PDU_SIZE_CRC = 2
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * (1') ... MB_PDU_SIZE_MAX     = 253
 | 
				
			||||||
 | 
					 * (2') ... MB_PDU_FUNC_OFF     = 0
 | 
				
			||||||
 | 
					 * (3') ... MB_PDU_DATA_OFF     = 1
 | 
				
			||||||
 | 
					 * </code>
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_PDU_SIZE_MAX     253 /*!< Maximum size of a PDU. */
 | 
				
			||||||
 | 
					#define MB_PDU_SIZE_MIN     1   /*!< Function Code */
 | 
				
			||||||
 | 
					#define MB_PDU_FUNC_OFF     0   /*!< Offset of function code in PDU. */
 | 
				
			||||||
 | 
					#define MB_PDU_DATA_OFF     1   /*!< Offset for response data in PDU. */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Prototypes  0-------------------------------------*/
 | 
				
			||||||
 | 
					typedef void    ( *pvMBFrameStart ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef void    ( *pvMBFrameStop ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef eMBErrorCode( *peMBFrameReceive ) ( UCHAR * pucRcvAddress,
 | 
				
			||||||
 | 
					                                            UCHAR ** pucFrame,
 | 
				
			||||||
 | 
					                                            USHORT * pusLength );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef eMBErrorCode( *peMBFrameSend ) ( UCHAR slaveAddress,
 | 
				
			||||||
 | 
					                                         const UCHAR * pucFrame,
 | 
				
			||||||
 | 
					                                         USHORT usLength );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef void( *pvMBFrameClose ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,79 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					#ifndef _MB_FUNC_H
 | 
				
			||||||
 | 
					#define _MB_FUNC_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_OTHER_REP_SLAVEID_BUF > 0
 | 
				
			||||||
 | 
					    eMBException eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_INPUT_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_COIL_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					eMBException    eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,128 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_PORT_H
 | 
				
			||||||
 | 
					#define _MB_PORT_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Type definitions ---------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    EV_READY,                   /*!< Startup finished. */
 | 
				
			||||||
 | 
					    EV_FRAME_RECEIVED,          /*!< Frame received. */
 | 
				
			||||||
 | 
					    EV_EXECUTE,                 /*!< Execute function. */
 | 
				
			||||||
 | 
					    EV_FRAME_SENT               /*!< Frame sent. */
 | 
				
			||||||
 | 
					} eMBEventType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \ingroup modbus
 | 
				
			||||||
 | 
					 * \brief Parity used for characters in serial mode.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The parity which should be applied to the characters sent over the serial
 | 
				
			||||||
 | 
					 * link. Please note that this values are actually passed to the porting
 | 
				
			||||||
 | 
					 * layer and therefore not all parity modes might be available.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    MB_PAR_NONE,                /*!< No parity. */
 | 
				
			||||||
 | 
					    MB_PAR_ODD,                 /*!< Odd parity. */
 | 
				
			||||||
 | 
					    MB_PAR_EVEN                 /*!< Even parity. */
 | 
				
			||||||
 | 
					} eMBParity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Supporting functions -----------------------------*/
 | 
				
			||||||
 | 
					BOOL            xMBPortEventInit( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBPortEventPost( eMBEventType eEvent );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBPortEventGet(  /*@out@ */ eMBEventType * eEvent );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Serial port functions ----------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
 | 
				
			||||||
 | 
					                                   UCHAR ucDataBits, eMBParity eParity );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBPortClose( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            xMBPortSerialClose( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBPortSerialGetByte( CHAR * pucByte );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBPortSerialPutByte( CHAR ucByte );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Timers functions ---------------------------------*/
 | 
				
			||||||
 | 
					BOOL            xMBPortTimersInit( USHORT usTimeOut50us );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            xMBPortTimersClose( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBPortTimersEnable( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBPortTimersDisable( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBPortTimersDelay( USHORT usTimeOutMS );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Callback for the protocol stack ------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*!
 | 
				
			||||||
 | 
					 * \brief Callback function for the porting layer when a new byte is
 | 
				
			||||||
 | 
					 *   available.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Depending upon the mode this callback function is used by the RTU or
 | 
				
			||||||
 | 
					 * ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
 | 
				
			||||||
 | 
					 * must immediately return a new character.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \return <code>TRUE</code> if a event was posted to the queue because
 | 
				
			||||||
 | 
					 *   a new byte was received. The port implementation should wake up the
 | 
				
			||||||
 | 
					 *   tasks which are currently blocked on the eventqueue.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					extern          BOOL( *pxMBFrameCBByteReceived ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern          BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern          BOOL( *pxMBPortCBTimerExpired ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- TCP port functions -------------------------------*/
 | 
				
			||||||
 | 
					BOOL            xMBTCPPortInit( USHORT usTCPPort );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBTCPPortClose( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void            vMBTCPPortDisable( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL            xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,82 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_PROTO_H
 | 
				
			||||||
 | 
					#define _MB_PROTO_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_ADDRESS_BROADCAST    ( 0 )   /*! Modbus broadcast address. */
 | 
				
			||||||
 | 
					#define MB_ADDRESS_MIN          ( 1 )   /*! Smallest possible slave address. */
 | 
				
			||||||
 | 
					#define MB_ADDRESS_MAX          ( 247 ) /*! Biggest possible slave address. */
 | 
				
			||||||
 | 
					#define MB_FUNC_NONE                          (  0 )
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_COILS                    (  1 )
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_DISCRETE_INPUTS          (  2 )
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_SINGLE_COIL             (  5 )
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_MULTIPLE_COILS          ( 15 )
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_HOLDING_REGISTER         (  3 )
 | 
				
			||||||
 | 
					#define MB_FUNC_READ_INPUT_REGISTER           (  4 )
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_REGISTER                (  6 )
 | 
				
			||||||
 | 
					#define MB_FUNC_WRITE_MULTIPLE_REGISTERS      ( 16 )
 | 
				
			||||||
 | 
					#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS  ( 23 )
 | 
				
			||||||
 | 
					#define MB_FUNC_DIAG_READ_EXCEPTION           (  7 )
 | 
				
			||||||
 | 
					#define MB_FUNC_DIAG_DIAGNOSTIC               (  8 )
 | 
				
			||||||
 | 
					#define MB_FUNC_DIAG_GET_COM_EVENT_CNT        ( 11 )
 | 
				
			||||||
 | 
					#define MB_FUNC_DIAG_GET_COM_EVENT_LOG        ( 12 )
 | 
				
			||||||
 | 
					#define MB_FUNC_OTHER_REPORT_SLAVEID          ( 17 )
 | 
				
			||||||
 | 
					#define MB_FUNC_ERROR                         ( 128 )
 | 
				
			||||||
 | 
					/* ----------------------- Type definitions ---------------------------------*/
 | 
				
			||||||
 | 
					    typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    MB_EX_NONE = 0x00,
 | 
				
			||||||
 | 
					    MB_EX_ILLEGAL_FUNCTION = 0x01,
 | 
				
			||||||
 | 
					    MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
 | 
				
			||||||
 | 
					    MB_EX_ILLEGAL_DATA_VALUE = 0x03,
 | 
				
			||||||
 | 
					    MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
 | 
				
			||||||
 | 
					    MB_EX_ACKNOWLEDGE = 0x05,
 | 
				
			||||||
 | 
					    MB_EX_SLAVE_BUSY = 0x06,
 | 
				
			||||||
 | 
					    MB_EX_MEMORY_PARITY_ERROR = 0x08,
 | 
				
			||||||
 | 
					    MB_EX_GATEWAY_PATH_FAILED = 0x0A,
 | 
				
			||||||
 | 
					    MB_EX_GATEWAY_TGT_FAILED = 0x0B
 | 
				
			||||||
 | 
					} eMBException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef         eMBException( *pxMBFunctionHandler ) ( UCHAR * pucFrame, USHORT * pusLength );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    UCHAR           ucFunctionCode;
 | 
				
			||||||
 | 
					    pxMBFunctionHandler pxHandler;
 | 
				
			||||||
 | 
					} xMBFunctionHandler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,107 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_UTILS_H
 | 
				
			||||||
 | 
					#define _MB_UTILS_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					/*! \defgroup modbus_utils Utilities
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This module contains some utility functions which can be used by
 | 
				
			||||||
 | 
					 * the application. It includes some special functions for working with
 | 
				
			||||||
 | 
					 * bitfields backed by a character array buffer.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/*! \addtogroup modbus_utils
 | 
				
			||||||
 | 
					 *  @{
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/*! \brief Function to set bits in a byte buffer.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function allows the efficient use of an array to implement bitfields.
 | 
				
			||||||
 | 
					 * The array used for storing the bits must always be a multiple of two
 | 
				
			||||||
 | 
					 * bytes. Up to eight bits can be set or cleared in one operation.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param ucByteBuf A buffer where the bit values are stored. Must be a
 | 
				
			||||||
 | 
					 *   multiple of 2 bytes. No length checking is performed and if
 | 
				
			||||||
 | 
					 *   usBitOffset / 8 is greater than the size of the buffer memory contents
 | 
				
			||||||
 | 
					 *   is overwritten.
 | 
				
			||||||
 | 
					 * \param usBitOffset The starting address of the bits to set. The first
 | 
				
			||||||
 | 
					 *   bit has the offset 0.
 | 
				
			||||||
 | 
					 * \param ucNBits Number of bits to modify. The value must always be smaller
 | 
				
			||||||
 | 
					 *   than 8.
 | 
				
			||||||
 | 
					 * \param ucValues Thew new values for the bits. The value for the first bit
 | 
				
			||||||
 | 
					 *   starting at <code>usBitOffset</code> is the LSB of the value
 | 
				
			||||||
 | 
					 *   <code>ucValues</code>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \code
 | 
				
			||||||
 | 
					 * ucBits[2] = {0, 0};
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
 | 
				
			||||||
 | 
					 * xMBUtilSetBits( ucBits, 4, 1, 1 );
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * // Set bit 7 to 1 and bit 8 to 0.
 | 
				
			||||||
 | 
					 * xMBUtilSetBits( ucBits, 7, 2, 0x01 );
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
 | 
				
			||||||
 | 
					 * xMBUtilSetBits( ucBits, 8, 8, 0x5A);
 | 
				
			||||||
 | 
					 * \endcode
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void            xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
 | 
				
			||||||
 | 
					                                UCHAR ucNBits, UCHAR ucValues );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! \brief Function to read bits in a byte buffer.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function is used to extract up bit values from an array. Up to eight
 | 
				
			||||||
 | 
					 * bit values can be extracted in one step.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \param ucByteBuf A buffer where the bit values are stored.
 | 
				
			||||||
 | 
					 * \param usBitOffset The starting address of the bits to set. The first
 | 
				
			||||||
 | 
					 *   bit has the offset 0.
 | 
				
			||||||
 | 
					 * \param ucNBits Number of bits to modify. The value must always be smaller
 | 
				
			||||||
 | 
					 *   than 8.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \code
 | 
				
			||||||
 | 
					 * UCHAR ucBits[2] = {0, 0};
 | 
				
			||||||
 | 
					 * UCHAR ucResult;
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * // Extract the bits 3 - 10.
 | 
				
			||||||
 | 
					 * ucResult = xMBUtilGetBits( ucBits, 3, 8 );
 | 
				
			||||||
 | 
					 * \endcode
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					UCHAR           xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
 | 
				
			||||||
 | 
					                                UCHAR ucNBits );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*! @} */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,411 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbproto.h"
 | 
				
			||||||
 | 
					#include "mbfunc.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					#if MB_RTU_ENABLED == 1
 | 
				
			||||||
 | 
					#include "mbrtu.h"
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_ASCII_ENABLED == 1
 | 
				
			||||||
 | 
					#include "mbascii.h"
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_TCP_ENABLED == 1
 | 
				
			||||||
 | 
					#include "mbtcp.h"
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef MB_PORT_HAS_CLOSE
 | 
				
			||||||
 | 
					#define MB_PORT_HAS_CLOSE 0
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static variables ---------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static UCHAR    ucMBAddress;
 | 
				
			||||||
 | 
					static eMBMode  eMBCurrentMode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    STATE_ENABLED,
 | 
				
			||||||
 | 
					    STATE_DISABLED,
 | 
				
			||||||
 | 
					    STATE_NOT_INITIALIZED
 | 
				
			||||||
 | 
					} eMBState = STATE_NOT_INITIALIZED;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Functions pointer which are initialized in eMBInit( ). Depending on the
 | 
				
			||||||
 | 
					 * mode (RTU or ASCII) the are set to the correct implementations.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static peMBFrameSend peMBFrameSendCur;
 | 
				
			||||||
 | 
					static pvMBFrameStart pvMBFrameStartCur;
 | 
				
			||||||
 | 
					static pvMBFrameStop pvMBFrameStopCur;
 | 
				
			||||||
 | 
					static peMBFrameReceive peMBFrameReceiveCur;
 | 
				
			||||||
 | 
					static pvMBFrameClose pvMBFrameCloseCur;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Callback functions required by the porting layer. They are called when
 | 
				
			||||||
 | 
					 * an external event has happend which includes a timeout or the reception
 | 
				
			||||||
 | 
					 * or transmission of a character.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					BOOL( *pxMBFrameCBByteReceived ) ( void );
 | 
				
			||||||
 | 
					BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
 | 
				
			||||||
 | 
					BOOL( *pxMBPortCBTimerExpired ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
 | 
				
			||||||
 | 
					BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* An array of Modbus functions handlers which associates Modbus function
 | 
				
			||||||
 | 
					 * codes with implementing functions.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
 | 
				
			||||||
 | 
					#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_INPUT_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_READ_COILS, eMBFuncReadCoils},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_COIL_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
 | 
				
			||||||
 | 
					    {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* check preconditions */
 | 
				
			||||||
 | 
					    if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
 | 
				
			||||||
 | 
					        ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EINVAL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ucMBAddress = ucSlaveAddress;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        switch ( eMode )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					#if MB_RTU_ENABLED > 0
 | 
				
			||||||
 | 
					        case MB_RTU:
 | 
				
			||||||
 | 
					            pvMBFrameStartCur = eMBRTUStart;//回调
 | 
				
			||||||
 | 
					            pvMBFrameStopCur = eMBRTUStop;
 | 
				
			||||||
 | 
					            peMBFrameSendCur = eMBRTUSend;
 | 
				
			||||||
 | 
					            peMBFrameReceiveCur = eMBRTUReceive;
 | 
				
			||||||
 | 
					            pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
 | 
				
			||||||
 | 
					            pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
 | 
				
			||||||
 | 
					            pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
 | 
				
			||||||
 | 
					            pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if MB_ASCII_ENABLED > 0
 | 
				
			||||||
 | 
					        case MB_ASCII:
 | 
				
			||||||
 | 
					            pvMBFrameStartCur = eMBASCIIStart;
 | 
				
			||||||
 | 
					            pvMBFrameStopCur = eMBASCIIStop;
 | 
				
			||||||
 | 
					            peMBFrameSendCur = eMBASCIISend;
 | 
				
			||||||
 | 
					            peMBFrameReceiveCur = eMBASCIIReceive;
 | 
				
			||||||
 | 
					            pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
 | 
				
			||||||
 | 
					            pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
 | 
				
			||||||
 | 
					            pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
 | 
				
			||||||
 | 
					            pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            eStatus = MB_EINVAL;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if( eStatus == MB_ENOERR )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if( !xMBPortEventInit(  ) )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* port dependent event module initalization failed. */
 | 
				
			||||||
 | 
					                eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                eMBCurrentMode = eMode;
 | 
				
			||||||
 | 
					                eMBState = STATE_DISABLED;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_TCP_ENABLED > 0
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBTCPInit( USHORT ucTCPPort )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eMBState = STATE_DISABLED;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if( !xMBPortEventInit(  ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Port dependent event module initalization failed. */
 | 
				
			||||||
 | 
					        eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        pvMBFrameStartCur = eMBTCPStart;
 | 
				
			||||||
 | 
					        pvMBFrameStopCur = eMBTCPStop;
 | 
				
			||||||
 | 
					        peMBFrameReceiveCur = eMBTCPReceive;
 | 
				
			||||||
 | 
					        peMBFrameSendCur = eMBTCPSend;
 | 
				
			||||||
 | 
					        pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
 | 
				
			||||||
 | 
					        ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
 | 
				
			||||||
 | 
					        eMBCurrentMode = MB_TCP;
 | 
				
			||||||
 | 
					        eMBState = STATE_DISABLED;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int             i;
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					        if( pxHandler != NULL )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if( ( xFuncHandlers[i].pxHandler == NULL ) ||
 | 
				
			||||||
 | 
					                    ( xFuncHandlers[i].pxHandler == pxHandler ) )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
 | 
				
			||||||
 | 
					                    xFuncHandlers[i].pxHandler = pxHandler;
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    xFuncHandlers[i].ucFunctionCode = 0;
 | 
				
			||||||
 | 
					                    xFuncHandlers[i].pxHandler = NULL;
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            /* Remove can't fail. */
 | 
				
			||||||
 | 
					            eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EINVAL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBClose( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( eMBState == STATE_DISABLED )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if( pvMBFrameCloseCur != NULL )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            pvMBFrameCloseCur(  );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EILLSTATE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBEnable( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( eMBState == STATE_DISABLED )//init完成
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Activate the protocol stack. */
 | 
				
			||||||
 | 
					        pvMBFrameStartCur(  );//函数指针,在init中指向了eMBRTUStart
 | 
				
			||||||
 | 
					        eMBState = STATE_ENABLED;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EILLSTATE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBDisable( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( eMBState == STATE_ENABLED )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        pvMBFrameStopCur(  );
 | 
				
			||||||
 | 
					        eMBState = STATE_DISABLED;
 | 
				
			||||||
 | 
					        eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if( eMBState == STATE_DISABLED )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EILLSTATE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBPoll( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    static UCHAR   *ucMBFrame;
 | 
				
			||||||
 | 
					    static UCHAR    ucRcvAddress;
 | 
				
			||||||
 | 
					    static UCHAR    ucFunctionCode;
 | 
				
			||||||
 | 
					    static USHORT   usLength;
 | 
				
			||||||
 | 
					    static eMBException eException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int             i;
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    eMBEventType    eEvent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Check if the protocol stack is ready. */
 | 
				
			||||||
 | 
					    if( eMBState != STATE_ENABLED )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return MB_EILLSTATE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Check if there is a event available. If not return control to caller.
 | 
				
			||||||
 | 
					     * Otherwise we will handle the event. */
 | 
				
			||||||
 | 
					    if( xMBPortEventGet( &eEvent ) == TRUE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        switch ( eEvent )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					        case EV_READY:
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        case EV_FRAME_RECEIVED:
 | 
				
			||||||
 | 
					            eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
 | 
				
			||||||
 | 
					            if( eStatus == MB_ENOERR )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* Check if the frame is for us. If not ignore the frame. */
 | 
				
			||||||
 | 
					                if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    ( void )xMBPortEventPost( EV_EXECUTE );
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        case EV_EXECUTE:
 | 
				
			||||||
 | 
					            ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
 | 
				
			||||||
 | 
					            eException = MB_EX_ILLEGAL_FUNCTION;
 | 
				
			||||||
 | 
					            for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                /* No more function handlers registered. Abort. */
 | 
				
			||||||
 | 
					                if( xFuncHandlers[i].ucFunctionCode == 0 )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* If the request was not sent to the broadcast address we
 | 
				
			||||||
 | 
					             * return a reply. */
 | 
				
			||||||
 | 
					            if( ucRcvAddress != MB_ADDRESS_BROADCAST )
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if( eException != MB_EX_NONE )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    /* An exception occured. Build an error frame. */
 | 
				
			||||||
 | 
					                    usLength = 0;
 | 
				
			||||||
 | 
					                    ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
 | 
				
			||||||
 | 
					                    ucMBFrame[usLength++] = eException;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
 | 
				
			||||||
 | 
					                }                
 | 
				
			||||||
 | 
					                eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        case EV_FRAME_SENT:
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return MB_ENOERR;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,97 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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, 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,
 | 
				
			||||||
 | 
					    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, 0x01, 0xC0, 0x80, 0x41, 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, 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, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
 | 
				
			||||||
 | 
					    0x00, 0xC1, 0x81, 0x40
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const UCHAR aucCRCLo[] = {
 | 
				
			||||||
 | 
					    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
 | 
				
			||||||
 | 
					    0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
 | 
				
			||||||
 | 
					    0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
 | 
				
			||||||
 | 
					    0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
 | 
				
			||||||
 | 
					    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, 
 | 
				
			||||||
 | 
					    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, 
 | 
				
			||||||
 | 
					    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,
 | 
				
			||||||
 | 
					    0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
 | 
				
			||||||
 | 
					    0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
 | 
				
			||||||
 | 
					    0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
 | 
				
			||||||
 | 
					    0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
 | 
				
			||||||
 | 
					    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
 | 
				
			||||||
 | 
					    0x41, 0x81, 0x80, 0x40
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					USHORT
 | 
				
			||||||
 | 
					usMBCRC16( UCHAR * pucFrame, USHORT usLen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    UCHAR           ucCRCHi = 0xFF;
 | 
				
			||||||
 | 
					    UCHAR           ucCRCLo = 0xFF;
 | 
				
			||||||
 | 
					    int             iIndex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while( usLen-- )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        iIndex = ucCRCLo ^ *( pucFrame++ );
 | 
				
			||||||
 | 
					        ucCRCLo = ( UCHAR )( ucCRCHi ^ aucCRCHi[iIndex] );
 | 
				
			||||||
 | 
					        ucCRCHi = aucCRCLo[iIndex];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return ( USHORT )( ucCRCHi << 8 | ucCRCLo );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,35 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_CRC_H
 | 
				
			||||||
 | 
					#define _MB_CRC_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					USHORT          usMBCRC16( UCHAR * pucFrame, USHORT usLen );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,354 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbrtu.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mbcrc.h"
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_SER_PDU_SIZE_MIN     4       /*!< Minimum size of a Modbus RTU frame. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_SIZE_MAX     256     /*!< Maximum size of a Modbus RTU frame. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_SIZE_CRC     2       /*!< Size of CRC field in PDU. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_ADDR_OFF     0       /*!< Offset of slave address in Ser-PDU. */
 | 
				
			||||||
 | 
					#define MB_SER_PDU_PDU_OFF      1       /*!< Offset of Modbus-PDU in Ser-PDU. */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Type definitions ---------------------------------*/
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    STATE_RX_INIT,              /*!< Receiver is in initial state. */
 | 
				
			||||||
 | 
					    STATE_RX_IDLE,              /*!< Receiver is in idle state. */
 | 
				
			||||||
 | 
					    STATE_RX_RCV,               /*!< Frame is beeing received. */
 | 
				
			||||||
 | 
					    STATE_RX_ERROR              /*!< If the frame is invalid. */
 | 
				
			||||||
 | 
					} eMBRcvState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    STATE_TX_IDLE,              /*!< Transmitter is in idle state. */
 | 
				
			||||||
 | 
					    STATE_TX_XMIT               /*!< Transmitter is in transfer state. */
 | 
				
			||||||
 | 
					} eMBSndState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Static variables ---------------------------------*/
 | 
				
			||||||
 | 
					static volatile eMBSndState eSndState;
 | 
				
			||||||
 | 
					static volatile eMBRcvState eRcvState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					volatile UCHAR  ucRTUBuf[MB_SER_PDU_SIZE_MAX];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static volatile UCHAR *pucSndBufferCur;
 | 
				
			||||||
 | 
					static volatile USHORT usSndBufferCount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static volatile USHORT usRcvBufferPos;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    ULONG           usTimerT35_50us;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ( void )ucSlaveAddress;
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Modbus RTU uses 8 Databits. */
 | 
				
			||||||
 | 
					    if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* If baudrate > 19200 then we should use the fixed timer values
 | 
				
			||||||
 | 
					         * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        if( ulBaudRate > 19200 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            usTimerT35_50us = 35;       /* 1800us. */
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            /* The timer reload value for a character is given by:
 | 
				
			||||||
 | 
					             *
 | 
				
			||||||
 | 
					             * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
 | 
				
			||||||
 | 
					             *             = 11 * Ticks_per_1s / Baudrate
 | 
				
			||||||
 | 
					             *             = 220000 / Baudrate
 | 
				
			||||||
 | 
					             * The reload for t3.5 is 1.5 times this value and similary
 | 
				
			||||||
 | 
					             * for t3.5.
 | 
				
			||||||
 | 
					             */
 | 
				
			||||||
 | 
					            usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					eMBRTUStart( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    /* Initially the receiver is in the state STATE_RX_INIT. we start
 | 
				
			||||||
 | 
					     * the timer and if no character is received within t3.5 we change
 | 
				
			||||||
 | 
					     * to STATE_RX_IDLE. This makes sure that we delay startup of the
 | 
				
			||||||
 | 
					     * modbus protocol stack until the bus is free.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    eRcvState = STATE_RX_INIT;//初始化状态
 | 
				
			||||||
 | 
					    vMBPortSerialEnable( TRUE, FALSE );//使能接收,禁止发送
 | 
				
			||||||
 | 
					    vMBPortTimersEnable(  );//启动定时器
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					eMBRTUStop( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    vMBPortSerialEnable( FALSE, FALSE );
 | 
				
			||||||
 | 
					    vMBPortTimersDisable(  );
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BOOL            xFrameReceived = FALSE;
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Length and CRC check */
 | 
				
			||||||
 | 
					    if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
 | 
				
			||||||
 | 
					        && ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Save the address field. All frames are passed to the upper layed
 | 
				
			||||||
 | 
					         * and the decision if a frame is used is done there.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
 | 
				
			||||||
 | 
					         * size of address field and CRC checksum.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        *pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Return the start of the Modbus PDU to the caller. */
 | 
				
			||||||
 | 
					        *pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
 | 
				
			||||||
 | 
					        xFrameReceived = TRUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    USHORT          usCRC16;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ENTER_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Check if the receiver is still in idle state. If not we where to
 | 
				
			||||||
 | 
					     * slow with processing the received frame and the master sent another
 | 
				
			||||||
 | 
					     * frame on the network. We have to abort sending the frame.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    if( eRcvState == STATE_RX_IDLE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* First byte before the Modbus-PDU is the slave address. */
 | 
				
			||||||
 | 
					        pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
 | 
				
			||||||
 | 
					        usSndBufferCount = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
 | 
				
			||||||
 | 
					        pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
 | 
				
			||||||
 | 
					        usSndBufferCount += usLength;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
 | 
				
			||||||
 | 
					        usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
 | 
				
			||||||
 | 
					        ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
 | 
				
			||||||
 | 
					        ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Activate the transmitter. */
 | 
				
			||||||
 | 
					        eSndState = STATE_TX_XMIT;
 | 
				
			||||||
 | 
					        vMBPortSerialEnable( FALSE, TRUE );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    EXIT_CRITICAL_SECTION(  );
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL
 | 
				
			||||||
 | 
					xMBRTUReceiveFSM( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BOOL            xTaskNeedSwitch = FALSE;
 | 
				
			||||||
 | 
					    UCHAR           ucByte;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert( eSndState == STATE_TX_IDLE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Always read the character. */
 | 
				
			||||||
 | 
					    ( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch ( eRcvState )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* If we have received a character in the init state we have to
 | 
				
			||||||
 | 
					         * wait until the frame is finished.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    case STATE_RX_INIT:
 | 
				
			||||||
 | 
					        vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* In the error state we wait until all characters in the
 | 
				
			||||||
 | 
					         * damaged frame are transmitted.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    case STATE_RX_ERROR:
 | 
				
			||||||
 | 
					        vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* In the idle state we wait for a new character. If a character
 | 
				
			||||||
 | 
					         * is received the t1.5 and t3.5 timers are started and the
 | 
				
			||||||
 | 
					         * receiver is in the state STATE_RX_RECEIVCE.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    case STATE_RX_IDLE:
 | 
				
			||||||
 | 
					        usRcvBufferPos = 0;
 | 
				
			||||||
 | 
					        ucRTUBuf[usRcvBufferPos++] = ucByte;
 | 
				
			||||||
 | 
					        eRcvState = STATE_RX_RCV;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Enable t3.5 timers. */
 | 
				
			||||||
 | 
					        vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* We are currently receiving a frame. Reset the timer after
 | 
				
			||||||
 | 
					         * every character received. If more than the maximum possible
 | 
				
			||||||
 | 
					         * number of bytes in a modbus frame is received the frame is
 | 
				
			||||||
 | 
					         * ignored.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    case STATE_RX_RCV:
 | 
				
			||||||
 | 
					        if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            ucRTUBuf[usRcvBufferPos++] = ucByte;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            eRcvState = STATE_RX_ERROR;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        vMBPortTimersEnable(  );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return xTaskNeedSwitch;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BOOL
 | 
				
			||||||
 | 
					xMBRTUTransmitFSM( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BOOL            xNeedPoll = FALSE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert( eRcvState == STATE_RX_IDLE );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch ( eSndState )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* We should not get a transmitter event if the transmitter is in
 | 
				
			||||||
 | 
					         * idle state.  */
 | 
				
			||||||
 | 
					    case STATE_TX_IDLE:
 | 
				
			||||||
 | 
					        /* enable receiver/disable transmitter. */
 | 
				
			||||||
 | 
					        vMBPortSerialEnable( TRUE, FALSE );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    case STATE_TX_XMIT:
 | 
				
			||||||
 | 
					        /* check if we are finished. */
 | 
				
			||||||
 | 
					        if( usSndBufferCount != 0 )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
 | 
				
			||||||
 | 
					            pucSndBufferCur++;  /* next byte in sendbuffer. */
 | 
				
			||||||
 | 
					            usSndBufferCount--;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
 | 
				
			||||||
 | 
					            /* Disable transmitter. This prevents another transmit buffer
 | 
				
			||||||
 | 
					             * empty interrupt. */
 | 
				
			||||||
 | 
					            vMBPortSerialEnable( TRUE, FALSE );
 | 
				
			||||||
 | 
					            eSndState = STATE_TX_IDLE;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return xNeedPoll;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					//定时中断发生,触发的函数
 | 
				
			||||||
 | 
					BOOL
 | 
				
			||||||
 | 
					xMBRTUTimerT35Expired( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    BOOL            xNeedPoll = FALSE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch ( eRcvState )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /* Timer t35 expired. Startup phase is finished. */
 | 
				
			||||||
 | 
					    case STATE_RX_INIT:
 | 
				
			||||||
 | 
					        xNeedPoll = xMBPortEventPost( EV_READY );//事件类型赋值
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* A frame was received and t35 expired. Notify the listener that
 | 
				
			||||||
 | 
					         * a new frame was received. */
 | 
				
			||||||
 | 
					    case STATE_RX_RCV:
 | 
				
			||||||
 | 
					        xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* An error occured while receiving the frame. */
 | 
				
			||||||
 | 
					    case STATE_RX_ERROR:
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Function called in an illegal state. */
 | 
				
			||||||
 | 
					    default:
 | 
				
			||||||
 | 
					        assert( ( eRcvState == STATE_RX_INIT ) ||
 | 
				
			||||||
 | 
					                ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    vMBPortTimersDisable(  );
 | 
				
			||||||
 | 
					    eRcvState = STATE_RX_IDLE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return xNeedPoll;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,50 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_RTU_H
 | 
				
			||||||
 | 
					#define _MB_RTU_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					    eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate,
 | 
				
			||||||
 | 
					                             eMBParity eParity );
 | 
				
			||||||
 | 
					void            eMBRTUStart( void );
 | 
				
			||||||
 | 
					void            eMBRTUStop( void );
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
 | 
				
			||||||
 | 
					eMBErrorCode    eMBRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
 | 
				
			||||||
 | 
					BOOL            xMBRTUReceiveFSM( void );
 | 
				
			||||||
 | 
					BOOL            xMBRTUTransmitFSM( void );
 | 
				
			||||||
 | 
					BOOL            xMBRTUTimerT15Expired( void );
 | 
				
			||||||
 | 
					BOOL            xMBRTUTimerT35Expired( void );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,157 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- System includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					#include "string.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Platform includes --------------------------------*/
 | 
				
			||||||
 | 
					#include "port.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Modbus includes ----------------------------------*/
 | 
				
			||||||
 | 
					#include "mb.h"
 | 
				
			||||||
 | 
					#include "mbconfig.h"
 | 
				
			||||||
 | 
					#include "mbtcp.h"
 | 
				
			||||||
 | 
					#include "mbframe.h"
 | 
				
			||||||
 | 
					#include "mbport.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if MB_TCP_ENABLED > 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- MBAP Header --------------------------------------*/
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * <------------------------ MODBUS TCP/IP ADU(1) ------------------------->
 | 
				
			||||||
 | 
					 *              <----------- MODBUS PDU (1') ---------------->
 | 
				
			||||||
 | 
					 *  +-----------+---------------+------------------------------------------+
 | 
				
			||||||
 | 
					 *  | TID | PID | Length | UID  |Code | Data                               |
 | 
				
			||||||
 | 
					 *  +-----------+---------------+------------------------------------------+
 | 
				
			||||||
 | 
					 *  |     |     |        |      |                                           
 | 
				
			||||||
 | 
					 * (2)   (3)   (4)      (5)    (6)                                          
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * (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)
 | 
				
			||||||
 | 
					 * (6)  ... MB_TCP_FUNC         = 7 (Modbus Function Code)
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * (1)  ... Modbus TCP/IP Application Data Unit
 | 
				
			||||||
 | 
					 * (1') ... Modbus Protocol Data Unit
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_TCP_TID          0
 | 
				
			||||||
 | 
					#define MB_TCP_PID          2
 | 
				
			||||||
 | 
					#define MB_TCP_LEN          4
 | 
				
			||||||
 | 
					#define MB_TCP_UID          6
 | 
				
			||||||
 | 
					#define MB_TCP_FUNC         7
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MB_TCP_PROTOCOL_ID  0   /* 0 = Modbus Protocol */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Start implementation -----------------------------*/
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBTCPDoInit( USHORT ucTCPPort )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( xMBTCPPortInit( ucTCPPort ) == FALSE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EPORTERR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					eMBTCPStart( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					eMBTCPStop( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /* Make sure that no more clients are connected. */
 | 
				
			||||||
 | 
					    vMBTCPPortDisable( );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    UCHAR          *pucMBTCPFrame;
 | 
				
			||||||
 | 
					    USHORT          usLength;
 | 
				
			||||||
 | 
					    USHORT          usPID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
 | 
				
			||||||
 | 
					        usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if( usPID == MB_TCP_PROTOCOL_ID )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            *ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
 | 
				
			||||||
 | 
					            *pusLength = usLength - MB_TCP_FUNC;
 | 
				
			||||||
 | 
					            eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /* Modbus TCP does not use any addresses. Fake the source address such
 | 
				
			||||||
 | 
					             * that the processing part deals with this frame.
 | 
				
			||||||
 | 
					             */
 | 
				
			||||||
 | 
					            *pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					eMBErrorCode
 | 
				
			||||||
 | 
					eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    eMBErrorCode    eStatus = MB_ENOERR;
 | 
				
			||||||
 | 
					    UCHAR          *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
 | 
				
			||||||
 | 
					    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 
 | 
				
			||||||
 | 
					     * the length is usLength plus one.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
 | 
				
			||||||
 | 
					    pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
 | 
				
			||||||
 | 
					    if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        eStatus = MB_EIO;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return eStatus;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,52 @@
 | 
				
			||||||
 | 
					/* 
 | 
				
			||||||
 | 
					 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
 | 
				
			||||||
 | 
					 * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
 | 
				
			||||||
 | 
					 * All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Redistribution and use in source and binary forms, with or without
 | 
				
			||||||
 | 
					 * modification, are permitted provided that the following conditions
 | 
				
			||||||
 | 
					 * are met:
 | 
				
			||||||
 | 
					 * 1. Redistributions of source code must retain the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer.
 | 
				
			||||||
 | 
					 * 2. Redistributions in binary form must reproduce the above copyright
 | 
				
			||||||
 | 
					 *    notice, this list of conditions and the following disclaimer in the
 | 
				
			||||||
 | 
					 *    documentation and/or other materials provided with the distribution.
 | 
				
			||||||
 | 
					 * 3. The name of the author may not be used to endorse or promote products
 | 
				
			||||||
 | 
					 *    derived from this software without specific prior written permission.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
				
			||||||
 | 
					 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
				
			||||||
 | 
					 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
				
			||||||
 | 
					 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
				
			||||||
 | 
					 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
				
			||||||
 | 
					 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
				
			||||||
 | 
					 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
				
			||||||
 | 
					 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
				
			||||||
 | 
					 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
				
			||||||
 | 
					 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef _MB_TCP_H
 | 
				
			||||||
 | 
					#define _MB_TCP_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_BEGIN_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Defines ------------------------------------------*/
 | 
				
			||||||
 | 
					#define MB_TCP_PSEUDO_ADDRESS   255
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* ----------------------- Function prototypes ------------------------------*/
 | 
				
			||||||
 | 
					    eMBErrorCode eMBTCPDoInit( USHORT ucTCPPort );
 | 
				
			||||||
 | 
					void            eMBTCPStart( void );
 | 
				
			||||||
 | 
					void            eMBTCPStop( void );
 | 
				
			||||||
 | 
					eMBErrorCode    eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
 | 
				
			||||||
 | 
					                               USHORT * pusLength );
 | 
				
			||||||
 | 
					eMBErrorCode    eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame,
 | 
				
			||||||
 | 
					                            USHORT usLength );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __cplusplus
 | 
				
			||||||
 | 
					PR_END_EXTERN_C
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||