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

main.cpp

Committer:
masterkookus
Date:
2019-10-07
Revision:
14:3d437acb92e3
Parent:
13:4d533751f951
Child:
15:9ad261a27883

File content as of revision 14:3d437acb92e3:

#if !FEATURE_LWIP
    #error [NOT_SUPPORTED] LWIP not supported for this target
#endif

#define netmsgdebug

#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"
#include "BufferedSerial.h"
#include "nettime.h"
#include "netdevices.h"
#include "mydevices.h"
#include "platform/CircularBuffer.h"
#include "netDataTypes.h"
#include "selMsg.h"
#include "string.h"
#include "analogAverager.h"
#include "analogMinMax.h"
#include "counterMinMax.h"
#include "netStatusReg.h"

Ticker nettimer;
Timer netDevTimers[7];

struct netsys net1;
netDevStatus netStat(0,0,0,0,0);

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);
counterMinMax tapMinMax(0,true,-16,true,16);

bool polltick;
bool sendtick;

BufferedSerial sport0(sport0tx, sport0rx, sport0buff, sport0mult);    // UART2

CircularBuffer<char, 256> receiveBuffer;
CircularBuffer<char, 64> sendLength;
CircularBuffer<char, 256> sendBuffer;

Thread conchkthread[2];
Thread rxtxdatathread[5];

unsigned int currenttime=0;

nsapi_error_t ret;

//Provides the deivce poll timing
void heartbeat()
{
    setPollTick(true);
    
    //Provide Sender Task Tick
    if (netDevTimers[0].read_ms()>=100)
    {
        netDevTimers[0].reset();
        setSendTick(true);
    }
    //Server Unsolicited Message Timeout
    if (netStat.bit_isset(srvIsActive))
    {
        if (netDevTimers[1].read_ms()>=setservertimeout)
        {
            //netDevTimers[1].reset();
            netDevTimers[1].stop();
            netStat.bit_toset(srvCloseConnection);
        }
    }
    if (netStat.bit_isset(devOnline))
    {
        #ifdef fastDemandEnabled
        //Fast Demand Poll Tick
        if (netStat.bit_isset(fmdEnabled))
        {
            if (netDevTimers[2].read_ms()>=setfmdemandpollinterval)
            {
                netDevTimers[2].reset();
                netStat.bit_toset(fmdPollReq);
            }
        }
        else
        {
            if (netDevTimers[2].read_ms()>=8000)
            {
                netDevTimers[2].reset();
                netStat.bit_toset(fmdPollReq);
            }
        }
        #endif
        #ifdef fastMeterEnabled
        //Fast Meter Poll Tick
        if (netStat.bit_isset(fmEnabled))
        {
            if (netDevTimers[3].read_ms()>=setfmpollinterval)
            {
                netDevTimers[3].reset();
                netStat.bit_toset(fmPollReq);
            }
        }
        else
        {
            if (netDevTimers[3].read_ms()>=8000)
            {
                netDevTimers[3].reset();
                netStat.bit_toset(fmPollReq);
            }
        }
        #endif
    }
    else
    {
        if (netDevTimers[3].read_ms()>=8000)
        {
            netDevTimers[3].reset();
            netStat.bit_toset(fmCfgReq);
        }
    }
    //Fast Message Timeout
    if ((netStat.bit_isset(fmPollInProg)) || (netStat.bit_isset(fmdPollInProg)))
    {
        if (netDevTimers[4].read_ms()>=2000)
        {
            //netDevTimers[4].reset();
            netDevTimers[4].stop();
            netStat.bit_toset(fmCloseConnection);
            netStat.bit_toset(fmTimeout);
        }
    }
    //Serial Message Timeout
    if (netStat.bit_isset(serPollInProg))
    {
        if (netDevTimers[5].read_ms()>=net1.serMsgTimeout)
        {
            //netDevTimers[5].reset();
            netDevTimers[5].stop();
            netStat.bit_toset(serCloseConnection);
            netStat.bit_toset(serTimeout);
        }
    }
    if (netDevTimers[6].read_ms()>=setDataRecInterval)
    {
        netStat.bit_toset(recDataFile);
        //netDevTimers[6].reset();
        netDevTimers[6].stop();
    }
}

void sendCmd(char cmdNum)
{
    sendLength.push(2);
    for (char txc=0;txc<2;txc++)
    {
        sendBuffer.push(fmCmd[cmdNum][txc]);
    }
}

