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

Revision:
11:d40adc7de05f
Parent:
10:9da863a6da3e
Child:
12:4bb088c27838
--- a/main.cpp	Wed Oct 02 12:12:30 2019 +0000
+++ b/main.cpp	Wed Oct 02 19:16:25 2019 +0000
@@ -2,6 +2,8 @@
     #error [NOT_SUPPORTED] LWIP not supported for this target
 #endif
 
+//#define netmsgdebug
+
 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "TCPServer.h"
@@ -14,12 +16,19 @@
 #include "netDataTypes.h"
 #include "selMsg.h"
 #include "string.h"
+#include "analogAverager.h"
+#include "analogMinMax.h"
+#include "counterMinMax.h"
 
 Ticker nettimer;
 
 struct netsys net1;
 struct vRegData vReg1;
 
+analogAverager voltageAvg(120,true,0.9,true,1.1);
+analogMinMax voltageMinMax(120,true,0.9,true,1.1);
+counterMinMax tapMinMax(0,true,-16,true,16);
+
 bool polltick;
 
 BufferedSerial sport0(sport0tx, sport0rx, sport0buff, sport0mult);    // UART2
@@ -191,14 +200,18 @@
                     //Ensure Analog channl count is correct
                     if (vReg1.numAnalog!=cbuf[6])
                     {
+                        #ifdef netmsgdebug
                         printf("\r\nFast Meter Analog Channel Count Off\r\n");
+                        #endif
                         clen=0;
                         break;
                     }
                     //Ensure Digital register count is correct
                     if (vReg1.numDigital!=cbuf[8])
                     {
+                        #ifdef netmsgdebug
                         printf("\r\nFast Meter Digital Channel Count Off\r\n");
+                        #endif
                         clen=0;
                         break;
                     }
@@ -211,7 +224,9 @@
                         {
                             if(vReg1.analogs[txc].analogName[nxc]!=cbuf[anum+nxc])
                             {
+                                #ifdef netmsgdebug
                                 printf("\r\nPoint %d Failed\r\n",txc);
+                                #endif
                                 break;
                             }
                         }
@@ -252,6 +267,9 @@
                         vReg1.digitalTargets[txc]=cbuf[anum+txc];
                     }
                     clen=0;
+                    tapMinMax.putVal(vReg1.analogs[7].analog1Value);
+                    voltageMinMax.putVal(vReg1.analogs[9].analog1Value);
+                    voltageAvg.putVal(vReg1.analogs[9].analog1Value);
                     break;
                 }
                 //Check fast demand meter configuration
@@ -261,7 +279,9 @@
                     //Ensure Analog channl count is correct
                     if (vReg1.numDemAnalog!=cbuf[6])
                     {
+                        #ifdef netmsgdebug
                         printf("\r\nDemand Analog Channel Count Off\r\n");
+                        #endif
                         clen=0;
                         break;
                     }
@@ -274,8 +294,9 @@
                         {
                             if(vReg1.analogs[txc+vReg1.numAnalog].analogName[nxc]!=cbuf[anum+nxc])
                             {
-                                printf("%c %c\r\n",vReg1.analogs[txc+vReg1.numAnalog].analogName[nxc],cbuf[anum+nxc]);
+                                #ifdef netmsgdebug
                                 printf("\r\nPoint %d Failed\r\n",txc);
+                                #endif
                                 break;
                             }
                         }
@@ -298,7 +319,7 @@
                         valpack.bytes[0]=cbuf[anum+3];
                         vReg1.analogs[txc].analog1Value = valpack.cmdflt;
                         anum = anum + 4;
-                        printf("%.2f\r\n",vReg1.analogs[txc].analog1Value);
+                        //printf("%.2f\r\n",vReg1.analogs[txc].analog1Value);
                     }
                     vReg1.demTimeStamp.month=cbuf[anum];
                     vReg1.demTimeStamp.day=cbuf[anum+1];
@@ -309,13 +330,25 @@
                     timepack.bytes[1]=cbuf[anum+6];
                     timepack.bytes[0]=cbuf[anum+7];
                     vReg1.demTimeStamp.msec=timepack.cmdshort;
