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
Diff: main.cpp
- Revision:
- 7:be13a9037d41
- Parent:
- 6:9f97716eae76
- Child:
- 8:fa2a2c3a16ce
--- a/main.cpp Thu Sep 19 18:14:31 2019 +0000 +++ b/main.cpp Mon Sep 23 12:29:52 2019 +0000 @@ -9,21 +9,20 @@ #include "BufferedSerial.h" #include "nettime.h" #include "netdevices.h" +#include "mydevices.h" #include "platform/CircularBuffer.h" -static const char* mbedIp = "10.150.1.242"; //IP -static const char* mbedMask = "255.255.255.0"; // Mask -static const char* mbedGateway = "10.150.1.245"; //Gateway - -Ticker nettimer[2]; +Ticker nettimer; struct netsys net1; bool polltick; -BufferedSerial sport0(PD_5, PD_6, 256, 4); // UART2 +BufferedSerial sport0(sport0tx, sport0rx, sport0buff, sport0mult); // UART2 -CircularBuffer<char, 256> cmdbuf; +CircularBuffer<char, 1024> receiveBuffer; +CircularBuffer<char, 64> sendLength; +CircularBuffer<char, 1024> sendBuffer; Thread conchkthread[2]; Thread rxtxdatathread[5]; @@ -41,16 +40,20 @@ void confignetdevices(EthernetInterface *eth) { - net1.cltPort=23; - net1.srv_addr="10.150.1.241"; + net1.cltPort=setclientport; + net1.srv_addr=setclientaddress; net1.cltIsActive=false; net1.cltCloseConnection=false; - net1.pollTimeout=200; + net1.pollTimeout=setpolltimeout; net1.pollTime=0; - net1.pollInterval=200; + net1.pollInterval=setpollinterval; + net1.pollRequestSent=false; + net1.pollResponseReceived=false; net1.srv_sock.set_blocking(false); - net1.aliveTimeout=50; + net1.aliveTimeout=setservertimeout; net1.srv.set_blocking(false); + net1.pollEnabled=setpollenabled; + net1.pollTimeoutCount=0; /* Open the server on ethernet stack */ net1.srv.open(eth); @@ -58,17 +61,19 @@ net1.srvCloseConnection=false; /* Bind port 23 to the server */ - net1.srvPort=23; + net1.srvPort=setserverport; net1.srv.bind(net1.srvPort); /* Can handle 5 simultaneous connections */ net1.srv.listen(5); - sport0.baud(9600); + sport0.baud(sport0baud); } -//Ethernet to Ethernet Send Command -void datapolltx(netsys *net2) +void dataprocess(netsys *net2) { + char* tstcmd = "Tock\r\n"; + char tstlen; + int txc; char cchar; char cbuf[256]; unsigned int clen=0; @@ -82,35 +87,62 @@ while(1) { - while (!cmdbuf.empty()) + while (!receiveBuffer.empty()) { - cmdbuf.pop(cchar); + receiveBuffer.pop(cchar); clen++; cbuf[clen]=cchar; } + if (net2->pollRequestSent) + { + if (clen>0) + { + net2->pollResponseReceived=true; + } + } if (clen>3) { - printf("%d\r\n",clen); - //clen=0; - for (cbc=0;cbc<clen-5;cbc++) + 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("Tock"); - net2->pollTime=net2->pollInterval; - net2->pollState=1; + tstlen=strlen(tstcmd); + sendLength.push(tstlen); + for (txc=0;txc<6;txc++) + { + sendBuffer.push(tstcmd[txc]); + } + if (net2->sendState==0) + { + net2->sendState=1; + } clen=0; break; } } - } + if (clen>0) + { + printf("Command not Received\r\n"); + clen=0; + } + } + } +} + +//Ethernet to Ethernet Send Data +void datantx(netsys *net2) +{ + while(1) + { if (net2->cltIsActive) { - while (net2->pollState==3) + while (net2->sendState==3) { if (net2->connectRetry == true) { @@ -122,52 +154,55 @@ net2->connectRetry = false; printf("Connected %d\r\n",ret); net2->cltCloseConnection=true; - net2->pollState=4; + net2->sendState=4; } else { net2->connectRetry = true; - net2->pollRetryCount++; + net2->sendRetryCount++; printf("Connect Attempt Failed, Code: %d\r\n",ret); - if (net2->pollRetryCount>3) + if (net2->sendRetryCount>3) { printf("Communication Failed, Closing\r\n"); net2->connectRetry = false; - net2->pollRetryCount = 0; + net2->sendRetryCount = 0; net2->messageFailCount++; net2->cltCloseConnection=true; - net2->pollState=0; + net2->sendState=0; + net2->sendTime=0; } } } - while (net2->pollState==4) + while (net2->sendState==4) { if (net2->sendRetry == true) { wait_ms(100); } - ret = net2->srv_sock.send("Tock\r\n",strlen("Tock\r\n")); + ret = net2->srv_sock.send(net2->sendString,net2->sendLen); if (ret>=0) { printf("Send Result %d\r\n",ret); net2->sendRetry = false; net2->txMessageCount++; net2->cltCloseConnection=true; - net2->pollState=0; + net2->sendState=0; + net2->sendTime=0; } else { net2->sendRetry = true; - net2->pollRetryCount++; + net2->sendRetryCount++; printf("Send Attempt Failed, Code: %d\r\n",ret); - if (net2->pollRetryCount>3) + if (net2->sendRetryCount>3) { printf("Communication Failed, Closing\r\n"); net2->sendRetry = false; - net2->pollRetryCount = 0; + net2->sendRetryCount = 0; net2->messageFailCount++; net2->cltCloseConnection=true; - net2->pollState=0; + net2->sendState=0; + net2->sendTime=0; } } } @@ -180,7 +215,7 @@ } } -//Ethernet to Serial transmit data +//Ethernet receive data and send to aux devices (serial, etc...) void datanrx(netsys *net2) { char rxbuf[256]; @@ -197,7 +232,7 @@ sport0.write(rxbuf,rxlen); for (rxc = 0;rxc<rxlen;rxc++) { - cmdbuf.push(rxbuf[rxc]); + receiveBuffer.push(rxbuf[rxc]); } net2->rxMessageCount++; } @@ -205,7 +240,7 @@ } } -//Ethernet to Serial receive data +//Serial device to server connected client void datasrx(netsys *net2) { char rxbuf[256]; @@ -253,7 +288,7 @@ int main() { EthernetInterface eth; - eth.set_network(mbedIp,mbedMask,mbedGateway); //Use these parameters for static IP + eth.set_network(setseveraddress,setsevermask,setsevergateway); //Use these parameters for static IP eth.connect(); printf("The target IP address is '%s'\r\n", eth.get_ip_address()); @@ -261,12 +296,18 @@ /* Setup Ethernet to Serial Connection Thread */ conchkthread[0].start(callback(conchk,&net1)); /* Setup polltick Ticker */ - nettimer[1].attach_us(heartbeat,10000); + nettimer.attach_us(heartbeat,10000); /* Setup Ethernet to Serial transmit data Thread */ rxtxdatathread[0].start(callback(datanrx,&net1)); /* Setup Ethernet to Serial receive data Thread */ rxtxdatathread[1].start(callback(datasrx,&net1)); - rxtxdatathread[2].start(callback(datapolltx,&net1)); + rxtxdatathread[2].start(callback(datantx,&net1)); + rxtxdatathread[3].start(callback(dataprocess,&net1)); + + char* tstcmd = "Tick\r\n"; + char tstlen; + unsigned int txc; + unsigned int sxc; while (true) { @@ -278,6 +319,7 @@ incTime(); currenttime=getTime(); net1.pollTime++; + net1.sendTime++; if (net1.srvIsActive) { net1.aliveTime++; @@ -290,24 +332,64 @@ } } - if (net1.pollTime >= net1.pollInterval) + if (net1.pollEnabled) { - switch (net1.pollState) + if (net1.pollTime >= net1.pollInterval) + { + tstlen=strlen(tstcmd); + sendLength.push(tstlen); + for (txc=0;txc<tstlen;txc++) + { + sendBuffer.push(tstcmd[txc]); + } + if (net1.sendState==0) + { + net1.sendState=1; + net1.pollRequestSent=true; + } + net1.pollTime=0; + } + if (net1.pollTime >= net1.pollTimeout) + { + if (net1.pollRequestSent) + { + if (net1.pollResponseReceived==false) + { + net1.pollTimeoutCount++; + net1.pollRequestSent=false; + } + } + } + } + if (net1.sendTime == 10) + { + switch (net1.sendState) { case 1: - + if (!sendLength.empty()) + { + sendLength.pop(net1.sendLen); + for (sxc=0;sxc<net1.sendLen;sxc++) + { + sendBuffer.pop(net1.sendString[sxc]); + } + } + else + { + net1.sendState=0; + } ret=net1.srv_sock.open(ð); printf("Socket%d\r\n",ret); if (ret < 0) { - net1.pollRetry = true; - net1.pollRetryCount=0; - net1.pollState=2; - net1.pollTime=net1.pollInterval-10; + net1.attachRetry = true; + net1.sendRetryCount=0; + net1.sendState=2; + net1.sendTime=0; break; } - net1.pollState=3; - net1.pollTime=0; + net1.sendState=3; + net1.sendTime=11; net1.cltIsActive=true; break; case 2: @@ -315,27 +397,32 @@ printf("Socket%d\r\n",ret); if (ret < 0) { - net1.pollRetry = true; - net1.pollRetryCount++; - net1.pollTime=net1.pollInterval-10; + net1.attachRetry = true; + net1.sendRetryCount++; + net1.sendTime=0; printf("Attach Attempt Failed, Code: %d\r\n",ret); - if (net1.pollRetryCount>3) + if (net1.sendRetryCount>3) { printf("Communication Failed, Closing\r\n"); - net1.pollRetry = false; - net1.pollRetryCount = 0; + net1.attachRetry = false; + net1.sendRetryCount = 0; net1.messageFailCount++; - net1.pollState=0; - net1.pollTime=0; + net1.sendState=0; + net1.sendTime=0; } break; } - net1.pollState=0; - net1.pollRetry = false; - net1.pollTime=0; + net1.sendState=0; + net1.attachRetry = false; + net1.sendTime=11; net1.cltIsActive=true; break; default: + net1.sendTime=0; + if (!sendLength.empty()) + { + net1.sendState=1; + } break; } }