void sendSerCmd(char cmdNum, char cmdCnt)
{
    sendLength.push(cmdCnt);
    for (char txc=0;txc<cmdCnt;txc++)
    {
        sendBuffer.push(serCmd[cmdNum][txc]);
    }
}

void confignetdevices(EthernetInterface *eth)
{   
    #ifdef sportEnabled
    netStat.bit_toset(serEnabled);
    #endif
    
    #ifdef setPollEnabled
    netStat.bit_toset(pollEnabled);
    #endif
    
    net1.cltPort=setclientport;
    net1.srv_addr=setclientaddress;
    net1.srv_sock.set_blocking(true);
    net1.srv_sock.set_timeout(100);
    net1.srv.set_blocking(true);
    net1.srv.set_timeout(100);
    net1.sendRetryCount=0;
    net1.messageFailCount=0;
    net1.serMsgTimeout=2000;
    
    /* Open the server on ethernet stack */
    net1.srv.open(eth);
    
    /* Bind port 23 to the server */
    net1.srvPort=setserverport;
    net1.srv.bind(net1.srvPort);
    
    /* Can handle 5 simultaneous connections */
    net1.srv.listen(5);
    sport0.baud(sport0baud);
}

void dataprocess(netsys *net2)
{
    int txc;
    char cchar;
    char cbuf[256];
    unsigned int clen=0;
    unsigned int cbc;
    float4byte valpack;
    short2byte timepack;
    
    while(1)
    {
        //Check to see if the Receive Buffer has anything
        while (!receiveBuffer.empty())
        {
            receiveBuffer.pop(cchar);
            cbuf[clen]=cchar;
            clen++;
        }
        //If a Poll Request has been made and data has been received set flag
        netStat.bit_sis(serPollInProg,serRespRx);
        netStat.bit_sis(fmPollInProg,fmRespRx);
        netStat.bit_sis(fmdPollInProg,fmdRespRx);
        
        //Check to see if bulk data and reports being sent from device due to serial command
        if (netStat.bit_isset(serMsgRx))
        {
            if (clen>0)
            {
                sport0.write(cbuf,clen);
                netDevTimers[5].reset();
                clen=0;
            }
        }
        //Check to see if at least 4 characters have been received
        else if (clen>3)
        {
            //Cycle through received data to search for a command
            for (cbc=0;cbc<clen-3;cbc++)
            {
                //Check fast message configuration
                if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC0))
                {
                    sport0.write(cbuf,clen);
                    netStat.bit_toclear(fmCfgInProg);
                    netStat.bit_toset(devOnline);
                    clen=0;
                    break;
                }
                //Check to see if serial commands are currently being received
                if (netStat.bit_isset(serPollInProg))
                {
                    //If relay acknowledges max metering command set bulk receive flag
                    if ((cbuf[cbc]==0x4d) && (cbuf[cbc+1]==0x45) && (cbuf[cbc+2]==0x54))
                    {
                        sport0.write(cbuf,clen);
                        netStat.bit_toset(serMsgRx);
                        netDevTimers[5].reset();
                        netStat.setDevMsgReq(0);
                        clen=0;
                        break;
                    }
                    //If relay acknowledges tap report command set bulk receive flag
                    if ((cbuf[cbc]==0x54) && (cbuf[cbc+1]==0x41) && (cbuf[cbc+2]==0x50))
                    {
                        sport0.write(cbuf,clen);
                        netStat.bit_toset(serMsgRx);
                        netDevTimers[5].reset();
                        netStat.setDevMsgReq(0);
                        clen=0;
                        break;
                    }
                    //If relay acknowledges level 1 login attempt increment command pointer to password
                    if ((cbuf[cbc]==0x41) && (cbuf[cbc+1]==0x43) && (cbuf[cbc+2]==0x43))
                    {
                        sport0.write(cbuf,clen);
                        netStat.setDevMsgPos(1);
                        sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                        netDevTimers[5].reset();
                        clen=0;
                        break;
                    }
                    //If relay sends mask character increment command pointer to send \r\n
                    if (cbuf[cbc]==0x2a)
                    {
                        sport0.write(cbuf,clen);
                        netStat.setDevMsgPos(2);
                        sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                        netDevTimers[5].reset();
                        clen=0;
                        break;
                    }
                    //If relay is in access level 1 or higher increment command pointer to desired command
                    if ((cbuf[cbc]==0x3d) && (cbuf[cbc+1]==0x3e))
                    {
                        sport0.write(cbuf,clen);
                        netStat.setDevMsgPos(4);
                        sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                        netDevTimers[5].reset();
                        clen=0;
                        break;
                    }
                    //If relay in level 0 increment command pointer to access command
                    if ((cbuf[cbc]==0x3d))
                    {
                        sport0.write(cbuf,clen);
                        netStat.setDevMsgPos(0);
                        sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]);
                        netDevTimers[5].reset();
                        clen=0;
                        break;
                    }
                }
                //if (bit_isset(net2->statusReg,fmPollInProg))
                if (netStat.bit_isset(fmPollInProg))
                {
                    //Check fast meter configuration
                    if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC1))
                    {
                        sport0.write(cbuf,clen);
                        //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;
                        }
                        char anum=16;
                        char nxc;
                        //Verify Analog names are correct
                        for (txc=0;txc<vReg1.numAnalog;txc++)
                        {
                            for (nxc=0;nxc<6;nxc++)
                            {
                                if(vReg1.analogs[txc].analogName[nxc]!=cbuf[anum+nxc])
                                {
                                    #ifdef netmsgdebug
                                    printf("\r\nPoint %d Failed\r\n",txc);
                                    #endif
                                    break;
                                }
                            }
                            anum = anum + 11;
                        }
                        //bit_toset(net2->statusReg,fmEnabled);
                        netStat.bit_toset(fmEnabled);
                        clen=0;
                        break;
                    }
                    //Receive data
                    if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xD1))
                    {
                        char anum=cbc+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("%.2f\r\n",vReg1.analogs[txc].analog1Value);
                        }
                        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;
                        tapMinMax.putVal(vReg1.analogs[7].analog1Value);
                        voltageMinMax.putVal(vReg1.analogs[9].analog1Value);
                        voltageAvg.putVal(vReg1.analogs[9].analog1Value);
                        break;
                    }
                }
                if (netStat.bit_isset(fmdPollInProg))
                {
                    //Check fast demand meter configuration
                    if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC2))
                    {
                        sport0.write(cbuf,clen);
                        //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;
                        }
                        char anum=18;
                        char nxc;
                        //Verify Analog names are correct
                        for (txc=0;txc<vReg1.numDemAnalog;txc++)
                        {
                            for (nxc=0;nxc<6;nxc++)
                            {
                                if(vReg1.analogs[txc+vReg1.numAnalog].analogName[nxc]!=cbuf[anum+nxc])
                                {
                                    #ifdef netmsgdebug
                                    printf("\r\nPoint %d Failed\r\n",txc);
                                    #endif
                                    break;
                                }
                            }
                            anum = anum + 11;
                        }
                        netStat.bit_toset(fmdEnabled);
                        clen=0;
                        break;
                    }
                    //Receive demand data
                    if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xD2))
                    {
                        char anum=cbc+4;
                        for (txc=0;txc<vReg1.numDemAnalog;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("%.2f\r\n",vReg1.analogs[txc].analog1Value);
                        }
                        vReg1.demTimeStamp.month=cbuf[anum];
                        vReg1.demTimeStamp.day=cbuf[anum+1];
                        vReg1.demTimeStamp.year=cbuf[anum+2];
                        vReg1.demTimeStamp.hour=cbuf[anum+3];
                        vReg1.demTimeStamp.min=cbuf[anum+4];
                        vReg1.demTimeStamp.sec=cbuf[anum+5];
                        timepack.bytes[1]=cbuf[anum+6];
                        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();*/
                        break;
                    }
                }
            }
            if (clen>0)
            {
                #ifdef netmsgdebug
                printf("Command not Received\r\n");
                #endif
                clen=0;
            }
        }
    }
}

