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 26 18:40:53 2019 +0000
Parent:
7:be13a9037d41
Child:
9:d6e7981dfc89
Commit message:
Command Response and Data Storage;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mydevices.h Show annotated file Show diff for this revision Revisions of this file
netDataTypes.h 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
selMsg.cpp Show annotated file Show diff for this revision Revisions of this file
selMsg.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Sep 23 12:29:52 2019 +0000
+++ b/main.cpp	Thu Sep 26 18:40:53 2019 +0000
@@ -11,10 +11,14 @@
 #include "netdevices.h"
 #include "mydevices.h"
 #include "platform/CircularBuffer.h"
+#include "netDataTypes.h"
+#include "selMsg.h"
+#include "string.h"
 
 Ticker nettimer;
 
 struct netsys net1;
+struct vRegData vReg1;
 
 bool polltick;
 
@@ -44,16 +48,24 @@
     net1.srv_addr=setclientaddress;
     net1.cltIsActive=false;
     net1.cltCloseConnection=false;
-    net1.pollTimeout=setpolltimeout;
     net1.pollTime=0;
+    net1.pollTimeout=0;
     net1.pollInterval=setpollinterval;
     net1.pollRequestSent=false;
     net1.pollResponseReceived=false;
-    net1.srv_sock.set_blocking(false);
+    net1.srv_sock.set_blocking(true);
+    net1.srv_sock.set_timeout(100);
     net1.aliveTimeout=setservertimeout;
-    net1.srv.set_blocking(false);
+    net1.srv.set_blocking(true);
+    net1.srv.set_timeout(100);
     net1.pollEnabled=setpollenabled;
     net1.pollTimeoutCount=0;
+    net1.pollState=0;
+    net1.pollTimeoutCount=0;
+    net1.sendRetryCount=0;
+    net1.messageFailCount=0;
+    net1.txMessageCount=0;
+    net1.rxMessageCount=0;
     
     /* Open the server on ethernet stack */
     net1.srv.open(eth);