-                    printf("%d/%d/%d %d:%d:%d.%d\r\n",vReg1.demTimeStamp.month,vReg1.demTimeStamp.day,vReg1.demTimeStamp.year,vReg1.demTimeStamp.hour,vReg1.demTimeStamp.min,vReg1.demTimeStamp.sec,vReg1.demTimeStamp.msec);
+                    sport0.printf("%d/%d/%d %d:%d:%d.%d\r\n",vReg1.demTimeStamp.month,vReg1.demTimeStamp.day,vReg1.demTimeStamp.year,vReg1.demTimeStamp.hour,vReg1.demTimeStamp.min,vReg1.demTimeStamp.sec,vReg1.demTimeStamp.msec);
+                    vReg1.calculated[0].analog1Value=voltageMinMax.getMin();
+                    vReg1.calculated[1].analog1Value=voltageAvg.getAvg();
+                    vReg1.calculated[2].analog1Value=voltageMinMax.getMax();
+                    vReg1.calculated[3].analog1Value=tapMinMax.getMin();
+                    vReg1.calculated[4].analog1Value=tapMinMax.getMax();
+                    sport0.printf("%.2f\r\n",vReg1.calculated[0].analog1Value);
+                    sport0.printf("%.2f\r\n",vReg1.calculated[1].analog1Value);
+                    sport0.printf("%.2f\r\n",vReg1.calculated[2].analog1Value);
+                    sport0.printf("%.2f\r\n",vReg1.calculated[3].analog1Value);
+                    sport0.printf("%.2f\r\n",vReg1.calculated[4].analog1Value);
                     break;
                 }
             }
             if (clen>0)
             {
+                #ifdef netmsgdebug
                 printf("Command not Received\r\n");
+                #endif
                 clen=0;
             }
         }
@@ -343,13 +376,17 @@
                 if (ret==0)
                 {
                     net2->connectRetry = false;
+                    #ifdef netmsgdebug
                     printf("Connected %d\r\n",ret);
+                    #endif
                     net2->sendState=4;
                 }
                 //If connect attempt failed check to see if may already be connected
                 else if (ret==-3015)
                 {
+                    #ifdef netmsgdebug
                     printf("May already be connected, attempting send.\r\n");
+                    #endif
                     net2->connectRetry = false;
                     net2->sendState=4;
                 }
