This demonstrates the process of communicating through ethernet to a SEL-2431 Voltage Regulator Control Panel using SEL Fast Message. Basic device commands and data cna be requested and displayed over a connected serial port. This is a basic version and full testing and documentation has yet to be completed.

Dependencies:   BufferedSerial analogAverager voltageRegulator netStatReg analogMinMax CounterMinMax

Files at this revision

API Documentation at this revision

Comitter:
masterkookus
Date:
Thu Sep 19 18:14:31 2019 +0000
Parent:
5:c656fd08007b
Child:
7:be13a9037d41
Commit message:
Single struct for Server/Client Device

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
netdevices.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Sep 19 16:48:15 2019 +0000
+++ b/main.cpp	Thu Sep 19 18:14:31 2019 +0000
@@ -17,7 +17,7 @@
 
 Ticker nettimer[2];
 
-struct netsys net1,nets;
+struct netsys net1;
 
 bool polltick;
 
@@ -41,25 +41,25 @@
 
 void confignetdevices(EthernetInterface *eth)
 {   
-    nets.tcpport=23;
-    nets.eth=eth;
-    nets.isactive=false;
-    nets.closeconnection=false;
-    nets.pollTimeout=200;
-    nets.pollTime=0;
-    nets.pollInterval=200;
-    nets.clt_sock.set_blocking(false);
-    net1.pollTimeout=50;
+    net1.cltPort=23;
+    net1.srv_addr="10.150.1.241";
+    net1.cltIsActive=false;
+    net1.cltCloseConnection=false;
+    net1.pollTimeout=200;
+    net1.pollTime=0;
+    net1.pollInterval=200;
+    net1.srv_sock.set_blocking(false);
+    net1.aliveTimeout=50;
     net1.srv.set_blocking(false);
     
     /* Open the server on ethernet stack */
     net1.srv.open(eth);
-    net1.isactive=false;
-    net1.closeconnection=false;
+    net1.srvIsActive=false;
+    net1.srvCloseConnection=false;
     
     /* Bind port 23 to the server */
-    net1.tcpport=23;
-    net1.srv.bind(net1.tcpport);
+    net1.srvPort=23;
+    net1.srv.bind(net1.srvPort);
     
     /* Can handle 5 simultaneous connections */
     net1.srv.listen(5);
@@ -100,7 +100,7 @@
                 cmdpack.bytes[0]=cbuf[cbc+3];
                 if (cmdpack.cmdint==0x5469636b)
                 {
-                    //printf("Tock");
+                    printf("Tock");
                     net2->pollTime=net2->pollInterval;
                     net2->pollState=1;
                     clen=0;
@@ -108,7 +108,7 @@
                 }
             }
         }    
-        if (net2->isactive)
+        if (net2->cltIsActive)
         {
             while (net2->pollState==3)
             {
@@ -116,12 +116,12 @@
                 {
                     wait_ms(100);
                 }
-                ret = net2->clt_sock.connect("10.150.1.241",nets.tcpport);
+                ret = net2->srv_sock.connect(net2->srv_addr,net2->cltPort);
                 if (ret==0)
                 {
                     net2->connectRetry = false;
                     printf("Connected %d\r\n",ret);
-                    net2->closeconnection=true;
+                    net2->cltCloseConnection=true;
                     net2->pollState=4;
                 }
                 else
@@ -135,7 +135,7 @@
                         net2->connectRetry = false;
                         net2->pollRetryCount = 0;
                         net2->messageFailCount++;
-                        net2->closeconnection=true;
+                        net2->cltCloseConnection=true;
                         net2->pollState=0;
                     }
                 }
@@ -146,13 +146,13 @@
                 {
                     wait_ms(100);
                 }
-                ret = net2->clt_sock.send("Tock\r\n",strlen("Tock\r\n"));
+                ret = net2->srv_sock.send("Tock\r\n",strlen("Tock\r\n"));
                 if (ret>=0)
                 {
                     printf("Send Result %d\r\n",ret);
                     net2->sendRetry = false;
                     net2->txMessageCount++;
-                    net2->closeconnection=true;
+                    net2->cltCloseConnection=true;
                     net2->pollState=0;
                 }
                 else
@@ -166,15 +166,15 @@
                         net2->sendRetry = false;
                         net2->pollRetryCount = 0;
                         net2->messageFailCount++;
-                        net2->closeconnection=true;
+                        net2->cltCloseConnection=true;
                         net2->pollState=0;
                     }
                 }
             }
-            if (net2->closeconnection==true)
+            if (net2->cltCloseConnection==true)
             {
-                net2->clt_sock.close();
-                net2->isactive=false;
+                net2->srv_sock.close();
+                net2->cltIsActive=false;
             }
         }
     }
@@ -188,7 +188,7 @@
     int rxc;
     while (1)
     {
-        while (net2->isactive)
+        while (net2->srvIsActive)
         {
             rxlen=net2->clt_sock.recv(rxbuf, sizeof(rxbuf));
             if (rxlen>0)
@@ -213,7 +213,7 @@
     int rxc;
     while (1)
     {
-        while (net2->isactive)
+        while (net2->srvIsActive)
         {
             rxlen=sport0.readable();
             if (rxlen>0)
@@ -224,6 +224,7 @@
                 }
                 net2->aliveTime=0;
                 net2->clt_sock.send(rxbuf, rxlen);
+                net2->txMessageCount++;
             }
         }
     }