@@ -71,27 +83,22 @@
 
 void dataprocess(netsys *net2)
 {
-    char* tstcmd = "Tock\r\n";
-    char tstlen;
     int txc;
     char cchar;
     char cbuf[256];
     unsigned int clen=0;
     unsigned int cbc;
-    union packer4byte
-    {
-        unsigned int cmdint;
-        char bytes[4];
-    };
-    packer4byte cmdpack;
+    int4byte cmdpack;
+    float4byte valpack;
+    short2byte timepack;
     
     while(1)
     {
         while (!receiveBuffer.empty())
         {
             receiveBuffer.pop(cchar);
+            cbuf[clen]=cchar;
             clen++;
-            cbuf[clen]=cchar;
         }
         if (net2->pollRequestSent)
         {
@@ -104,26 +111,114 @@
         {
             for (cbc=0;cbc<clen-3;cbc++)
             {
-                printf("Length: %d\r\n",clen);
-                printf("%c%c%c%c\r\n",cbuf[cbc],cbuf[cbc+1],cbuf[cbc+2],cbuf[cbc+3]);
-                cmdpack.bytes[3]=cbuf[cbc];
-                cmdpack.bytes[2]=cbuf[cbc+1];
-                cmdpack.bytes[1]=cbuf[cbc+2];
-                cmdpack.bytes[0]=cbuf[cbc+3];
-                if (cmdpack.cmdint==0x5469636b)
+                //printf("Length: %d\r\n",clen);
+                //printf("%c%c%c%c\r\n",cbuf[cbc],cbuf[cbc+1],cbuf[cbc+2],cbuf[cbc+3]);
+                if (net2->sendState==3)
+                {
+                    cmdpack.bytes[3]=cbuf[cbc];
+                    cmdpack.bytes[2]=cbuf[cbc+1];
+                    cmdpack.bytes[1]=cbuf[cbc+2];
+                    cmdpack.bytes[0]=0x00;
+                    //printf("%08x\r\n",cmdpack.cmdint);
+                    if (cmdpack.cmdint&0xF0000000)
+                    {
+                        printf("Telnet Command Received\r\n");
+                        cbc=cbc+3;
+                        if (cbc>=clen)
+                        {
+                            clen=0;
+                        }
+                    }
+                }
+                else
                 {
-                    tstlen=strlen(tstcmd);
-                    sendLength.push(tstlen);
-                    for (txc=0;txc<6;txc++)
+                    //printf("%c%c%c%c\r\n",cbuf[cbc],cbuf[cbc+1],cbuf[cbc+2],cbuf[cbc+3]);
+                    cmdpack.bytes[3]=cbuf[cbc];
+                    cmdpack.bytes[2]=cbuf[cbc+1];
+                    cmdpack.bytes[1]=0x00;
+                    cmdpack.bytes[0]=0x00;
+                    if (cmdpack.cmdint==0xA5C00000)
                     {
-                        sendBuffer.push(tstcmd[txc]);
+                        sport0.write(cbuf,clen);
+                        /*sendLength.push(tstlen);
+                        for (txc=0;txc<2;txc++)
+                        {
+                            sendBuffer.push(tstcmd[txc]);
+                        }*/
+                        net2->pollState=1;
+                        clen=0;
+                        break;
                     }
-                    if (net2->sendState==0)
+                    if (cmdpack.cmdint==0xA5C10000)
                     {
-                        net2->sendState=1;
+                        sport0.write(cbuf,clen);
+                        if (vReg1.numAnalog!=cbuf[6])
+                        {
+                            printf("\r\nAnalog Channel Count Off\r\n");
+                            net2->pollState=0;
+                            break;
+                        }
+                        if (vReg1.numDigital!=cbuf[8])
+                        {
+                            printf("\r\nDigital Channel Count Off\r\n");
+                            net2->pollState=0;
+                            break;
+                        }
+                        char anum=16;
+                        char nxc;
+                        for (txc=0;txc<vReg1.numAnalog;txc++)
+                        {
+                            for (nxc=0;nxc<6;nxc++)
+                            {
+                                //printf("%c %c",vReg1.analogs[txc].analogName[nxc],cbuf[anum+nxc]);
+                                if(vReg1.analogs[txc].analogName[nxc]!=cbuf[anum+nxc])
+                                {
+                                    printf("\r\nPoint %d Failed\r\n",txc);
+                                    break;
+                                }
+                            }
+                            anum = anum + 11;
+                            //printf("\r\n");
+                        }
+                        net2->pollState=2;
+                        clen=0;
+                        break;
                     }
-                    clen=0;
-                    break;
+                    if (cmdpack.cmdint==0xA5D10000)
+                    {
+                        char anum=4;
+                        for (txc=0;txc<vReg1.numAnalog;txc++)
+                        {
+                            valpack.bytes[3]=cbuf[anum];
+                            valpack.bytes[2]=cbuf[anum+1];
+                            valpack.bytes[1]=cbuf[anum+2];
+                            valpack.bytes[0]=cbuf[anum+3];
+                            vReg1.analogs[txc].analog1Value = valpack.cmdflt;
+                            anum = anum + 4;
+                            //printf("%02x, %02x %02x %02x\r\n",cbuf[anum],cbuf[anum+1],cbuf[anum+2],cbuf[anum+3]);
+                            //printf("%02x, %02x %02x %02x\r\n",valpack.bytes[3],valpack.bytes[2],valpack.bytes[1],valpack.bytes[0]);
+                            //printf("%f\r\n",valpack.cmdflt);
+                            printf("%.2f\r\n",vReg1.analogs[txc].analog1Value);
+                        }
+                        printf("%d\r\n",anum);
+                        vReg1.timeStamp.month=cbuf[anum];
+                        vReg1.timeStamp.day=cbuf[anum+1];
+                        vReg1.timeStamp.year=cbuf[anum+2];
+                        vReg1.timeStamp.hour=cbuf[anum+3];
+                        vReg1.timeStamp.min=cbuf[anum+4];
+                        vReg1.timeStamp.sec=cbuf[anum+5];
+                        timepack.bytes[1]=cbuf[anum+6];
+                        timepack.bytes[0]=cbuf[anum+7];
+                        vReg1.timeStamp.msec=timepack.cmdshort;
+                        printf("%d/%d/%d %d:%d:%d.%d\r\n",vReg1.timeStamp.month,vReg1.timeStamp.day,vReg1.timeStamp.year,vReg1.timeStamp.hour,vReg1.timeStamp.min,vReg1.timeStamp.sec,vReg1.timeStamp.msec);
+                        anum=anum+8;
+                        for (txc=0;txc<vReg1.numDigital;txc++)
+                        {
+                            vReg1.digitalTargets[txc]=cbuf[anum+txc];
+                        }
+                        clen=0;
+                        break;
+                    }
                 }
             }
             if (clen>0)
@@ -153,7 +248,13 @@
                 {
                     net2->connectRetry = false;
                     printf("Connected %d\r\n",ret);
-                    net2->cltCloseConnection=true;
+                    //net2->cltCloseConnection=true;
+                    net2->sendState=4;
+                }
+                else if (ret==-3015)
+                {
+                    printf("May already be connected, attempting send.\r\n");
+                    net2->connectRetry = false;
                     net2->sendState=4;
                 }
                 else
@@ -185,8 +286,8 @@
                     printf("Send Result %d\r\n",ret);
                     net2->sendRetry = false;
                     net2->txMessageCount++;
-                    net2->cltCloseConnection=true;
-                    net2->sendState=0;
+                    //net2->cltCloseConnection=true;
+                    net2->sendState=5;
                     net2->sendTime=0;
                 }
                 else
@@ -206,17 +307,37 @@
                     }
                 }
             }
