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:
- 8:e9248126f512
- Parent:
- 7:cbb5a2a2a0d2
--- a/main.cpp Wed Feb 26 07:55:51 2020 +0000 +++ b/main.cpp Fri Feb 26 06:30:37 2021 +0000 @@ -5,10 +5,8 @@ * * */ - #include "ste_config.h" - /* If choose static IP, specify the default IP address here. */ #if 0 // private IP address for general purpose @@ -27,14 +25,26 @@ 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) +#if MBED_MAJOR_VERSION <= 5 BufferedSerial serial_0(PB_3, PB_2, 256, 4); // UART1 BufferedSerial serial_1(PA_5, PA_4, 256, 4); // UART5 //BufferedSerial serial_2(PB_13, PB_12, 256, 4); // UART0, Debug +#else +BufferedSerial serial_0(PB_3, PB_2); // UART1 +BufferedSerial serial_1(PA_5, PA_4); // UART5 +//BufferedSerial serial_2(PB_13, PB_12); // UART0, Debug +#endif #elif defined (TARGET_NUMAKER_PFM_NUC472) +#if MBED_MAJOR_VERSION <= 5 BufferedSerial serial_0(PH_1, PH_0, 256, 4); // UART4 BufferedSerial serial_1(PG_2, PG_1, 256, 4); // UART0 BufferedSerial serial_2(PC_11, PC_10, 256, 4); // UART2 +#else +BufferedSerial serial_0(PH_1, PH_0); // UART4 +BufferedSerial serial_1(PG_2, PG_1); // UART0 +BufferedSerial serial_2(PC_11, PC_10); // UART2 +#endif #elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130) || defined(TARGET_NUMAKER_PFM_M2351) #error The board has no Ethernet. @@ -123,8 +133,17 @@ /*** Serial to Network ***/ // try to get more data from serial port +#if MBED_MAJOR_VERSION <= 5 for(; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++) s_buf[s_index] = pmap->pserial->getc(); +#else + if (pmap->pserial->readable()) + { + s_len = pmap->pserial->read(s_buf+s_index, sizeof(s_buf)-s_index); + if (s_len > 0) + s_index += s_len; + } +#endif if (s_index >= 240 || (s_index != 0 && ++eth_tx_count >= 5)) { @@ -162,7 +181,7 @@ printf("Thread %x in TCP client mode.\r\n", (unsigned int)pmap); - if ((err=socket.open(ð)) < 0) + if ((err=socket.open(ð)) != NSAPI_ERROR_OK) { printf("TCP socket can't open (%d)(%x).\r\n", err, (unsigned int)pmap); return; @@ -194,12 +213,12 @@ printf("Thread %x in TCP server mode.\r\n", (unsigned int)pmap); - if ((err=tcp_server.open(ð)) < 0) + if ((err=tcp_server.open(ð)) != NSAPI_ERROR_OK) { printf("TCP server can't open (%d)(%x).\r\n", err, (unsigned int)pmap); return; } - if ((err=tcp_server.bind(pmap->port)) < 0) + if ((err=tcp_server.bind(pmap->port)) != NSAPI_ERROR_OK) { printf("TCP server can't bind address and port (%d)(%x).\r\n", err, (unsigned int)pmap); return; @@ -213,7 +232,7 @@ while(1) { client_socket = tcp_server.accept(&err); - if (err < 0) // && err != NSAPI_ERROR_WOULD_BLOCK) + if (err != NSAPI_ERROR_OK && err != NSAPI_ERROR_WOULD_BLOCK) { printf("TCP server fail to accept connection (%d)(%x).\r\n", err, (unsigned int)pmap); return; @@ -295,8 +314,13 @@ printf("Configure UART ports...\r\n"); for(int i=0; i<MAX_UART_PORTS; i++) { +#if MBED_MAJOR_VERSION <= 5 port_config[i].pserial->baud(port_config[i].baud); port_config[i].pserial->format(port_config[i].data, port_config[i].parity, port_config[i].stop); +#else + port_config[i].pserial->set_baud(port_config[i].baud); + port_config[i].pserial->set_format(port_config[i].data, port_config[i].parity, port_config[i].stop); +#endif } /* Configure network IP address */