optimize control framework using socket, support LwIP and W5500(need to add init/socket apis)

This commit is contained in:
Liu_Weichao 2023-01-06 17:25:11 +08:00
parent 9ad282c39e
commit f8b845ca62
10 changed files with 72 additions and 20 deletions

View File

@ -7,10 +7,28 @@ menuconfig SUPPORT_CONTROL_FRAMEWORK
select LIB_USING_CJSON select LIB_USING_CJSON
if SUPPORT_CONTROL_FRAMEWORK if SUPPORT_CONTROL_FRAMEWORK
config BSP_USING_SERIAL_485 config CONTROL_USING_SERIAL_485
bool bool
default n 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 config CONTROL_RECIPE_FILE
string "control framework recipe file name" string "control framework recipe file name"
default "test_recipe.json" default "test_recipe.json"

View File

@ -1,7 +1,7 @@
config CONTROL_PROTOCOL_MODBUS_TCP config CONTROL_PROTOCOL_MODBUS_TCP
bool "Using modbus_tcp control protocol" bool "Using modbus_tcp control protocol"
default n default n
select BSP_USING_LWIP select CONTROL_USING_SOCKET
if CONTROL_PROTOCOL_MODBUS_TCP if CONTROL_PROTOCOL_MODBUS_TCP
source "$APP_DIR/Framework/control/ipc_protocol/modbus_tcp/Kconfig" source "$APP_DIR/Framework/control/ipc_protocol/modbus_tcp/Kconfig"
endif endif

View File