-            if (net2->cltCloseConnection==true)
+        }
+    }
+}
+
+//Ethernet receive data and send to aux devices (serial, etc...)
+void datancrx(netsys *net2)
+{
+    char rxbuf[256];
+    int rxlen=0;
+    int rxc;
+    while (1)
+    {
+        while (net2->cltIsActive)
+        {
+            rxlen=net2->srv_sock.recv(rxbuf, sizeof(rxbuf));
+            if (rxlen>0)
             {
-                net2->srv_sock.close();
-                net2->cltIsActive=false;
+                net2->aliveTime=0;
+                for (rxc = 0;rxc<rxlen;rxc++)
+                {
+                    receiveBuffer.push(rxbuf[rxc]);
+                }
+                printf("Client Received Data\r\n");
+                net2->rxMessageCount++;
             }
         }
     }
 }
 
 //Ethernet receive data and send to aux devices (serial, etc...)
-void datanrx(netsys *net2)
+void datansrx(netsys *net2)
 {
     char rxbuf[256];
     int rxlen=0;
@@ -229,18 +350,18 @@
             if (rxlen>0)
             {
                 net2->aliveTime=0;
-                sport0.write(rxbuf,rxlen);
                 for (rxc = 0;rxc<rxlen;rxc++)
                 {
                     receiveBuffer.push(rxbuf[rxc]);
                 }
+                printf("Server Received Data\r\n");
                 net2->rxMessageCount++;
             }
         }
     }
 }
 
-//Serial device to server connected client
+//Serial device to server
 void datasrx(netsys *net2)
 {
     char rxbuf[256];
@@ -291,21 +412,23 @@
     eth.set_network(setseveraddress,setsevermask,setsevergateway); //Use  these parameters for static IP
     eth.connect();
     
+    initVoltageRegulator(&vReg1);
+    
     printf("The target IP address is '%s'\r\n", eth.get_ip_address());
+    printf("%s\r\n",vReg1.analogs[0].analogName);
     confignetdevices(&eth);
     /* Setup Ethernet to Serial Connection Thread */
     conchkthread[0].start(callback(conchk,&net1));
     /* Setup polltick Ticker */
     nettimer.attach_us(heartbeat,10000);
     /* Setup Ethernet to Serial transmit data Thread */
-    rxtxdatathread[0].start(callback(datanrx,&net1));
+    rxtxdatathread[0].start(callback(datansrx,&net1));
+    rxtxdatathread[1].start(callback(datancrx,&net1));
     /* Setup Ethernet to Serial receive data Thread */
-    rxtxdatathread[1].start(callback(datasrx,&net1));
-    rxtxdatathread[2].start(callback(datantx,&net1));
-    rxtxdatathread[3].start(callback(dataprocess,&net1));
+    rxtxdatathread[2].start(callback(datasrx,&net1));
+    rxtxdatathread[3].start(callback(datantx,&net1));
+    rxtxdatathread[4].start(callback(dataprocess,&net1));
     
-    char* tstcmd = "Tick\r\n";
-    char tstlen;
     unsigned int txc;
     unsigned int sxc;
     
@@ -336,11 +459,10 @@
             {
                 if (net1.pollTime >= net1.pollInterval)
                 {
-                    tstlen=strlen(tstcmd);
-                    sendLength.push(tstlen);
-                    for (txc=0;txc<tstlen;txc++)
+                    sendLength.push(2);
+                    for (txc=0;txc<2;txc++)
                     {
-                        sendBuffer.push(tstcmd[txc]);
+                        sendBuffer.push(fmCmd[net1.pollState][txc]);
                     }
                     if (net1.sendState==0)
                     {
@@ -349,17 +471,33 @@
                     }
                     net1.pollTime=0;
                 }
-                if (net1.pollTime >= net1.pollTimeout)
+                if (net1.sendState==5)
                 {
-                    if (net1.pollRequestSent)
+                    net1.pollTimeout++;
+                    if (net1.pollTimeout==setpolltimeout)
                     {
-                        if (net1.pollResponseReceived==false)
+                        if (net1.pollRequestSent)
                         {
-                            net1.pollTimeoutCount++;
+                            printf("Poll Request Sent\r\n");
+                            if (net1.pollResponseReceived==false)
+                            {
+                                printf("Poll Response Not Received\r\n");
+                            }
+                            else
+                            {
+                                printf("Poll Response Received\r\n");
+                            }
                             net1.pollRequestSent=false;
+                            net1.pollResponseReceived=false;
+                            net1.cltCloseConnection=true;
+                            net1.sendState=0;
                         }
                     }
                 }
+                else
+                {
+                    net1.pollTimeout=0;
+                }
             }
             if (net1.sendTime == 10)
             {
@@ -382,11 +520,18 @@
                         printf("Socket%d\r\n",ret);
                         if (ret < 0)
                         {
-                            net1.attachRetry = true;
-                            net1.sendRetryCount=0;
-                            net1.sendState=2;
-                            net1.sendTime=0;
-                            break;
+                            if (ret==-3003)
+                            {
+                                printf("May already be attached, attempting connect.\r\n");
+                            }
+                            else
+                            {
+                                net1.attachRetry = true;
+                                net1.sendRetryCount=0;
+                                net1.sendState=2;
+                                net1.sendTime=0;
+                                break;
+                            }
                         }
                         net1.sendState=3;
                         net1.sendTime=11;
@@ -421,11 +566,34 @@
                         net1.sendTime=0;
                         if (!sendLength.empty())
                         {
-                            net1.sendState=1;
+                            if (net1.sendState==5)
+                            {
+                                if (!sendLength.empty())
+                                {
+                                    sendLength.pop(net1.sendLen);
+                                    for (sxc=0;sxc<net1.sendLen;sxc++)
+                                    {
+                                        sendBuffer.pop(net1.sendString[sxc]);
+                                    }
+                                }
+                                net1.sendState=4;
+                            }
+                            else
+                            {
+                                net1.sendState=1;
+                            }
                         }
                         break;
                 }
             }
         }
