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:
15:9ad261a27883
Parent:
14:3d437acb92e3
Child:
16:018bed9abe89
--- a/main.cpp	Mon Oct 07 19:40:36 2019 +0000
+++ b/main.cpp	Fri Oct 11 16:32:45 2019 +0000
@@ -25,12 +25,18 @@
 Timer netDevTimers[7];
 
 struct netsys net1;
-netDevStatus netStat(0,0,0,0,0);
+netDevStatus netStat(0,0,0,0,0,setConsFailCount);
 
 struct vRegData vReg1;
 
 analogAverager voltageAvg(120,true,true,0.9,true,true,1.1);
 analogMinMax voltageMinMax(120,true,true,0.9,true,true,1.1);
+analogAverager currentAvg(50,true,false,-100,true,false,100);
+analogMinMax currentMinMax(50,true,false,-100,true,false,100);
+analogAverager powerAvg(360000,true,false,720000,true,false,720000);
+analogMinMax powerMinMax(360000,true,false,-720000,true,false,720000);
+analogAverager reactiveAvg(0,true,false,-360000,true,false,360000);
+analogMinMax reactiveMinMax(0,true,false,-360000,true,false,360000);
 counterMinMax tapMinMax(0,true,-16,true,16);
 
 bool polltick;
@@ -179,9 +185,9 @@
     
     net1.cltPort=setclientport;
     net1.srv_addr=setclientaddress;
-    net1.srv_sock.set_blocking(true);
+    net1.srv_sock.set_blocking(false);
     net1.srv_sock.set_timeout(100);
-    net1.srv.set_blocking(true);
+    net1.srv.set_blocking(false);
     net1.srv.set_timeout(100);
     net1.sendRetryCount=0;
     net1.messageFailCount=0;
@@ -231,6 +237,7 @@
                 sport0.write(cbuf,clen);
                 netDevTimers[5].reset();
                 clen=0;
+                netStat.iedOnline(true);
             }
         }
         //Check to see if at least 4 characters have been received
@@ -246,6 +253,7 @@
                     netStat.bit_toclear(fmCfgInProg);
                     netStat.bit_toset(devOnline);
                     clen=0;
+                    netStat.iedOnline(true);
                     break;
                 }
                 //Check to see if serial commands are currently being received
@@ -259,6 +267,7 @@
                         netDevTimers[5].reset();
                         netStat.setDevMsgReq(0);
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //If relay acknowledges tap report command set bulk receive flag
@@ -269,6 +278,7 @@
                         netDevTimers[5].reset();
                         netStat.setDevMsgReq(0);
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //If relay acknowledges level 1 login attempt increment command pointer to password
@@ -279,6 +289,7 @@
                         sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                         netDevTimers[5].reset();
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //If relay sends mask character increment command pointer to send \r\n
@@ -289,6 +300,7 @@
                         sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                         netDevTimers[5].reset();
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //If relay is in access level 1 or higher increment command pointer to desired command
@@ -299,6 +311,7 @@
                         sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                         netDevTimers[5].reset();
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //If relay in level 0 increment command pointer to access command
@@ -309,6 +322,7 @@
                         sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                         netDevTimers[5].reset();
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                 }
@@ -357,6 +371,7 @@
                         //bit_toset(net2->statusReg,fmEnabled);
                         netStat.bit_toset(fmEnabled);
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //Receive data
@@ -392,6 +407,13 @@
                         tapMinMax.putVal(vReg1.analogs[7].analog1Value);
                         voltageMinMax.putVal(vReg1.analogs[9].analog1Value);
                         voltageAvg.putVal(vReg1.analogs[9].analog1Value);
+                        currentMinMax.putVal(vReg1.analogs[0].analog1Value);
+                        currentAvg.putVal(vReg1.analogs[0].analog1Value);
+                        powerMinMax.putVal(vReg1.analogs[11].analog1Value);
+                        powerAvg.putVal(vReg1.analogs[11].analog1Value);
+                        reactiveMinMax.putVal(vReg1.analogs[12].analog1Value);
+                        reactiveAvg.putVal(vReg1.analogs[12].analog1Value);
+                        netStat.iedOnline(true);
                         break;
                     }
                 }
