From f8b845ca6202f66cae9503d506dd27d2b10a9b08 Mon Sep 17 00:00:00 2001 From: Liu_Weichao Date: Fri, 6 Jan 2023 17:25:11 +0800 Subject: [PATCH] optimize control framework using socket, support LwIP and W5500(need to add init/socket apis) --- APP_Framework/Framework/control/Kconfig | 20 ++++++++++++++++++- .../Framework/control/ipc_protocol/Kconfig | 2 +- .../ipc_protocol/modbus_tcp/modbus_tcp.c | 6 ++++++ .../Framework/control/plc_protocol/Kconfig | 2 +- .../control/plc_protocol/fins/fins.c | 6 ++++++ .../control/plc_protocol/melsec/Kconfig | 18 ++++++++--------- .../control/plc_protocol/melsec/melsec.c | 6 +++--- .../Framework/control/shared/control_def.c | 9 ++++++++- .../Framework/control/shared/control_io.c | 13 ++++++++---- .../Framework/control/shared/control_io.h | 10 ++++++++++ 10 files changed, 72 insertions(+), 20 deletions(-) diff --git a/APP_Framework/Framework/control/Kconfig b/APP_Framework/Framework/control/Kconfig index 5618d752f..7d757e4ab 100755 --- a/APP_Framework/Framework/control/Kconfig +++ b/APP_Framework/Framework/control/Kconfig @@ -7,10 +7,28 @@ menuconfig SUPPORT_CONTROL_FRAMEWORK select LIB_USING_CJSON if SUPPORT_CONTROL_FRAMEWORK - config BSP_USING_SERIAL_485 + config CONTROL_USING_SERIAL_485 bool default n + config CONTROL_USING_SOCKET + bool + default n + if CONTROL_USING_SOCKET + choice + prompt "select socket lib" + default CONTROL_SOCKET_LWIP + + config CONTROL_SOCKET_LWIP + bool "support socket, using LwIP" + select BSP_USING_LWIP + + config CONTROL_SOCKET_W5500 + bool "support socket, using W5500" + select BSP_USING_W5500 + endchoice + endif + config CONTROL_RECIPE_FILE string "control framework recipe file name" default "test_recipe.json" diff --git a/APP_Framework/Framework/control/ipc_protocol/Kconfig b/APP_Framework/Framework/control/ipc_protocol/Kconfig index cd48d8cc5..2e97d8832 100755 --- a/APP_Framework/Framework/control/ipc_protocol/Kconfig +++ b/APP_Framework/Framework/control/ipc_protocol/Kconfig @@ -1,7 +1,7 @@ config CONTROL_PROTOCOL_MODBUS_TCP bool "Using modbus_tcp control protocol" default n - select BSP_USING_LWIP + select CONTROL_USING_SOCKET if CONTROL_PROTOCOL_MODBUS_TCP source "$APP_DIR/Framework/control/ipc_protocol/modbus_tcp/Kconfig" endif diff --git a/APP_Framework/Framework/control/ipc_protocol/modbus_tcp/modbus_tcp.c b/APP_Framework/Framework/control/ipc_protocol/modbus_tcp/modbus_tcp.c index a731695b8..dfd0f3577 100755 --- a/APP_Framework/Framework/control/ipc_protocol/modbus_tcp/modbus_tcp.c +++ b/APP_Framework/Framework/control/ipc_protocol/modbus_tcp/modbus_tcp.c @@ -75,6 +75,7 @@ static void ModbusTcpTransformRecvBuffToData(ModbusTcpReadItem *p_read_item, uin printf("\n"); } +#ifdef CONTROL_USING_SOCKET /** * @description: Modbus Tcp Get Data From Socket * @param socket - socket @@ -133,6 +134,7 @@ static int ModbusTcpGetData(int32_t socket, ModbusTcpReadItem *p_read_item) } return -2; } +#endif /** * @description: Modbus Tcp Data Info Init @@ -312,6 +314,7 @@ void *ReceivePlcDataTask(void *parameter) while (1) { for (i = 0; i < control_protocol->recipe->read_item_count; i ++) { +#ifdef CONTROL_USING_SOCKET /*only connect socket when close socket or init*/ while (ControlConnectSocket(&plc_socket) < 0) { PrivTaskDelay(1000); @@ -320,6 +323,7 @@ void *ReceivePlcDataTask(void *parameter) ModbusTcpForamatWriteData((ModbusTcpReadItem *)modbus_tcp_read_item + i); ModbusTcpGetData(plc_socket.socket, (ModbusTcpReadItem *)modbus_tcp_read_item + i); +#endif } /*read all variable item data, put them into circular_area*/ @@ -360,7 +364,9 @@ int ModbusTcpClose(struct ControlProtocol *control_protocol) { CircularAreaAppRelease(g_write_data); +#ifdef CONTROL_USING_SOCKET ControlDisconnectSocket(&plc_socket); +#endif ControlProtocolCloseDef(); diff --git a/APP_Framework/Framework/control/plc_protocol/Kconfig b/APP_Framework/Framework/control/plc_protocol/Kconfig index 46f632974..91e27b474 100755 --- a/APP_Framework/Framework/control/plc_protocol/Kconfig +++ b/APP_Framework/Framework/control/plc_protocol/Kconfig @@ -1,7 +1,7 @@ config CONTROL_PROTOCOL_FINS bool "Using fins control protocol" default n - select BSP_USING_LWIP + select CONTROL_USING_SOCKET if CONTROL_PROTOCOL_FINS source "$APP_DIR/Framework/control/plc_protocol/fins/Kconfig" endif diff --git a/APP_Framework/Framework/control/plc_protocol/fins/fins.c b/APP_Framework/Framework/control/plc_protocol/fins/fins.c index 1b0419a88..0a8c20125 100644 --- a/APP_Framework/Framework/control/plc_protocol/fins/fins.c +++ b/APP_Framework/Framework/control/plc_protocol/fins/fins.c @@ -154,6 +154,7 @@ static int FinsTransformRecvBuffToData(FinsReadItem *p_read_item, uint8_t *recv_ return 0; } +#ifdef CONTROL_USING_SOCKET /** * @description: Fins Protocol Handshake * @param socket - socket @@ -242,6 +243,7 @@ static int FinsGetData(int32_t socket, FinsReadItem *p_read_item) } return -2; } +#endif /** * @description: Fins Data Info Init @@ -303,6 +305,7 @@ void *ReceivePlcDataTask(void *parameter) while (1) { for (i = 0; i < control_protocol->recipe->read_item_count; i ++) { +#ifdef CONTROL_USING_SOCKET /*only connect socket when close socket or init*/ while (ControlConnectSocket(&plc_socket) < 0) { PrivTaskDelay(1000); @@ -320,6 +323,7 @@ void *ReceivePlcDataTask(void *parameter) plc_socket.secondary_connect_flag = 1; FinsGetData(plc_socket.socket, (FinsReadItem *)fins_read_item + i); +#endif } /*read all variable item data, put them into circular_area*/ @@ -352,7 +356,9 @@ int FinsOpen(struct ControlProtocol *control_protocol) */ int FinsClose(struct ControlProtocol *control_protocol) { +#ifdef CONTROL_USING_SOCKET ControlDisconnectSocket(&plc_socket); +#endif ControlProtocolCloseDef(); diff --git a/APP_Framework/Framework/control/plc_protocol/melsec/Kconfig b/APP_Framework/Framework/control/plc_protocol/melsec/Kconfig index 8a8d32c5a..c441642b9 100755 --- a/APP_Framework/Framework/control/plc_protocol/melsec/Kconfig +++ b/APP_Framework/Framework/control/plc_protocol/melsec/Kconfig @@ -3,27 +3,27 @@ choice default CONTROL_PROTOCOL_MELSEC_1E config CONTROL_PROTOCOL_MELSEC_1E - bool "support melsec_1e protocol, using TCP" - select BSP_USING_LWIP + bool "support melsec_1e protocol, using SOCKET" + select CONTROL_USING_SOCKET config CONTROL_PROTOCOL_MELSEC_3E_Q_L - bool "support melsec_3e_q_l protocol, using TCP" - select BSP_USING_LWIP + bool "support melsec_3e_q_l protocol, using SOCKET" + select CONTROL_USING_SOCKET config CONTROL_PROTOCOL_MELSEC_3E_IQ_R - bool "support melsec_3e_iq_r protocol, using TCP" - select BSP_USING_LWIP + bool "support melsec_3e_iq_r protocol, using SOCKET" + select CONTROL_USING_SOCKET config CONTROL_PROTOCOL_MELSEC_1C bool "support melsec_1c protocol, using SERIAL" - select BSP_USING_SERIAL_485 + select CONTROL_USING_SERIAL_485 config CONTROL_PROTOCOL_MELSEC_3C bool "support melsec_3c protocol, using SERIAL" - select BSP_USING_SERIAL_485 + select CONTROL_USING_SERIAL_485 endchoice -if BSP_USING_SERIAL_485 +if CONTROL_USING_SERIAL_485 if ADD_XIZI_FETURES config CONTROL_FRAMEWORK_UART_485_DIR int "control framework 485 direction pin number" diff --git a/APP_Framework/Framework/control/plc_protocol/melsec/melsec.c b/APP_Framework/Framework/control/plc_protocol/melsec/melsec.c index 584fbdaf5..27b97a831 100644 --- a/APP_Framework/Framework/control/plc_protocol/melsec/melsec.c +++ b/APP_Framework/Framework/control/plc_protocol/melsec/melsec.c @@ -575,7 +575,7 @@ static int MelsecTransformRecvBuffToData(MelsecReadItem *p_read_item, uint8_t *r return 0; } -#ifdef BSP_USING_LWIP +#ifdef CONTROL_USING_SOCKET /** * @description: Melsec Get Data From Socket * @param socket - socket @@ -675,7 +675,7 @@ void *ReceivePlcDataTask(void *parameter) if ((PROTOCOL_MELSEC_1C == control_protocol->protocol_type) || (PROTOCOL_MELSEC_3C == control_protocol->protocol_type)) { MelsecGetDataBySerial((MelsecReadItem *)melsec_read_item + i); } else { -#ifdef BSP_USING_LWIP +#ifdef CONTROL_USING_SOCKET /*only connect socket when close socket or init*/ while (ControlConnectSocket(&plc_socket) < 0) { PrivTaskDelay(1000); @@ -717,7 +717,7 @@ int MelsecOpen(struct ControlProtocol *control_protocol) int MelsecClose(struct ControlProtocol *control_protocol) { if ((PROTOCOL_MELSEC_1C != control_protocol->protocol_type) && (PROTOCOL_MELSEC_3C != control_protocol->protocol_type)) { -#ifdef BSP_USING_LWIP +#ifdef CONTROL_USING_SOCKET ControlDisconnectSocket(&plc_socket); #endif } diff --git a/APP_Framework/Framework/control/shared/control_def.c b/APP_Framework/Framework/control/shared/control_def.c index 3168af845..c19ae9757 100644 --- a/APP_Framework/Framework/control/shared/control_def.c +++ b/APP_Framework/Framework/control/shared/control_def.c @@ -41,6 +41,10 @@ extern int MelsecProtocolInit(struct ControlRecipe *p_recipe); extern int ModbusTcpProtocolInit(struct ControlRecipe *p_recipe); #endif +#ifdef CONTROL_PROTOCOL_MODBUS_UART +extern int ModbusUartProtocolInit(struct ControlRecipe *p_recipe); +#endif + /* CONTROL FRAMEWORK READ DATA FORMAT: | HEAD |device_id|read data length|read item count| data | @@ -73,6 +77,9 @@ static struct ControlProtocolInitParam protocol_init[] = #ifdef CONTROL_PROTOCOL_MODBUS_TCP { PROTOCOL_MODBUS_TCP, ModbusTcpProtocolInit }, #endif +#ifdef CONTROL_PROTOCOL_MODBUS_UART + { PROTOCOL_MODBUS_UART, ModbusUartProtocolInit }, +#endif { PROTOCOL_END, NULL }, }; @@ -210,7 +217,7 @@ void ControlPrintfList(char name[5], uint8_t *number_list, uint16_t length) printf("\n**************************************\n"); } -#ifdef BSP_USING_LWIP +#ifdef CONTROL_USING_SOCKET /** * @description: Control Framework Connect Socket * @param p_plc - basic socket plc pointer diff --git a/APP_Framework/Framework/control/shared/control_io.c b/APP_Framework/Framework/control/shared/control_io.c index 0bc8b77d7..4ad09b789 100644 --- a/APP_Framework/Framework/control/shared/control_io.c +++ b/APP_Framework/Framework/control/shared/control_io.c @@ -20,7 +20,7 @@ #include -#ifdef BSP_USING_SERIAL_485 +#ifdef CONTROL_USING_SERIAL_485 static int pin_fd = 0; static int uart_fd = 0; @@ -119,9 +119,14 @@ void SocketInit(char *ip, char *mask, char *gw) ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2], mask[3], gw[0], gw[1], gw[2], gw[3]); +#ifdef CONTROL_USING_SOCKET #ifdef BSP_USING_LWIP lwip_config_tcp(0, ip, mask, gw); #endif +#ifdef BSP_USING_W5500 + //to do +#endif +#endif } /** @@ -134,7 +139,7 @@ void SocketInit(char *ip, char *mask, char *gw) */ void SerialInit(uint32_t baud_rate, uint8_t data_bits, uint8_t stop_bits, uint8_t check_mode) { -#ifdef BSP_USING_SERIAL_485 +#ifdef CONTROL_USING_SERIAL_485 Uart485Init(baud_rate, data_bits, stop_bits, check_mode); #endif } @@ -147,7 +152,7 @@ void SerialInit(uint32_t baud_rate, uint8_t data_bits, uint8_t stop_bits, uint8_ */ void SerialWrite(uint8_t *write_data, int length) { -#ifdef BSP_USING_SERIAL_485 +#ifdef CONTROL_USING_SERIAL_485 Set485Output(); PrivTaskDelay(20); @@ -166,7 +171,7 @@ void SerialWrite(uint8_t *write_data, int length) */ int SerialRead(uint8_t *read_data, int length) { -#ifdef BSP_USING_SERIAL_485 +#ifdef CONTROL_USING_SERIAL_485 int data_size = 0; int data_recv_size = 0; diff --git a/APP_Framework/Framework/control/shared/control_io.h b/APP_Framework/Framework/control/shared/control_io.h index ada560ae2..9446916a6 100644 --- a/APP_Framework/Framework/control/shared/control_io.h +++ b/APP_Framework/Framework/control/shared/control_io.h @@ -24,20 +24,30 @@ #include #include +#ifdef CONTROL_USING_SOCKET #ifdef BSP_USING_LWIP #include "lwip/sys.h" #include "lwip/sockets.h" #endif +#endif #ifdef __cplusplus extern "C" { #endif +#ifdef CONTROL_USING_SOCKET #ifdef BSP_USING_LWIP #define socket_write lwip_write #define socket_read lwip_read #endif +#ifdef BSP_USING_W5500 +//to do +#define socket_write +#define socket_read +#endif +#endif + /*Control Framework Socket Init*/ void SocketInit(char *ip, char *mask, char *gw);