+        if (net1.cltCloseConnection==true)
+        {
+            printf("Client Socket Closed\r\n");
+            net1.srv_sock.close();
+            net1.cltIsActive=false;
+            net1.cltCloseConnection=false;
+            net1.sendState=0;
+        }
     }
 }
--- a/mydevices.h	Mon Sep 23 12:29:52 2019 +0000
+++ b/mydevices.h	Thu Sep 26 18:40:53 2019 +0000
@@ -1,15 +1,17 @@
 /*Client Information*/
-#define setclientport 23
+#define setclientport 23000
 #define setclientaddress "10.150.1.242"
 #define setpolltimeout 200
 #define setpollinterval 500
+#define setslowpollinterval 1500
+#define setfastpollcount 5
 #define setpollenabled true
 
 #define setserverport 23
 #define setseveraddress  "10.150.1.241"
 #define setsevermask  "255.255.255.0"
 #define setsevergateway  "10.150.1.245"
-#define setservertimeout 50
+#define setservertimeout 100
 
 /*Serial Port Information*/
 #define sport0rx PD_6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netDataTypes.h	Thu Sep 26 18:40:53 2019 +0000
@@ -0,0 +1,21 @@
+#ifndef NETDATADTYPES_H 
+#define NETDATATYPES_H 
+
+union int4byte
+    {
+        unsigned int cmdint;
+        char bytes[4];
+    };
+
+union float4byte
+    {
+        float cmdflt;
+        char bytes[4];
+    };
+
+union short2byte
+    {
+        unsigned short cmdshort;
+        char bytes[2];
+    };
+#endif
\ No newline at end of file
--- a/netdevices.h	Mon Sep 23 12:29:52 2019 +0000
+++ b/netdevices.h	Thu Sep 26 18:40:53 2019 +0000
@@ -33,6 +33,7 @@
     unsigned int sendTime;
     unsigned int sendInterval;
     unsigned int sendState;