@@ -429,6 +451,7 @@
                         }
                         netStat.bit_toset(fmdEnabled);
                         clen=0;
+                        netStat.iedOnline(true);
                         break;
                     }
                     //Receive demand data
@@ -455,20 +478,7 @@
                         timepack.bytes[0]=cbuf[anum+7];
                         vReg1.demTimeStamp.msec=timepack.cmdshort;
                         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);
-                        
-                        voltageAvg.resetNum();
-                        voltageMinMax.resetNum();
-                        tapMinMax.resetNum();*/
+                        netStat.iedOnline(true);
                         break;
                     }
                 }
@@ -494,17 +504,11 @@
             //Attempt to connect to server
             while (net2->sendState==3)
             {
-                //delay connect attempt if previous attempt unsuccessful
-                if (netStat.bit_isset(connectRetry))
-                {
-                    wait_ms(100);
-                }
                 //Attempt to connect
                 ret = net2->srv_sock.connect(net2->srv_addr,net2->cltPort);
                 //If connect successful proceed to send data
                 if (ret==0)
                 {
-                    netStat.bit_toclear(connectRetry);
                     #ifdef netmsgdebug
                     printf("Connected %d\r\n",ret);
                     #endif
@@ -516,39 +520,22 @@
                     #ifdef netmsgdebug
                     printf("May already be connected, attempting send.\r\n");
                     #endif
-                    netStat.bit_toclear(connectRetry);
                     net2->sendState=4;
                 }
-                //If connect attempt failed on other error attempt reconnect
                 else
                 {
-                    netStat.bit_toset(connectRetry);
-                    net2->sendRetryCount++;
                     #ifdef netmsgdebug
-                    printf("Connect Attempt Failed, Code: %d\r\n",ret);
+                    printf("Communication Failed, Closing\r\n");
                     #endif
-                    //If connect attempt failed exceeds 3 then end attempt
-                    if (net2->sendRetryCount>3)
-                    {
-                        #ifdef netmsgdebug
-                        printf("Communication Failed, Closing\r\n");
-                        #endif
-                        netStat.bit_toclear(connectRetry);
-                        net2->sendRetryCount = 0;
-                        net2->messageFailCount++;
-                        netStat.bit_toset(cltCloseConnection);
-                        net2->sendState=0;
-                    }
+                    net2->sendRetryCount = 0;
+                    netStat.iedOnline(false);
+                    netStat.bit_toset(cltCloseConnection);
+                    net2->sendState=0;
                 }
             }
             //Attempt to send data
             while (net2->sendState==4)
             {
-                //Delay send data attempt if previous attempt failed
-                if (netStat.bit_isset(sendRetry))
-                {
-                    wait_ms(100);
-                }
                 //Attempt to send data
                 ret = net2->srv_sock.send(net2->sendString,net2->sendLen);
                 //If data send successful proceed to wait for response
@@ -557,31 +544,28 @@
                     #ifdef netmsgdebug
                     printf("Send Result %d\r\n",ret);
                     #endif
-                    netStat.bit_toclear(sendRetry);
-                    //net2->txMessageCount++;
                     netStat.incCount(1);
                     net2->sendState=5;
+                    /*if ((netStat.bit_isset(fmPollReq)) || (netStat.bit_isset(fmdPollReq)))
+                    {
+                        netDevTimers[4].reset();
+                        netDevTimers[4].start();
+                    }
+                    if (netStat.bit_isset(serPollReq))
+                    {
+                        netDevTimers[5].reset();
+                        netDevTimers[5].start();
+                    }*/
                 }
-                //If send attempt failed attempt to re-send data
                 else
                 {
-                    netStat.bit_toset(sendRetry);
-                    net2->sendRetryCount++;
                     #ifdef netmsgdebug
-                    printf("Send Attempt Failed, Code: %d\r\n",ret);
+                    printf("Communication Failed, Closing\r\n");
                     #endif
-                    //If send attempt failed exceeds 3 then end attempt
-                    if (net2->sendRetryCount>3)
-                    {
-                        #ifdef netmsgdebug
-                        printf("Communication Failed, Closing\r\n");
-                        #endif
-                        netStat.bit_toclear(sendRetry);
-                        net2->sendRetryCount = 0;
-                        net2->messageFailCount++;
-                        netStat.bit_toset(cltCloseConnection);
-                        net2->sendState=0;
-                    }
+                    net2->sendRetryCount = 0;
+                    netStat.iedOnline(false);
+                    netStat.bit_toset(cltCloseConnection);
+                    net2->sendState=0;
                 }
             }
         }
