forked from xuos/xiuos
move freemodbus tcp server from control to connetion
This commit is contained in:
parent
a247de691c
commit
b82bdaea6b
|
@ -13,3 +13,12 @@ config CONNECTION_ADAPTER_POWERLINK
|
|||
if CONNECTION_ADAPTER_POWERLINK
|
||||
source "$APP_DIR/Framework/connection/industrial_network/powerlink/Kconfig"
|
||||
endif
|
||||
|
||||
config CONNECTION_ADAPTER_FREEMODBUSTCP
|
||||
bool "Using FREEMODBUSTCP on industrial network adapter device"
|
||||
default n
|
||||
|
||||
if CONNECTION_ADAPTER_FREEMODBUSTCP
|
||||
source "$APP_DIR/Framework/connection/industrial_network/freemodbus_tcp/Kconfig"
|
||||
endif
|
||||
|
|
@ -8,4 +8,8 @@ ifeq ($(CONFIG_CONNECTION_ADAPTER_ETHERCAT),y)
|
|||
SRC_DIR += ethercat
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_FREEMODBUSTCP),y)
|
||||
SRC_DIR += freemodbus_tcp
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
|
||||
|
||||
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);
|
|
@ -38,7 +38,7 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sockets.h>
|
||||
#include <sockets.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
|
@ -33,11 +33,13 @@
|
|||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <transform.h>
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/sockets.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
#include "freemodbustcpserver.h"
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define PROG "freemodbus"
|
||||
|
@ -65,6 +67,7 @@ static BOOL bCreatePollingThread( void );
|
|||
static enum ThreadState eGetPollingThreadState( void );
|
||||
static void eSetPollingThreadState( enum ThreadState eNewState );
|
||||
static void* pvPollingThread( void *pvParameter );
|
||||
int LWIPConnectSocket(uint16_t port);
|
||||
|
||||
/* ----------------------- Start implementation -----------------------------*/
|
||||
int MBServer()
|
||||
|
@ -74,6 +77,17 @@ int MBServer()
|
|||
BOOL bDoExit;
|
||||
usRegHoldingBuf[5] = 123;
|
||||
usRegHoldingBuf[7] = 234;
|
||||
|
||||
printf("%s ip %d.%d.%d.%d mask %d.%d.%d.%d gw %d.%d.%d.%d\n", __func__,
|
||||
192, 168, 250, 233,
|
||||
255, 255, 255, 255,
|
||||
192, 168, 250, 1);
|
||||
uint8_t local_ip[4] = {192,168,250,233};
|
||||
uint8_t gateway[4] = {192,168,250,1};
|
||||
uint8_t netmask[4] = {255,255,255,0};
|
||||
lwip_config_tcp(0, local_ip, netmask, gateway);
|
||||
printf("%s LWIPInit done\n", __func__);
|
||||
|
||||
if( eMBTCPInit( MB_TCP_PORT_USE_DEFAULT ) != MB_ENOERR )
|
||||
{
|
||||
fprintf( stderr, "%s: can't initialize modbus stack!\r\n", PROG );
|
||||
|
@ -88,19 +102,23 @@ int MBServer()
|
|||
printf( "Can't start protocol stack! Already running?\r\n" );
|
||||
}
|
||||
}
|
||||
printf("%d %d\n",sizeof(usRegHoldingBuf),__LINE__);
|
||||
printf("%d %d %s\n",sizeof(usRegHoldingBuf),__LINE__,__func__);
|
||||
|
||||
while(1)
|
||||
{
|
||||
for(int i =0; i<sizeof(usRegHoldingBuf)/2;i++)
|
||||
{
|
||||
printf("poll recv is %3d\n", usRegHoldingBuf[i]);
|
||||
MdelayKTask(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iExitCode;
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(MBServer, a Mtcp server Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
||||
|
||||
BOOL bCreatePollingThread( void )
|
||||
{
|
||||
BOOL bResult;
|
||||
|
@ -249,33 +267,33 @@ eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT us
|
|||
* @param control_protocol - control protocol pointer
|
||||
* @return success : 0 error
|
||||
*/
|
||||
int FreeModbusTcpOpen(struct ControlProtocol *control_protocol)
|
||||
{
|
||||
ControlProtocolOpenDef(control_protocol);
|
||||
return 0;
|
||||
}
|
||||
// int FreeModbusTcpOpen(struct ControlProtocol *control_protocol)
|
||||
// {
|
||||
// ControlProtocolOpenDef(control_protocol);
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
static struct ControlDone FreeModbusTcp_protocol_done =
|
||||
{
|
||||
._open = FreeModbusTcpOpen,
|
||||
._close = NULL,
|
||||
._read = NULL,
|
||||
._write = NULL,
|
||||
._ioctl = NULL,
|
||||
};
|
||||
// static struct ControlDone FreeModbusTcp_protocol_done =
|
||||
// {
|
||||
// ._open = FreeModbusTcpOpen,
|
||||
// ._close = NULL,
|
||||
// ._read = NULL,
|
||||
// ._write = NULL,
|
||||
// ._ioctl = NULL,
|
||||
// };
|
||||
|
||||
void *ReceivePlcDataTask(void *parameter)
|
||||
{
|
||||
MBServer();
|
||||
}
|
||||
// void *ReceivePlcDataTask(void *parameter)
|
||||
// {
|
||||
// MBServer();
|
||||
// }
|
||||
|
||||
/**
|
||||
* @description: Modbus Tcp Server Init
|
||||
* @param p_recipe - recipe pointer
|
||||
* @return success : 0 error : -1
|
||||
*/
|
||||
int FreeModbusTcpServerInit(struct ControlRecipe *p_recipe)
|
||||
{
|
||||
p_recipe->done = &FreeModbusTcp_protocol_done;
|
||||
return 0;
|
||||
}
|
||||
// int FreeModbusTcpServerInit(struct ControlRecipe *p_recipe)
|
||||
// {
|
||||
// p_recipe->done = &FreeModbusTcp_protocol_done;
|
||||
// return 0;
|
||||
// }
|
|
@ -14,11 +14,3 @@ if CONTROL_PROTOCOL_MODBUS_UART
|
|||
source "$APP_DIR/Framework/control/ipc_protocol/modbus_uart/Kconfig"
|
||||
endif
|
||||
|
||||
config CONTROL_PROTOCOL_FREEMODBUS_TCP_SERVER
|
||||
bool "Using modbus_tcp_server control protocol"
|
||||
default n
|
||||
select CONTROL_USING_SOCKET
|
||||
select LIB_USING_FREEMODBUS
|
||||
if CONTROL_PROTOCOL_FREEMODBUS_TCP_SERVER
|
||||
source "$APP_DIR/Framework/control/ipc_protocol/freemodbustcpserver/Kconfig"
|
||||
endif
|
||||
|
|
|
@ -6,9 +6,5 @@ ifeq ($(CONFIG_CONTROL_PROTOCOL_MODBUS_UART), y)
|
|||
SRC_DIR := modbus_uart
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_CONTROL_PROTOCOL_FREEMODBUS_TCP_SERVER), y)
|
||||
SRC_DIR := freemodbustcpserver
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
#ifndef FREEMODBUSTCP_H
|
||||
#define FREEMODBUSTCP_H
|
||||
|
||||
#include <control_def.h>
|
||||
|
||||
#endif
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"device_id": 1,
|
||||
"device_name": "freemodbustcp",
|
||||
"communication_type": 0,
|
||||
"socket_config": {
|
||||
"local_ip": "192.168.250.233",
|
||||
"gateway": "192.168.250.1",
|
||||
"netmask": "255.255.255.0",
|
||||
"port": 502
|
||||
},
|
||||
"protocol_type": 2,
|
||||
"read_period": 100
|
||||
}
|
|
@ -220,7 +220,6 @@ cip_error_code_e read_value(int fd, const char *address, int length, byte_array_
|
|||
response.length = BUFFER_SIZE;
|
||||
if (cip_read_response(fd, &response))
|
||||
ret = cip_analysis_read_byte(response, out_bytes);
|
||||
// printf("%s %hu\n",__func__,response.data);
|
||||
free(response.data);
|
||||
}
|
||||
free(core_cmd.data);
|
||||
|
|
|
@ -15,5 +15,4 @@ menu "app lib"
|
|||
source "$APP_DIR/lib/embedded_database/Kconfig"
|
||||
source "$APP_DIR/lib/lorawan/Kconfig"
|
||||
source "$APP_DIR/lib/mqtt/Kconfig"
|
||||
source "$APP_DIR/lib/freemodbus/Kconfig"
|
||||
endmenu
|
||||
|
|
|
@ -22,8 +22,8 @@ ifeq ($(CONFIG_TOOL_USING_MQTT),y)
|
|||
SRC_DIR += mqtt
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIB_USING_FREEMODBUS),y)
|
||||
SRC_DIR += freemodbus
|
||||
endif
|
||||
# ifeq ($(CONFIG_LIB_USING_FREEMODBUS),y)
|
||||
# SRC_DIR += freemodbus
|
||||
# endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
menu "lib using freemodbus"
|
||||
menuconfig LIB_USING_FREEMODBUS
|
||||
bool "USING freemodbus"
|
||||
default n
|
||||
endmenu
|
|
@ -1,3 +0,0 @@
|
|||
SRC_FILES := $(wildcard ./*.c)
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -1,4 +1,4 @@
|
|||
# SRC_FILES := ping.c lwip_ping_demo.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c http_test.c
|
||||
SRC_FILES := ping.c lwip_ping_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c
|
||||
SRC_FILES := ping.c lwip_ping_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c lwip_tcp_demo.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
Loading…
Reference in New Issue