+    unsigned int pollState;
     
     unsigned int deivceType;
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/selMsg.cpp	Thu Sep 26 18:40:53 2019 +0000
@@ -0,0 +1,24 @@
+#include "selMsg.h"
+#include "string.h"
+void initVoltageRegulator(vRegData *vReg)
+{
+    vReg->numAnalog=0x11;
+    vReg->numDigital=0x34;
+    strcpy(vReg->analogs[0].analogName,"IL\0\0\0\0");
+    strcpy(vReg->analogs[1].analogName,"ILA\0\0\0");
+    strcpy(vReg->analogs[2].analogName,"VS\0\0\0\0");
+    strcpy(vReg->analogs[3].analogName,"VSA\0\0\0");
+    strcpy(vReg->analogs[4].analogName,"VL\0\0\0\0");
+    strcpy(vReg->analogs[5].analogName,"VLA\0\0\0");
+    strcpy(vReg->analogs[6].analogName,"VCMP\0\0");
+    strcpy(vReg->analogs[7].analogName,"TAPPOS");
+    strcpy(vReg->analogs[8].analogName,"VSSEC\0");
+    strcpy(vReg->analogs[9].analogName,"VLSEC\0");
+    strcpy(vReg->analogs[10].analogName,"VCMPSC");
+    strcpy(vReg->analogs[11].analogName,"PL\0\0\0\0");
+    strcpy(vReg->analogs[12].analogName,"QL\0\0\0\0");
+    strcpy(vReg->analogs[13].analogName,"SL\0\0\0\0");
+    strcpy(vReg->analogs[14].analogName,"PF\0\0\0\0");
+    strcpy(vReg->analogs[15].analogName,"PFLD\0\0");
+    strcpy(vReg->analogs[16].analogName,"FREQ\0\0");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/selMsg.h	Thu Sep 26 18:40:53 2019 +0000
@@ -0,0 +1,71 @@
+#ifndef SELMSG_H 
+#define SELMSG_H 
+
+const char fmCmd[3][2] = {
+    {0xA5,0xC0},
+    {0xA5,0xC1},
+    {0xA5,0xD1}
+};
+
+const char vRegDevCfg[23] = {
+    0xA5,0xC0,0x17,0x02,0x03,0x00,0xA5,0xC1,0xA5,0xD1,0xA5,0xC2,0xA5,0xD2,0xA5,0xC3,0xA5,0xD3,0x01,0x00,0x00,0x05,0x21
+};
+
+/*const char vRegMsgCfg204] = {
+    0xA5,0xC1,0xBB,0x01,0x00,0x00,0x11,0x01,0x34,0x00,0x00,0x04,0x00,0x48,0x00,0x50,0x49,0x4C,0x00,0x00,0x00,0x00,0x01,0xFF,
+    0xFF,0x00,0x00,0x49,0x4C,0x41,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x56,0x53,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,
+    0x00,0x56,0x53,0x41,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x56,0x4C,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x56,
+    0x4C,0x41,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x56,0x43,0x4D,0x50,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x54,0x41,0x50,
+    0x50,0x4F,0x53,0x01,0xFF,0xFF,0x00,0x00,0x56,0x53,0x53,0x45,0x43,0x00,0x01,0xFF,0xFF,0x00,0x00,0x56,0x4C,0x53,0x45,0x43,
+    0x00,0x01,0xFF,0xFF,0x00,0x00,0x56,0x43,0x4D,0x50,0x53,0x43,0x01,0xFF,0xFF,0x00,0x00,0x50,0x4C,0x00,0x00,0x00,0x00,0x01,
+    0xFF,0xFF,0x00,0x00,0x51,0x4C,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x53,0x4C,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,
+    0x00,0x00,0x50,0x46,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x50,0x46,0x4C,0x44,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,
+    0x46,0x52,0x45,0x51,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x23
+    };*/
+
+//short2byte analogScaleOffset;
+
+struct analog1Sample {
+    char analogName[6];
+    float analog1Value;
+};
+struct analog2Sample {
+    char analogName[6];
+    float analog1Value;
+    float analog2Value;
+};
+struct analog3Sample {
+    char analogName[6];
+    float analog1Value;
+    float analog2Value;
+    float analog3Value;
+};
+struct analog4Sample {
+    char analogName[6];
+    float analog1Value;
+    float analog2Value;
+    float analog3Value;
+    float analog4Value;
+};
+
+struct fmTimeStamp {
+    char month;
+    char day;
+    char year;
+    char hour;
+    char min;
+    char sec;
+    short msec;
+};
+
+struct vRegData {
+    char numAnalog;
+    char numDigital;
+    struct analog1Sample analogs[17];
+    struct fmTimeStamp timeStamp;
+    char digitalTargets[52];
+};
+
+void initVoltageRegulator(vRegData *vReg);
+
+#endif
\ No newline at end of file