@@ -717,7 +701,12 @@
                         #ifdef netmsgdebug
                         printf("STATS\r\n");
                         #endif
-                        sport0.printf("Status %08x\r\n",netStat.getStatus());
+                        sport0.printf("\r\nStatus %08x\r\n",netStat.getStatus());
+                        sport0.printf("Messages Sent: %d\r\n",netStat.getCount(1));
+                        sport0.printf("Messages Received: %d\r\n",netStat.getCount(2));
+                        sport0.printf("Fast Message Timeouts: %d\r\n",netStat.getCount(3));
+                        sport0.printf("Serial Timeouts: %d\r\n",netStat.getCount(4));
+                        sport0.printf("Messages Failed: %d\r\n\r\n",netStat.getCount(5));
                         rxindex=0;
                     }
                 }
@@ -857,73 +846,94 @@
                     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);
+                    vReg1.calculated[3].analog1Value=currentMinMax.getMin();
+                    vReg1.calculated[4].analog1Value=currentAvg.getAvg();
+                    vReg1.calculated[5].analog1Value=currentMinMax.getMax();
+                    vReg1.calculated[6].analog1Value=powerMinMax.getMin();
+                    vReg1.calculated[7].analog1Value=powerAvg.getAvg();
+                    vReg1.calculated[8].analog1Value=powerMinMax.getMax();
+                    vReg1.calculated[9].analog1Value=reactiveMinMax.getMin();
+                    vReg1.calculated[10].analog1Value=reactiveAvg.getAvg();
+                    vReg1.calculated[11].analog1Value=reactiveMinMax.getMax();
+                    vReg1.calculated[12].analog1Value=tapMinMax.getMin();
+                    vReg1.calculated[13].analog1Value=tapMinMax.getMax();
+                    sport0.printf(
+                    "%2d:%2d  ,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7.2f,%7d,%7d,%7d,%7d,%7d,%7d,%7d,%7d\r\n",
+                    vReg1.timeStamp.hour,vReg1.timeStamp.min,
+                    vReg1.calculated[0].analog1Value,vReg1.calculated[1].analog1Value,vReg1.calculated[2].analog1Value,
+                    vReg1.calculated[3].analog1Value,vReg1.calculated[4].analog1Value,vReg1.calculated[5].analog1Value,
+                    vReg1.calculated[6].analog1Value,vReg1.calculated[7].analog1Value,vReg1.calculated[8].analog1Value,
+                    vReg1.calculated[9].analog1Value,vReg1.calculated[10].analog1Value,vReg1.calculated[11].analog1Value,
+                    vReg1.calculated[12].analog1Value,vReg1.calculated[13].analog1Value);
                     
                     voltageAvg.resetNum();
                     voltageMinMax.resetNum();
+                    currentAvg.resetNum();
+                    currentMinMax.resetNum();
+                    powerAvg.resetNum();
+                    powerMinMax.resetNum();
+                    reactiveAvg.resetNum();
+                    reactiveMinMax.resetNum();
                     tapMinMax.resetNum();
                     
                     netStat.bit_toclear(recDataFile);
                     netDevTimers[6].reset();
                     netDevTimers[6].start();
                 }
