Morgan Du
/
Serial-to-Ethernet
Transfer data between UART ports and Ethernet.
Transfer data between UART ports and Ethernet.
- Support multiple UART ports.
- Support fixed IP address and DHCP.
- Support TCP server and TCP client modes.
- Support a simple web server for UART and Ethernet configuration.
Diff: main.cpp
- Revision:
- 7:cbb5a2a2a0d2
- Parent:
- 6:dc9f344f4bf0
- Child:
- 8:e9248126f512
--- a/main.cpp Fri Mar 22 09:59:52 2019 +0000 +++ b/main.cpp Wed Feb 26 07:55:51 2020 +0000 @@ -8,7 +8,8 @@ #include "ste_config.h" -/* If define USE_STATIC_IP, specify the default IP address. */ + +/* If choose static IP, specify the default IP address here. */ #if 0 // private IP address for general purpose #define IP_ADDRESS "192.168.1.2" @@ -22,7 +23,7 @@ #define GATEWAY_ADDRESS "169.254.108.1" #endif -/* Default configuration for network */ +/* Default IP configuration for Ethernet network */ S_NET_CONFIG net_config = {IP_STATIC_MODE, IP_ADDRESS, NETWORK_MASK, GATEWAY_ADDRESS}; #if defined (TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487) @@ -35,7 +36,7 @@ BufferedSerial serial_1(PG_2, PG_1, 256, 4); // UART0 BufferedSerial serial_2(PC_11, PC_10, 256, 4); // UART2 -#elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130) +#elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130) || defined(TARGET_NUMAKER_PFM_M2351) #error The board has no Ethernet. #else @@ -62,14 +63,12 @@ #endif }; -/* UART port to output debug message */ -RawSerial output(USBTX, USBRX); // UART3 on NuMaker-PFM-NUC472, UART0 on NuMaker-PFM-M487 EthernetInterface eth; -#ifdef ENABLE_WEB_CONFIG +#if ENABLE_WEB_CONFIG /* Declare the SD card as storage */ -NuSDBlockDevice bd; +NuSDBlockDevice *bd = new NuSDBlockDevice(); FATFileSystem fs("fs"); bool SD_Card_Mounted = FALSE; @@ -158,7 +157,7 @@ void bridge_net_client(S_PORT_CONFIG *pmap) { TCPSocket socket; - SocketAddress server_address; + SocketAddress server_ipaddr; nsapi_error_t err; printf("Thread %x in TCP client mode.\r\n", (unsigned int)pmap); @@ -172,7 +171,9 @@ printf("Connecting server %s:%d ...\r\n", pmap->server_addr, pmap->server_port); while(1) { - if ((err=socket.connect(pmap->server_addr, pmap->server_port)) >= 0) + server_ipaddr.set_ip_address(pmap->server_addr); + server_ipaddr.set_port(pmap->server_port); + if ((err=socket.connect(server_ipaddr)) >= 0) { printf("\r\nConnected."); break; @@ -188,7 +189,7 @@ { TCPSocket tcp_server; TCPSocket *client_socket; - SocketAddress client_address; + SocketAddress client_ipaddr; nsapi_error_t err; printf("Thread %x in TCP server mode.\r\n", (unsigned int)pmap); @@ -198,7 +199,7 @@ printf("TCP server can't open (%d)(%x).\r\n", err, (unsigned int)pmap); return; } - if ((err=tcp_server.bind(eth.get_ip_address(), pmap->port)) < 0) + if ((err=tcp_server.bind(pmap->port)) < 0) { printf("TCP server can't bind address and port (%d)(%x).\r\n", err, (unsigned int)pmap); return; @@ -219,8 +220,8 @@ } else { - client_socket->getpeername(&client_address); - printf("Connect (%d) from %s:%d ...\r\n", pmap->port, client_address.get_ip_address(), client_address.get_port()); + client_socket->getpeername(&client_ipaddr); + printf("Connect (%d) from %s:%d ...\r\n", pmap->port, client_ipaddr.get_ip_address(), client_ipaddr.get_port()); client_socket->set_timeout(1); exchange_data(pmap, client_socket); @@ -231,18 +232,22 @@ int main() { - /* Set the console baud-rate */ - output.baud(115200); + SocketAddress ip_addr; + SocketAddress ip_mask; + SocketAddress ip_gwaddr; + printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION); printf("Start Serial-to-Ethernet...\r\n"); -#ifdef ENABLE_WEB_CONFIG +#if ENABLE_WEB_CONFIG /* Restore configuration from SD card */ - SD_Card_Mounted = (fs.mount(&bd) >= 0); + printf("Mounting SD card...\r\n"); + SD_Card_Mounted = (fs.mount(bd) == 0); if (SD_Card_Mounted) { + printf("SD card mounted. Read configuration file...\r\n"); FILE *fd = fopen(SER_CONFIG_FILE, "r"); if (fd != NULL) { @@ -286,6 +291,7 @@ #endif + /* Configure serial ports */ printf("Configure UART ports...\r\n"); for(int i=0; i<MAX_UART_PORTS; i++) { @@ -293,21 +299,27 @@ port_config[i].pserial->format(port_config[i].data, port_config[i].parity, port_config[i].stop); } + /* Configure network IP address */ if (net_config.mode == IP_STATIC_MODE) { printf("Start Ethernet in Static mode.\r\n"); eth.disconnect(); - ((NetworkInterface *)ð)->set_network(net_config.ip, net_config.mask, net_config.gateway); + + ip_addr.set_ip_address(net_config.ip); + ip_mask.set_ip_address(net_config.mask); + ip_gwaddr.set_ip_address(net_config.gateway); + ((NetworkInterface *)ð)->set_network(ip_addr, ip_mask, ip_gwaddr); } else printf("Start Ethernet in DHCP mode.\r\n"); eth.connect(); - printf("IP Address is %s\r\n", eth.get_ip_address()); + eth.get_ip_address(&ip_addr); + printf("IP Address is %s\r\n", ip_addr.get_ip_address()); Thread thread[MAX_UART_PORTS]; - // Folk thread for each port + /* Folk thread for each serial port */ for(int i=0; i<MAX_UART_PORTS; i++) { if (port_config[i].mode == NET_SERVER_MODE) @@ -320,7 +332,7 @@ } } -#ifdef ENABLE_WEB_CONFIG +#if ENABLE_WEB_CONFIG /*** main thread to be a web server for configuration ***/ start_httpd();