//Ethernet to Ethernet Send Data
void datantx(netsys *net2)
{
    while(1)
    {
        if (netStat.bit_isset(cltIsActive))
        {
            //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
                    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
                    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);
                    #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;
                    }
                }
            }
            //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
                if (ret>=0)
                {
                    #ifdef netmsgdebug
                    printf("Send Result %d\r\n",ret);
                    #endif
                    netStat.bit_toclear(sendRetry);
                    //net2->txMessageCount++;
                    netStat.incCount(1);
                    net2->sendState=5;
                }
                //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);
                    #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;
                    }
                }
            }
        }
    }
}

//Ethernet receive data and send to aux devices (serial, etc...)
void datancrx(netsys *net2)
{
    char rxbuf[256];
    int rxlen=0;
    int rxc;
    while (1)
    {
        //If the client is active check to see if data has been received
        while (netStat.bit_isset(cltIsActive))
        {
            //Store the length of the received data
            rxlen=net2->srv_sock.recv(rxbuf, sizeof(rxbuf));
            //if there is data then push data into received buffer
            if (rxlen>0)
            {
                for (rxc = 0;rxc<rxlen;rxc++)
                {
                    receiveBuffer.push(rxbuf[rxc]);
                }
                #ifdef netmsgdebug
                printf("Client Received Data\r\n");
                #endif
                //Increment received message counter
                netStat.incCount(2);
            }
        }
    }
}

