rs485 to ethernet

Dependencies:   BufferedSerialttt

Fork of Serial-to-Ethernet by Morgan Du

Revision:
1:c53f82eb6b42
Parent:
0:11bc39d0f367
Child:
2:1af7ad1f058a
--- a/main.cpp	Fri May 05 07:40:19 2017 +0000
+++ b/main.cpp	Fri Sep 01 02:30:10 2017 +0000
@@ -25,14 +25,16 @@
 S_NET_CONFIG net_config = {IP_STATIC_MODE, IP_ADDRESS, NETWORK_MASK, GATEWAY_ADDRESS};
 
 #if defined (TARGET_NUMAKER_PFM_M487)
-#error Not defined UART ports yet.
+BufferedSerial serial_0(PH_8, PH_9, 256, 4);    // UART1
+BufferedSerial serial_1(PA_5, PA_4, 256, 4);    // UART5
+//BufferedSerial serial_1(PC_12, PC_11, 256, 4);  // UART0, Debug
 
 #elif defined (TARGET_NUMAKER_PFM_NUC472)
 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
 
-#elif defined (TARGET_NUMAKER_PFM_M453)
+#elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130)
 #error The board has no Ethernet.
 
 #else
@@ -84,17 +86,23 @@
     unsigned char s_buf[256];
     int n_len = 0, n_index = 0;
     int s_len = 0, s_index = 0;
+    unsigned int eth_tx_count = 0;
     
     while(1)
     {   
         /*** Network to Serial ***/
         
-        if (n_len < 0 || n_len == n_index)
+        if (n_len == 0)
         {
             // net buffer is empty, try to get new data from network.
             n_len = psocket->recv(n_buf, sizeof(n_buf));
-            if (n_len == NSAPI_ERROR_WOULD_BLOCK)
+            if (n_len == 0)
             {
+                eth_tx_count += 3;
+            }
+            else if (n_len == NSAPI_ERROR_WOULD_BLOCK)
+            {
+                n_len = 0;
             }
             else if (n_len < 0)
             {
@@ -102,53 +110,52 @@
                 psocket->close();
                 break;
             }
-            else
-            {
-                n_index = 0;
-            }
         }
         else
         {
-            // send data to serial port.
-            for(;n_index < n_len && pmap->pserial->writeable(); n_index++)
+            n_index += pmap->pserial->write(n_buf+n_index, n_len-n_index);
+            if (n_index == n_len)
             {
-                pmap->pserial->putc(n_buf[n_index]);
+                n_len = 0;
+                n_index = 0;
             }
         }
 
         /*** Serial to Network ***/
+
+        // try to get more data from serial port
+        for(; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++)
+            s_buf[s_index] = pmap->pserial->getc();
         
-        if (pmap->pserial->readable())
+        if (s_index >= 240 || (s_index != 0 && ++eth_tx_count >= 5))
         {
-            // try to get more data from serial port
-            for(s_index = 0; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++)
-                s_buf[s_index] = pmap->pserial->getc();
-            
-            // send all data to network.
-            if (s_index > 0)
+            s_len = psocket->send(s_buf, s_index); 
+
+            if (s_len == NSAPI_ERROR_WOULD_BLOCK)
+            {
+                printf("Socket Send no block.\r\n");
+            }               
+            else if (s_len < 0)
             {
-                s_len = psocket->send(s_buf, s_index); 
-                if (s_len == NSAPI_ERROR_WOULD_BLOCK)
-                {
-                    printf("Socket Send no block.\r\n");
-                }               
-                else if (s_len < 0)
-                {
-                    printf("Socket Send Err (%d)\r\n", s_len);
-                    psocket->close();
-                    break;
-                }
-                else if (s_len != s_index)
-                {
-                    printf("Socket Send not complete.\r\n");
-                    psocket->close();
-                    break;
-                }
+                printf("Socket Send Err (%d)\r\n", s_len);
+                psocket->close();
+                break;
             }
-        }
+            else // s_len >= s_index
+            {
+                unsigned int i;
+
+                // move remain data if existed.
+                for(i=0; s_len < s_index; i++, s_len++)
+                    s_buf[i] = s_buf[s_len];
+
+                s_index = i;
+                eth_tx_count = 0;
+            }
+        }       
     }
 }   
-
+   
 void bridge_net_client(S_PORT_CONFIG *pmap)
 {
     TCPSocket socket;
@@ -167,11 +174,12 @@
     while(1)
     {
         if ((err=socket.connect(pmap->server_addr, pmap->server_port)) >= 0)
+        {
+            printf("\r\nConnected.");
             break;
+        }
     }
     
-    printf("\r\nConnected.");
-
     socket.set_timeout(1);
     exchange_data(pmap, &socket);
 }