@@ -242,10 +243,10 @@
         {
             //printf("Connection Failed.\r\n");
         }
-        printf("Server Port %d\r\n", net2->tcpport);
+        printf("Server Port %d\r\n", net2->srvPort);
         printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port());
         net2->aliveTime=0;
-        net2->isactive=true;
+        net2->srvIsActive=true;
     }
 }
 
@@ -265,7 +266,7 @@
     rxtxdatathread[0].start(callback(datanrx,&net1));
     /* Setup Ethernet to Serial receive data Thread */
     rxtxdatathread[1].start(callback(datasrx,&net1));
-    rxtxdatathread[2].start(callback(datapolltx,&nets));
+    rxtxdatathread[2].start(callback(datapolltx,&net1));
     
     while (true) {
         
@@ -276,63 +277,63 @@
             setTick(false);
             incTime();
             currenttime=getTime();
-            nets.pollTime++;
-            if (net1.isactive)
+            net1.pollTime++;
+            if (net1.srvIsActive)
             {
                 net1.aliveTime++;
-                if (net1.aliveTime>net1.pollTimeout)
+                if (net1.aliveTime>net1.aliveTimeout)
                 {
                     printf("Closed\r\n");
                     net1.clt_sock.close();
-                    net1.isactive=false;
-                    net1.closeconnection=false;
+                    net1.srvIsActive=false;
+                    net1.srvCloseConnection=false;
                 }
                 
             }
-            if (nets.pollTime >= nets.pollInterval)
+            if (net1.pollTime >= net1.pollInterval)
             {
-                switch (nets.pollState)
+                switch (net1.pollState)
                 {
                     case 1:
                         
-                        ret=nets.clt_sock.open(&eth);
+                        ret=net1.srv_sock.open(&eth);
                         printf("Socket%d\r\n",ret);
                         if (ret < 0)
                         {
-                            nets.pollRetry = true;
-                            nets.pollRetryCount=0;
-                            nets.pollState=2;
-                            nets.pollTime=nets.pollInterval-10;
+                            net1.pollRetry = true;
+                            net1.pollRetryCount=0;
+                            net1.pollState=2;
+                            net1.pollTime=net1.pollInterval-10;
                             break;
                         }
-                        nets.pollState=3;
-                        nets.pollTime=0;
-                        nets.isactive=true;
+                        net1.pollState=3;
+                        net1.pollTime=0;
+                        net1.cltIsActive=true;
                         break;
                     case 2:
-                        ret=nets.clt_sock.open(&eth);
+                        ret=net1.srv_sock.open(&eth);
                         printf("Socket%d\r\n",ret);
                         if (ret < 0)
                         {
-                            nets.pollRetry = true;
-                            nets.pollRetryCount++;
-                            nets.pollTime=nets.pollInterval-10;
+                            net1.pollRetry = true;
+                            net1.pollRetryCount++;
+                            net1.pollTime=net1.pollInterval-10;
                             printf("Attach Attempt Failed, Code: %d\r\n",ret);
-                            if (nets.pollRetryCount>3)
+                            if (net1.pollRetryCount>3)
                             {
                                 printf("Communication Failed, Closing\r\n");
-                                nets.pollRetry = false;
-                                nets.pollRetryCount = 0;
-                                nets.messageFailCount++;
-                                nets.pollState=0;
-                                nets.pollTime=0;
+                                net1.pollRetry = false;
+                                net1.pollRetryCount = 0;
+                                net1.messageFailCount++;
+                                net1.pollState=0;
+                                net1.pollTime=0;
                             }
                             break;
                         }
-                        nets.pollState=0;
-                        nets.pollRetry = false;
-                        nets.pollTime=0;
-                        nets.isactive=true;
+                        net1.pollState=0;
+                        net1.pollRetry = false;
+                        net1.pollTime=0;
+                        net1.cltIsActive=true;
                         break;
                     default:
                         break;
--- a/netdevices.h	Thu Sep 19 16:48:15 2019 +0000
+++ b/netdevices.h	Thu Sep 19 18:14:31 2019 +0000
@@ -11,15 +11,22 @@
     TCPServer srv;
     TCPSocket clt_sock;
     SocketAddress clt_addr;
-    EthernetInterface *eth;
-    unsigned int tcpport;
-    bool isactive;
-    bool closeconnection;
+    unsigned int srvPort;
+    bool srvIsActive;
+    bool srvCloseConnection;
     unsigned int aliveTime;
+    unsigned int aliveTimeout;
+    
+    TCPSocket srv_sock;
+    unsigned int cltPort;
+    char* srv_addr;
+    bool cltIsActive;
+    bool cltCloseConnection;
     unsigned int pollTimeout;
     unsigned int pollTime;
     unsigned int pollInterval;
     unsigned int pollState;
+    
     bool pollRetry;
     bool sendRetry;
     bool connectRetry;