//Ethernet receive data and send to aux devices (serial, etc...)
void datansrx(netsys *net2)
{
    char rxbuf[256];
    int rxlen=0;
    int rxc;
    while (1)
    {
        //If the server is active check to see if data has been received
        while (netStat.bit_isset(srvIsActive))
        {
            //Store the length of the received data
            rxlen=net2->clt_sock.recv(rxbuf, sizeof(rxbuf));
            //if there is data then push data into received buffer
            if (rxlen>0)
            {
                for (rxc = 0;rxc<rxlen;rxc++)
                {
                    receiveBuffer.push(rxbuf[rxc]);
                }
                #ifdef netmsgdebug
                printf("Server Received Data\r\n");
                #endif
                //Increment received message counter
                netStat.incCount(2);
            }
        }
    }
}

//Serial device to server
void datasrx(netsys *net2)
{
    char rxbuf[256];
    int rxlen=0;
    int rxindex=0;
    int rxc;
    bool crRx = false;
    int4byte cmdpack;
    
    while (1)
    {
        //Check to see if there is any data received by serial port and store length
        rxlen=sport0.readable();
        //If data is ready push into receive buffer
        if (rxlen>0)
        {
            for (rxc=0;rxc<rxlen;rxc++)
            {
                rxbuf[rxindex+rxc]=sport0.getc();
                //Check to see if a carriage return has been detected
                if (rxbuf[rxindex+rxc]=='\r')
                {
                    crRx=true;
                }
            }
            //Store the current cursor location in the buffer
            rxindex=rxindex+rxlen;
            //Set the received length to zero
            rxlen=0;
            //If there is data in the buffer and a carriage return has been detected search for a command
            if ((rxindex>2) && (crRx==true))
            {
                //Set the carriage return flag false
                crRx=false;
                for (rxc=0;rxc<=rxindex-3;rxc++)
                {
                    cmdpack.bytes[3]=rxbuf[rxc];
                    cmdpack.bytes[2]=rxbuf[rxc+1];
                    cmdpack.bytes[1]=rxbuf[rxc+2];
                    cmdpack.bytes[0]=rxbuf[rxc+3];
                    //Search for the Min/Max command
                    if (cmdpack.cmdint==0x4d45544d)
                    {
                        #ifdef netmsgdebug
                        printf("MIN MAX METERING\r\n");
                        #endif
                        netStat.setDevMsgReq(0);
                        netStat.setDevMsgPos(2);
                        netStat.bit_toset(serPollReq);
                        rxindex=0;
                    }
                    //Search for the Tap Report Command
                    if (cmdpack.cmdint==0x54415052)
                    {
                        #ifdef netmsgdebug
                        printf("TAP REPORT\r\n");
                        #endif
                        netStat.setDevMsgReq(1);
                        netStat.setDevMsgPos(2);
                        netStat.bit_toset(serPollReq);
                        rxindex=0;
                    }
                    //Search for the Tap Report Command
                    if (cmdpack.cmdint==0x53544154)
                    {
                        #ifdef netmsgdebug
                        printf("STATS\r\n");
                        #endif
                        sport0.printf("Status %08x\r\n",netStat.getStatus());
                        rxindex=0;
                    }
                }
                //If no command found set cursor location to zero
                if (rxindex>0)
                {
                    #ifdef netmsgdebug
                    printf("Serial Command Not Found");
                    #endif
                    rxindex=0;
                }
            }
        }
    }
}