@ -75,6 +75,7 @@ static void ModbusTcpTransformRecvBuffToData(ModbusTcpReadItem *p_read_item, uin
printf("\n"); printf("\n");
} }
#ifdef CONTROL_USING_SOCKET
/** /**
* @description: Modbus Tcp Get Data From Socket * @description: Modbus Tcp Get Data From Socket
* @param socket - socket * @param socket - socket
@ -133,6 +134,7 @@ static int ModbusTcpGetData(int32_t socket, ModbusTcpReadItem *p_read_item)
} }
return -2; return -2;
} }
#endif
/** /**
* @description: Modbus Tcp Data Info Init * @description: Modbus Tcp Data Info Init
@ -312,6 +314,7 @@ void *ReceivePlcDataTask(void *parameter)
while (1) { while (1) {
for (i = 0; i < control_protocol->recipe->read_item_count; i ++) { for (i = 0; i < control_protocol->recipe->read_item_count; i ++) {
#ifdef CONTROL_USING_SOCKET
/*only connect socket when close socket or init*/ /*only connect socket when close socket or init*/
while (ControlConnectSocket(&plc_socket) < 0) { while (ControlConnectSocket(&plc_socket) < 0) {
PrivTaskDelay(1000); PrivTaskDelay(1000);
@ -320,6 +323,7 @@ void *ReceivePlcDataTask(void *parameter)
ModbusTcpForamatWriteData((ModbusTcpReadItem *)modbus_tcp_read_item + i); ModbusTcpForamatWriteData((ModbusTcpReadItem *)modbus_tcp_read_item + i);
ModbusTcpGetData(plc_socket.socket, (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*/ /*read all variable item data, put them into circular_area*/
@ -360,7 +364,9 @@ int ModbusTcpClose(struct ControlProtocol *control_protocol)
{ {
CircularAreaAppRelease(g_write_data); CircularAreaAppRelease(g_write_data);
#ifdef CONTROL_USING_SOCKET
ControlDisconnectSocket(&plc_socket); ControlDisconnectSocket(&plc_socket);
#endif
ControlProtocolCloseDef(); ControlProtocolCloseDef();

View File

@ -1,7 +1,7 @@
config CONTROL_PROTOCOL_FINS config CONTROL_PROTOCOL_FINS
bool "Using fins control protocol" bool "Using fins control protocol"
default n default n
select BSP_USING_LWIP select CONTROL_USING_SOCKET
if CONTROL_PROTOCOL_FINS if CONTROL_PROTOCOL_FINS
source "$APP_DIR/Framework/control/plc_protocol/fins/Kconfig" source "$APP_DIR/Framework/control/plc_protocol/fins/Kconfig"
endif endif

View File

@ -154,6 +154,7 @@ static int FinsTransformRecvBuffToData(FinsReadItem *p_read_item, uint8_t *recv_
return 0; return 0;
} }
#ifdef CONTROL_USING_SOCKET
/** /**
* @description: Fins Protocol Handshake * @description: Fins Protocol Handshake
* @param socket - socket * @param socket - socket
@ -242,6 +243,7 @@ static int FinsGetData(int32_t socket, FinsReadItem *p_read_item)
} }
return -2; return -2;
} }
#endif
/** /**
* @description: Fins Data Info Init * @description: Fins Data Info Init
@ -303,6 +305,7 @@ void *ReceivePlcDataTask(void *parameter)
while (1) { while (1) {
for (i = 0; i < control_protocol->recipe->read_item_count; i ++) { for (i = 0; i < control_protocol->recipe->read_item_count; i ++) {
#ifdef CONTROL_USING_SOCKET
/*only connect socket when close socket or init*/ /*only connect socket when close socket or init*/
while (ControlConnectSocket(&plc_socket) < 0) { while (ControlConnectSocket(&plc_socket) < 0) {
PrivTaskDelay(1000); PrivTaskDelay(1000);
@ -320,6 +323,7 @@ void *ReceivePlcDataTask(void *parameter)
plc_socket.secondary_connect_flag = 1; plc_socket.secondary_connect_flag = 1;
FinsGetData(plc_socket.socket, (FinsReadItem *)fins_read_item + i); FinsGetData(plc_socket.socket, (FinsReadItem *)fins_read_item + i);
#endif
} }
/*read all variable item data, put them into circular_area*/ /*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) int FinsClose(struct ControlProtocol *control_protocol)
{ {
#ifdef CONTROL_USING_SOCKET
ControlDisconnectSocket(&plc_socket); ControlDisconnectSocket(&plc_socket);
#endif
ControlProtocolCloseDef(); ControlProtocolCloseDef();

View File

@ -3,27 +3,27 @@ choice
default CONTROL_PROTOCOL_MELSEC_1E default CONTROL_PROTOCOL_MELSEC_1E
config CONTROL_PROTOCOL_MELSEC_1E config CONTROL_PROTOCOL_MELSEC_1E
bool "support melsec_1e protocol, using TCP" bool "support melsec_1e protocol, using SOCKET"
select BSP_USING_LWIP select CONTROL_USING_SOCKET
config CONTROL_PROTOCOL_MELSEC_3E_Q_L config CONTROL_PROTOCOL_MELSEC_3E_Q_L
bool "support melsec_3e_q_l protocol, using TCP" bool "support melsec_3e_q_l protocol, using SOCKET"
select BSP_USING_LWIP select CONTROL_USING_SOCKET
config CONTROL_PROTOCOL_MELSEC_3E_IQ_R config CONTROL_PROTOCOL_MELSEC_3E_IQ_R
bool "support melsec_3e_iq_r protocol, using TCP" bool "support melsec_3e_iq_r protocol, using SOCKET"
select BSP_USING_LWIP select CONTROL_USING_SOCKET
config CONTROL_PROTOCOL_MELSEC_1C config CONTROL_PROTOCOL_MELSEC_1C
bool "support melsec_1c protocol, using SERIAL" bool "support melsec_1c protocol, using SERIAL"
select BSP_USING_SERIAL_485 select CONTROL_USING_SERIAL_485
config CONTROL_PROTOCOL_MELSEC_3C config CONTROL_PROTOCOL_MELSEC_3C
bool "support melsec_3c protocol, using SERIAL" bool "support melsec_3c protocol, using SERIAL"
select BSP_USING_SERIAL_485 select CONTROL_USING_SERIAL_485
endchoice endchoice
if BSP_USING_SERIAL_485 if CONTROL_USING_SERIAL_485
if ADD_XIZI_FETURES if ADD_XIZI_FETURES
config CONTROL_FRAMEWORK_UART_485_DIR config CONTROL_FRAMEWORK_UART_485_DIR
int "control framework 485 direction pin number" int "control framework 485 direction pin number"

View File

@ -575,7 +575,7 @@ static int MelsecTransformRecvBuffToData(MelsecReadItem *p_read_item, uint8_t *r
return 0; return 0;
} }
#ifdef BSP_USING_LWIP #ifdef CONTROL_USING_SOCKET
/** /**
* @description: Melsec Get Data From Socket * @description: Melsec Get Data From Socket
* @param socket - 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)) { if ((PROTOCOL_MELSEC_1C == control_protocol->protocol_type) || (PROTOCOL_MELSEC_3C == control_protocol->protocol_type)) {
MelsecGetDataBySerial((MelsecReadItem *)melsec_read_item + i); MelsecGetDataBySerial((MelsecReadItem *)melsec_read_item + i);
} else { } else {
#ifdef BSP_USING_LWIP #ifdef CONTROL_USING_SOCKET
/*only connect socket when close socket or init*/ /*only connect socket when close socket or init*/
while (ControlConnectSocket(&plc_socket) < 0) { while (ControlConnectSocket(&plc_socket) < 0) {
PrivTaskDelay(1000); PrivTaskDelay(1000);
@ -717,7 +717,7 @@ int MelsecOpen(struct ControlProtocol *control_protocol)
int MelsecClose(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)) { 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); ControlDisconnectSocket(&plc_socket);
#endif #endif
} }

View File

@ -41,6 +41,10 @@ extern int MelsecProtocolInit(struct ControlRecipe *p_recipe);
extern int ModbusTcpProtocolInit(struct ControlRecipe *p_recipe); extern int ModbusTcpProtocolInit(struct ControlRecipe *p_recipe);
#endif #endif
#ifdef CONTROL_PROTOCOL_MODBUS_UART
extern int ModbusUartProtocolInit(struct ControlRecipe *p_recipe);
#endif
/* /*
CONTROL FRAMEWORK READ DATA FORMAT: CONTROL FRAMEWORK READ DATA FORMAT:
| HEAD |device_id|read data length|read item count| data | | HEAD |device_id|read data length|read item count| data |
@ -73,6 +77,9 @@ static struct ControlProtocolInitParam protocol_init[] =
#ifdef CONTROL_PROTOCOL_MODBUS_TCP #ifdef CONTROL_PROTOCOL_MODBUS_TCP
{ PROTOCOL_MODBUS_TCP, ModbusTcpProtocolInit }, { PROTOCOL_MODBUS_TCP, ModbusTcpProtocolInit },
#endif #endif
#ifdef CONTROL_PROTOCOL_MODBUS_UART
{ PROTOCOL_MODBUS_UART, ModbusUartProtocolInit },
#endif
{ PROTOCOL_END, NULL }, { PROTOCOL_END, NULL },
}; };
@ -210,7 +217,7 @@ void ControlPrintfList(char name[5], uint8_t *number_list, uint16_t length)
printf("\n**************************************\n"); printf("\n**************************************\n");
} }
#ifdef BSP_USING_LWIP #ifdef CONTROL_USING_SOCKET
/** /**
* @description: Control Framework Connect Socket * @description: Control Framework Connect Socket
* @param p_plc - basic socket plc pointer * @param p_plc - basic socket plc pointer

View File

@ -20,7 +20,7 @@
#include <control_io.h> #include <control_io.h>
#ifdef BSP_USING_SERIAL_485 #ifdef CONTROL_USING_SERIAL_485
static int pin_fd = 0; static int pin_fd = 0;
static int uart_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], ip[0], ip[1], ip[2], ip[3],
mask[0], mask[1], mask[2], mask[3], mask[0], mask[1], mask[2], mask[3],
gw[0], gw[1], gw[2], gw[3]); gw[0], gw[1], gw[2], gw[3]);
#ifdef CONTROL_USING_SOCKET
#ifdef BSP_USING_LWIP #ifdef BSP_USING_LWIP
lwip_config_tcp(0, ip, mask, gw); lwip_config_tcp(0, ip, mask, gw);
#endif #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) 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); Uart485Init(baud_rate, data_bits, stop_bits, check_mode);
#endif #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) void SerialWrite(uint8_t *write_data, int length)
{ {
#ifdef BSP_USING_SERIAL_485 #ifdef CONTROL_USING_SERIAL_485
Set485Output(); Set485Output();
PrivTaskDelay(20); PrivTaskDelay(20);
@ -166,7 +171,7 @@ void SerialWrite(uint8_t *write_data, int length)
*/ */
int SerialRead(uint8_t *read_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_size = 0;
int data_recv_size = 0; int data_recv_size = 0;

View File

@ -24,20 +24,30 @@
#include <transform.h> #include <transform.h>
#include <list.h> #include <list.h>
#ifdef CONTROL_USING_SOCKET
#ifdef BSP_USING_LWIP #ifdef BSP_USING_LWIP
#include "lwip/sys.h" #include "lwip/sys.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#endif #endif
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef CONTROL_USING_SOCKET
#ifdef BSP_USING_LWIP #ifdef BSP_USING_LWIP
#define socket_write lwip_write #define socket_write lwip_write
#define socket_read lwip_read #define socket_read lwip_read
#endif #endif
#ifdef BSP_USING_W5500
//to do
#define socket_write
#define socket_read
#endif
#endif
/*Control Framework Socket Init*/ /*Control Framework Socket Init*/
void SocketInit(char *ip, char *mask, char *gw); void SocketInit(char *ip, char *mask, char *gw);