@@ -358,11 +395,15 @@
                 {
                     net2->connectRetry = true;
                     net2->sendRetryCount++;
+                    #ifdef netmsgdebug
                     printf("Connect Attempt Failed, Code: %d\r\n",ret);
+                    #endif
                     //If connect attempt failed exceeds 3 then end attempt
                     if (net2->sendRetryCount>3)
                     {
+                        #ifdef netmsgdebug
                         printf("Communication Failed, Closing\r\n");
+                        #endif
                         net2->connectRetry = false;
                         net2->sendRetryCount = 0;
                         net2->messageFailCount++;
@@ -385,7 +426,9 @@
                 //If data send successful proceed to wait for response
                 if (ret>=0)
                 {
+                    #ifdef netmsgdebug
                     printf("Send Result %d\r\n",ret);
+                    #endif
                     net2->sendRetry = false;
                     net2->txMessageCount++;
                     net2->sendState=5;
@@ -396,11 +439,15 @@
                 {
                     net2->sendRetry = true;
                     net2->sendRetryCount++;
+                    #ifdef netmsgdebug
                     printf("Send Attempt Failed, Code: %d\r\n",ret);
+                    #endif
                     //If send attempt failed exceeds 3 then end attempt
                     if (net2->sendRetryCount>3)
                     {
+                        #ifdef netmsgdebug
                         printf("Communication Failed, Closing\r\n");
+                        #endif
                         net2->sendRetry = false;
                         net2->sendRetryCount = 0;
                         net2->messageFailCount++;
@@ -435,7 +482,9 @@
                 {
                     receiveBuffer.push(rxbuf[rxc]);
                 }
+                #ifdef netmsgdebug
                 printf("Client Received Data\r\n");
+                #endif
                 //Increment received message counter
                 net2->rxMessageCount++;
             }
@@ -464,7 +513,9 @@
                 {
                     receiveBuffer.push(rxbuf[rxc]);
                 }
+                #ifdef netmsgdebug
                 printf("Server Received Data\r\n");
+                #endif
                 //Increment received message counter
                 net2->rxMessageCount++;
             }
@@ -519,7 +570,9 @@
                     //Search for the Min/Max command
                     if (cmdpack.cmdint==0x4d45544d)
                     {
+                        #ifdef netmsgdebug
                         printf("MIN MAX METERING\r\n");
+                        #endif
                         net2->devMsgReq=1;
                         net2->devMsgPos=2;
                         rxindex=0;
@@ -527,7 +580,9 @@
                     //Search for the Tap Report Command
                     if (cmdpack.cmdint==0x54415052)
                     {
+                        #ifdef netmsgdebug
                         printf("TAP REPORT\r\n");
+                        #endif
                         net2->devMsgReq=2;
                         net2->devMsgPos=2;
                         rxindex=0;
@@ -536,7 +591,9 @@
                 //If no command found set cursor location to zero
                 if (rxindex>0)
                 {
+                    #ifdef netmsgdebug
                     printf("Serial Command Not Found");
+                    #endif
                     rxindex=0;
                 }
             }
@@ -558,8 +615,10 @@
         {
             //printf("Connection Failed.\r\n");
         }
+        #ifdef netmsgdebug
         printf("Server Port %d\r\n", net2->srvPort);
         printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port());
+        #endif
         net2->aliveTime=0;
         net2->srvIsActive=true;
     }
@@ -582,8 +641,9 @@
     
     initVoltageRegulator(&vReg1);
     
+    #ifdef netmsgdebug
     printf("The target IP address is '%s'\r\n", eth.get_ip_address());
-    printf("%s\r\n",vReg1.analogs[0].analogName);
+    #endif
     confignetdevices(&eth);
     /* Setup Ethernet to Serial Connection Thread */
     conchkthread[0].start(callback(conchk,&net1));
@@ -618,7 +678,9 @@
                 net1.aliveTime++;
                 if (net1.aliveTime>net1.aliveTimeout)
                 {
+                    #ifdef netmsgdebug
                     printf("Closed\r\n");
+                    #endif
                     net1.clt_sock.close();
                     net1.srvIsActive=false;
                     net1.srvCloseConnection=false;
@@ -723,14 +785,20 @@
                     {
                         if (net1.pollRequestSent)
                         {
+                            #ifdef netmsgdebug
                             printf("Poll Request Sent\r\n");
+                            #endif
                             if (net1.pollResponseReceived==false)
                             {
+                                #ifdef netmsgdebug
                                 printf("Poll Response Not Received\r\n");
+                                #endif
                             }
                             else
                             {
+                                #ifdef netmsgdebug
                                 printf("Poll Response Received\r\n");
+                                #endif
                             }
                             net1.pollRequestSent=false;
                             net1.pollResponseReceived=false;
@@ -754,7 +822,9 @@
                             {
                                 net1.pollTimeout=0;
                                 net1.serTimeoutCount++;
+                                #ifdef netmsgdebug
                                 printf("Serial Active Timeout Count: %d\r\n",net1.serTimeoutCount);
+                                #endif
                             }
                         }
                     }
@@ -782,12 +852,16 @@
                             net1.sendState=0;
                         }
                         ret=net1.srv_sock.open(&eth);
+                        #ifdef netmsgdebug
                         printf("Socket%d\r\n",ret);
+                        #endif
                         if (ret < 0)
                         {
                             if (ret==-3003)
                             {
+                                #ifdef netmsgdebug
                                 printf("May already be attached, attempting connect.\r\n");
+                                #endif
                             }
                             else
                             {
@@ -804,16 +878,22 @@
                         break;
                     case 2:
                         ret=net1.srv_sock.open(&eth);
+                        #ifdef netmsgdebug
                         printf("Socket%d\r\n",ret);
+                        #endif
                         if (ret < 0)
                         {
                             net1.attachRetry = true;
                             net1.sendRetryCount++;
                             net1.sendTime=0;
+                            #ifdef netmsgdebug
                             printf("Attach Attempt Failed, Code: %d\r\n",ret);
+                            #endif
                             if (net1.sendRetryCount>3)
                             {
+                                #ifdef netmsgdebug
                                 printf("Communication Failed, Closing\r\n");
+                                #endif
                                 net1.attachRetry = false;
                                 net1.sendRetryCount = 0;
                                 net1.messageFailCount++;
@@ -852,7 +932,9 @@
         }
         if (net1.cltCloseConnection==true)
         {
+            #ifdef netmsgdebug
             printf("Client Socket Closed\r\n");
+            #endif
             net1.srv_sock.close();
             net1.cltIsActive=false;
             net1.cltCloseConnection=false;