//Checks for a Ethernet to Serial connection
void conchk(netsys *net2)
{
    TCPServer *server=&(net2->srv);
    TCPSocket *client_socket=&(net2->clt_sock);
    SocketAddress *client_address=&(net2->clt_addr);
    
    while(1)
    {
        //Wait for someone to connect
        while (server->accept(client_socket, client_address) < 0)
        {
            //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
        netStat.bit_toset(srvIsActive);
        netDevTimers[1].reset();
        netDevTimers[1].start();
    }
}

int main()
{
    EthernetInterface eth;
    eth.set_network(setseveraddress,setsevermask,setsevergateway); //Use  these parameters for static IP
    eth.connect();
    
    initVoltageRegulator(&vReg1);
    
    #ifdef netmsgdebug
    printf("The target IP address is '%s'\r\n", eth.get_ip_address());
    #endif
    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(datansrx,&net1));
    rxtxdatathread[1].start(callback(datancrx,&net1));
    /* Setup Ethernet to Serial receive data Thread */
    rxtxdatathread[2].start(callback(datasrx,&net1));
    rxtxdatathread[3].start(callback(datantx,&net1));
    rxtxdatathread[4].start(callback(dataprocess,&net1));

    unsigned int sxc;
    
    netDevTimers[0].start();
    netDevTimers[2].start();
    netDevTimers[3].start();
    netDevTimers[6].start();
    
    while (true) {
        
        polltick=getPollTick();
        
        if (polltick)
        {
            setPollTick(false);
            
            if (netStat.bit_isset(srvCloseConnection))
            {
                #ifdef netmsgdebug
                printf("Closed\r\n");
                #endif
                net1.clt_sock.close();
                netStat.bit_toclear(srvIsActive);
                netStat.bit_toclear(srvCloseConnection);
                
            }
            if (netStat.bit_isset(serCloseConnection))
            {
                #ifdef netmsgdebug
                printf("Serial Connection Closed\r\n");
                #endif
                if (netStat.bit_isset(serRespRx))
                {
                    netStat.incCount(2);
                }
                else
                {
                    netStat.incCount(4);
                }
                netStat.bit_toclear(serclose);
                netStat.bit_toset(cltCloseConnection);
            }
            if (netStat.bit_isset(fmCloseConnection))
            {
                #ifdef netmsgdebug
                printf("Fast Meter Connection Closed\r\n");
                #endif
                if ((netStat.bit_isset(fmRespRx)) || (netStat.bit_isset(fmdRespRx)))
                {
                    netStat.incCount(2);
                }
                else
                {
                    netStat.incCount(3);
                }
                netStat.bit_toclear(fmclose);
                netStat.bit_toset(cltCloseConnection);
            }
            if (netStat.bit_isset(cltCloseConnection))
            {
                sport0.printf("Status %08x\r\n",netStat.getStatus());
                #ifdef netmsgdebug
                printf("Connection Closed\r\n");
                #endif
                net1.srv_sock.close();
                net1.sendState=0;
                netStat.bit_toclear(cltclose);
            }
            
            if ((netStat.bit_isclear(serPollInProg)) && (netStat.bit_isclear(fmPollInProg)) && (netStat.bit_isclear(fmdPollInProg)) && (netStat.bit_isset(devOnline)))
            {
                if (netStat.bit_isset(recDataFile))
                {
                    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.bit_toclear(recDataFile);
                    netDevTimers[6].reset();
                    netDevTimers[6].start();
                }
                if (netStat.bit_isset(fmdPollReq))
                {
                    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();
                }
                else if (netStat.bit_isset(fmPollReq))
                {
                    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();
                }
                else if (netStat.bit_isset(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);
                    }
                }
            }
            if ((netStat.bit_isclear(devOnline)) &&  (netStat.bit_isclear(fmCfgInProg)) && (netStat.bit_isset(fmCfgReq)))
            {
                sendCmd(0);
                netStat.bit_toclear(fmCfgReq);
                netStat.bit_toset(fmCfgInProg);
                net1.sendState=1;
            }
        }
        
        
        //Begin Sending Data
        sendtick=getSendTick();
        
        if (sendtick)
        {
            setSendTick(false);
            //sport0.printf("Status %08x\r\n",netStat.getStatus());
            switch (net1.sendState)
            {
                case 0:
                    if (!sendLength.empty())
                    {
                        sport0.printf("Status %08x\r\n",netStat.getStatus());
                        sendLength.pop(net1.sendLen);
                        for (sxc=0;sxc<net1.sendLen;sxc++)
                        {
                            sendBuffer.pop(net1.sendString[sxc]);
                        }
                        net1.sendState=1;
                    }
                    break;
                case 1:
                    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
                        {
                            netStat.bit_toset(attachRetry);
                            net1.sendRetryCount=0;
                            net1.sendState=2;
                            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())
                    {
                        sport0.printf("Status %08x\r\n",netStat.getStatus());
                        sendLength.pop(net1.sendLen);
                        for (sxc=0;sxc<net1.sendLen;sxc++)
                        {
                            sendBuffer.pop(net1.sendString[sxc]);
                        }
                        net1.sendState=4;
                    }
                    break;
                default:
                    break;
            }
        }
    }
}