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-09-19
- Revision:
- 5:c656fd08007b
- Parent:
- 4:98cdccfb17d6
- Child:
- 6:9f97716eae76
File content as of revision 5:c656fd08007b:
#if !FEATURE_LWIP #error [NOT_SUPPORTED] LWIP not supported for this target #endif #include "mbed.h" #include "EthernetInterface.h" #include "TCPServer.h" #include "TCPSocket.h" #include "BufferedSerial.h" #include "nettime.h" #include "netdevices.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]; struct netsys net1,nets; bool polltick; BufferedSerial sport0(PD_5, PD_6, 256, 4); // UART2 CircularBuffer<char, 256> cmdbuf; Thread conchkthread[2]; Thread rxtxdatathread[5]; unsigned int currenttime=0; nsapi_error_t ret; //Provides the deivce poll timing void heartbeat() { //polltick=true; setTick(true); } void confignetdevices(EthernetInterface *eth) { nets.tcpport=23; nets.eth=eth; nets.isactive=false; nets.closeconnection=false; nets.pollTimeout=200; nets.pollTime=0; nets.pollInterval=200; nets.clt_sock.set_blocking(false); net1.pollTimeout=50; net1.srv.set_blocking(false); /* Open the server on ethernet stack */ net1.srv.open(eth); net1.isactive=false; net1.closeconnection=false; /* Bind port 23 to the server */ net1.tcpport=23; net1.srv.bind(net1.tcpport); /* Can handle 5 simultaneous connections */ net1.srv.listen(5); sport0.baud(9600); } //Ethernet to Ethernet Send Command void datapolltx(netsys *net2) { char cchar; char cbuf[256]; unsigned int clen=0; unsigned int cbc; union packer4byte { unsigned int cmdint; char bytes[4]; }; packer4byte cmdpack; while(1) { while (!cmdbuf.empty()) { cmdbuf.pop(cchar); clen++; cbuf[clen]=cchar; } if (clen>3) { printf("%d\r\n",clen); //clen=0; for (cbc=0;cbc<clen-5;cbc++) { 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; clen=0; break; } } } if (net2->isactive) { while (net2->pollState==3) { if (net2->connectRetry == true) { wait_ms(100); } ret = net2->clt_sock.connect("10.150.1.241",nets.tcpport); if (ret==0) { net2->connectRetry = false; printf("Connected %d\r\n",ret); net2->closeconnection=true; net2->pollState=4; } else { net2->connectRetry = true; net2->pollRetryCount++; printf("Connect Attempt Failed, Code: %d\r\n",ret); if (net2->pollRetryCount>3) { printf("Communication Failed, Closing\r\n"); net2->connectRetry = false; net2->pollRetryCount = 0; net2->messageFailCount++; net2->closeconnection=true; net2->pollState=0; } } } while (net2->pollState==4) { if (net2->sendRetry == true) { wait_ms(100); } ret = net2->clt_sock.send("Tock\r\n",strlen("Tock\r\n")); if (ret>=0) { printf("Send Result %d\r\n",ret); net2->sendRetry = false; net2->txMessageCount++; net2->closeconnection=true; net2->pollState=0; } else { net2->sendRetry = true; net2->pollRetryCount++; printf("Send Attempt Failed, Code: %d\r\n",ret); if (net2->pollRetryCount>3) { printf("Communication Failed, Closing\r\n"); net2->sendRetry = false; net2->pollRetryCount = 0; net2->messageFailCount++; net2->closeconnection=true; net2->pollState=0; } } } if (net2->closeconnection==true) { net2->clt_sock.close(); net2->isactive=false; } } } } //Ethernet to Serial transmit data void datanrx(netsys *net2) { char rxbuf[256]; int rxlen=0; int rxc; while (1) { while (net2->isactive) { rxlen=net2->clt_sock.recv(rxbuf, sizeof(rxbuf)); if (rxlen>0) { net2->aliveTime=0; sport0.write(rxbuf,rxlen); for (rxc = 0;rxc<rxlen;rxc++) { cmdbuf.push(rxbuf[rxc]); } net2->rxMessageCount++; } } } } //Ethernet to Serial receive data void datasrx(netsys *net2) { char rxbuf[256]; int rxlen=0; int rxc; while (1) { while (net2->isactive) { rxlen=sport0.readable(); if (rxlen>0) { for (rxc = 0;rxc<rxlen;rxc++) { rxbuf[rxc] = sport0.getc(); } net2->aliveTime=0; net2->clt_sock.send(rxbuf, rxlen); } } } } //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) { while (server->accept(client_socket, client_address) < 0) { //printf("Connection Failed.\r\n"); } printf("Server Port %d\r\n", net2->tcpport); printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port()); net2->aliveTime=0; net2->isactive=true; } } int main() { EthernetInterface eth; eth.set_network(mbedIp,mbedMask,mbedGateway); //Use these parameters for static IP eth.connect(); printf("The target IP address is '%s'\r\n", eth.get_ip_address()); confignetdevices(ð); /* Setup Ethernet to Serial Connection Thread */ conchkthread[0].start(callback(conchk,&net1)); /* Setup polltick Ticker */ nettimer[1].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,&nets)); while (true) { polltick=getTick(); if (polltick) { setTick(false); incTime(); currenttime=getTime(); nets.pollTime++; if (net1.isactive) { net1.aliveTime++; if (net1.aliveTime>net1.pollTimeout) { printf("Closed\r\n"); net1.clt_sock.close(); net1.isactive=false; net1.closeconnection=false; } } if (nets.pollTime >= nets.pollInterval) { switch (nets.pollState) { case 1: ret=nets.clt_sock.open(ð); printf("Socket%d\r\n",ret); if (ret < 0) { nets.pollRetry = true; nets.pollRetryCount=0; nets.pollState=2; nets.pollTime=nets.pollInterval-10; break; } nets.pollState=3; nets.pollTime=0; nets.isactive=true; break; case 2: ret=nets.clt_sock.open(ð); printf("Socket%d\r\n",ret); if (ret < 0) { nets.pollRetry = true; nets.pollRetryCount++; nets.pollTime=nets.pollInterval-10; printf("Attach Attempt Failed, Code: %d\r\n",ret); if (nets.pollRetryCount>3) { printf("Communication Failed, Closing\r\n"); nets.pollRetry = false; nets.pollRetryCount = 0; nets.messageFailCount++; nets.pollState=0; nets.pollTime=0; } break; } nets.pollState=0; nets.pollRetry = false; nets.pollTime=0; nets.isactive=true; break; default: break; } } } } }