add three PLC test demo and readme

This commit is contained in:
Wien.b
2023-11-23 23:23:08 -08:00
parent b82bdaea6b
commit d3c7d7ce9f
42 changed files with 522 additions and 158 deletions

View File

@@ -21,4 +21,3 @@ config CONNECTION_ADAPTER_FREEMODBUSTCP
if CONNECTION_ADAPTER_FREEMODBUSTCP
source "$APP_DIR/Framework/connection/industrial_network/freemodbus_tcp/Kconfig"
endif

View File

@@ -1,92 +0,0 @@
void LwipTcpRecvTest(void)
{
uint8_t enet_port = 0; ///< test enet port 0
lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
uint8_t *recv_data;
socklen_t sin_size;
int sock = -1, connected, bytes_received, i;
struct sockaddr_in server_addr, client_addr;
fd_set readset;
struct timeval timeout;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
KPrintf("[%s:%d] Socket error!\n", __FILE__, __LINE__);
goto __exit;
}
recv_data = (uint8_t *)malloc(128);
if (recv_data == NULL) {
KPrintf("No memory!\n");
goto __exit;
}
//configure tcp server param
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(tcp_server_port);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
KPrintf("Unable to bind!\n");
goto __exit;
}
if (listen(sock, 5) == -1) {
KPrintf("Listen error!\n");
goto __exit;
}
timeout.tv_sec = 30;
timeout.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) ==-1) {
KPrintf("setsockopt failed!");
goto __exit;
}
while (1) {
FD_ZERO(&readset);
FD_SET(sock, &readset);
if (select(sock + 1, &readset, NULL, NULL, &timeout) == 0) {
continue;
}
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
while (1) {
bytes_received = recv(connected, recv_data, 128, 0);
if (bytes_received == 0) {
KPrintf("client disconnected (%s, %d)\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
} else if (bytes_received < 0) {
KPrintf("recv error, client: (%s, %d)\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
} else {
KPrintf("new client connected from (%s, %d)\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
KPrintf("recv data length %d Bytes\n", bytes_received);
for (i = 0; i < bytes_received; i ++) {
KPrintf("data 0x%x\n", recv_data[i]);
}
if (i = bytes_received) {
KPrintf("\r\n");
memset(recv_data, 0, sizeof(recv_data));
}
}
}
if (connected >= 0) {
closesocket(connected);
connected = -1;
break;
}
__exit:
if (sock >= 0) closesocket(sock);
if (recv_data) free(recv_data);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) |
SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),TCPRecv, LwipTcpRecvTest, TCP Recv message);

View File

@@ -112,7 +112,6 @@ int MBServer()
MdelayKTask(100);
}
}
return iExitCode;
}

View File

@@ -14,3 +14,11 @@ if CONTROL_PROTOCOL_MODBUS_UART
source "$APP_DIR/Framework/control/ipc_protocol/modbus_uart/Kconfig"
endif
config CONTROL_PROTOCOL_ETHERCAT
bool "Using ethercat control protocol"
default n
select CONTROL_USING_SOCKET
if CONTROL_PROTOCOL_ETHERCAT
source "$APP_DIR/Framework/control/ipc_protocol/ethercat/Kconfig"
endif

View File

@@ -6,5 +6,9 @@ ifeq ($(CONFIG_CONTROL_PROTOCOL_MODBUS_UART), y)
SRC_DIR := modbus_uart
endif
ifeq ($(CONFIG_CONTROL_PROTOCOL_ETHERCAT), y)
SRC_DIR := ethercat
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -33,5 +33,6 @@ config CONTROL_PROTOCOL_CIP
default n
select CONTROL_USING_SOCKET
if CONTROL_PROTOCOL_CIP
source "$APP_DIR/Framework/control/plc_protocol/cip/Kconfig"
source "$APP_DIR/Framework/control/plc_protocol/ethernet_ip_cip/Kconfig"
endif

View File

@@ -15,7 +15,7 @@ ifeq ($(CONFIG_CONTROL_PROTOCOL_S7), y)
endif
ifeq ($(CONFIG_CONTROL_PROTOCOL_CIP), y)
SRC_DIR := cip
SRC_DIR := ethernet_ip_cip
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -1,2 +1,3 @@
SRC_FILES := $(wildcard ./*.c)
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -57,6 +57,7 @@ typedef enum
PROTOCOL_MELSEC_3C,
PROTOCOL_FREEMODBUS_TCP_SERVER,
PROTOCOL_CIP,
PROTOCOL_ETHERCAT,
PROTOCOL_END
}ProtocolType;
@@ -116,6 +117,7 @@ int ControlProtocolIoctl(struct ControlProtocol *control_protocol, int cmd, void
/*Control Framework new certain Protocol*/
ControlProtocolType control_protocol;
#ifdef __cplusplus
}
#endif

View File

@@ -57,6 +57,10 @@ extern int FreeModbusTcpServerInit(struct ControlRecipe *p_recipe);
extern int CipProtocolInit(struct ControlRecipe *p_recipe);
#endif
#ifdef CONTROL_PROTOCOL_ETHERCAT
extern int EthercatProtocolInit(struct ControlRecipe *p_recipe);
#endif
/*
CONTROL FRAMEWORK READ DATA FORMAT:
| HEAD |device_id|read data length|read item count| data |
@@ -103,6 +107,11 @@ static struct ControlProtocolInitParam protocol_init[] =
#ifdef CONTROL_PROTOCOL_CIP
{ PROTOCOL_CIP, CipProtocolInit },
#endif
#ifdef CONTROL_PROTOCOL_ETHERCAT
{ PROTOCOL_ETHERCAT, EthercatProtocolInit },
#endif
{ PROTOCOL_END, NULL },
};