-                if (netStat.bit_isset(fmdPollReq))
+                if ((net1.sendState==0) || (net1.sendState==5))
                 {
-                    if (netStat.bit_isset(fmdEnabled))
-                    {
-                        printf("Demand Reading\r\n");
-                        sendCmd(4);
-                        netStat.bit_toclear(fmdPollReq);
-                        netStat.bit_toset(fmdPollInProg);
-                    }
-                    else
+                    if (netStat.bit_isset(fmdPollReq))
                     {
-                        printf("Demand Configuration\r\n");
-                        sendCmd(3);
-                        netStat.bit_toclear(fmdPollReq);
-                        netStat.bit_toset(fmdPollInProg);
+                        if (netStat.bit_isset(fmdEnabled))
+                        {
+                            printf("Demand Reading\r\n");
+                            sendCmd(4);
+                            netStat.bit_toclear(fmdPollReq);
+                            netStat.bit_toset(fmdPollInProg);
+                        }
+                        else
+                        {
+                            printf("Demand Configuration\r\n");
+                            sendCmd(3);
+                            netStat.bit_toclear(fmdPollReq);
+                            netStat.bit_toset(fmdPollInProg);
+                        }
+                        netDevTimers[4].reset();
+                        netDevTimers[4].start();
                     }
-                    netDevTimers[4].reset();
-                    netDevTimers[4].start();
-                }
-                else if (netStat.bit_isset(fmPollReq))
-                {
-                    if (netStat.bit_isset(fmEnabled))
+                    else if (netStat.bit_isset(fmPollReq))
                     {
-                        printf("Meter Reading\r\n");
-                        sendCmd(2);
-                        netStat.bit_toclear(fmPollReq);
-                        netStat.bit_toset(fmPollInProg);
-                    }
-                    else
-                    {
-                        printf("Meter Configuration\r\n");
-                        sendCmd(1);
-                        netStat.bit_toclear(fmPollReq);
-                        netStat.bit_toset(fmPollInProg);
+                        if (netStat.bit_isset(fmEnabled))
+                        {
+                            printf("Meter Reading\r\n");
+                            sendCmd(2);
+                            netStat.bit_toclear(fmPollReq);
+                            netStat.bit_toset(fmPollInProg);
+                        }
+                        else
+                        {
+                            printf("Meter Configuration\r\n");
+                            sendCmd(1);
+                            netStat.bit_toclear(fmPollReq);
+                            netStat.bit_toset(fmPollInProg);
+                        }
+                        netDevTimers[4].reset();
+                        netDevTimers[4].start();
                     }
-                    netDevTimers[4].reset();
-                    netDevTimers[4].start();
-                }
-                else if (netStat.bit_isset(serPollReq))
-                {
-                    if (netStat.bit_isset(serEnabled))
+                    else if (netStat.bit_isset(serPollReq))
                     {
-                        sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
-                        netStat.bit_toclear(serPollReq);
-                        netStat.bit_toset(serPollInProg);
-                        netDevTimers[5].reset();
-                        netDevTimers[5].start();
-                    }
-                    else
-                    {
-                        netStat.bit_toclear(serPollReq);
+                        if (netStat.bit_isset(serEnabled))
+                        {
+                            sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
+                            netStat.bit_toclear(serPollReq);
+                            netStat.bit_toset(serPollInProg);
+                            netDevTimers[5].reset();
+                            netDevTimers[5].start();
+                        }
+                        else
+                        {
+                            netStat.bit_toclear(serPollReq);
+                        }
                     }
                 }
             }
@@ -943,7 +953,6 @@
         if (sendtick)
         {
             setSendTick(false);
-            //sport0.printf("Status %08x\r\n",netStat.getStatus());
             switch (net1.sendState)
             {
                 case 0:
@@ -973,43 +982,18 @@
                         }
                         else
                         {
-                            netStat.bit_toset(attachRetry);
-                            net1.sendRetryCount=0;
-                            net1.sendState=2;
+                            #ifdef netmsgdebug
+                            printf("Communication Failed, Closing\r\n");
+                            #endif
+                            net1.sendRetryCount = 0;
+                            netStat.iedOnline(false);
+                            net1.sendState=0;
                             break;
                         }
                     }
                     net1.sendState=3;
                     netStat.bit_toset(cltIsActive);
                     break;
-                case 2:
-                    ret=net1.srv_sock.open(&eth);
-                    #ifdef netmsgdebug
-                    printf("Socket%d\r\n",ret);
-                    #endif
-                    if (ret < 0)
-                    {
-                        netStat.bit_toset(attachRetry);
-                        net1.sendRetryCount++;
-                        #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
-                            netStat.bit_toclear(attachRetry);
-                            net1.sendRetryCount = 0;
-                            net1.messageFailCount++;
-                            net1.sendState=0;
-                        }
-                        break;
-                    }
-                    net1.sendState=3;
-                    netStat.bit_toclear(attachRetry);
-                    netStat.bit_toset(cltIsActive);
-                    break;
                 case 5:
                     if (!sendLength.empty())
                     {