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-11
- Revision:
- 2:ec972966689e
- Parent:
- 1:a57cbebba3fd
- Child:
- 3:ac1f2af8bd0f
File content as of revision 2:ec972966689e:
#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" #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" #define HTTP_MESSAGE_BODY "" \ "<html>" "\r\n" \ " <body style=\"display:flex;text-align:center\">" "\r\n" \ " <div style=\"margin:auto\">" "\r\n" \ " <h1>Hello World</h1>" "\r\n" \ " <p>It works !</p>" "\r\n" \ " </div>" "\r\n" \ " </body>" "\r\n" \ "</html>" #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ HTTP_HEADER_FIELDS "\r\n" \ "\r\n" \ HTTP_MESSAGE_BODY "\r\n" static const char* mbedIp = "10.150.1.241"; //IP static const char* mbedMask = "255.255.255.0"; // Mask static const char* mbedGateway = "10.150.1.245"; //Gateway Ticker nettimer[2]; struct netsys { TCPServer srv; TCPSocket clt_sock; SocketAddress clt_addr; int tcpport; bool isactive; bool closeconnection; int sockalivetime; } net1,nets,http1; bool polltick; BufferedSerial sport0(PD_5, PD_6, 256, 4); // UART2 Thread conchkthread[2]; Thread rxtxdatathread[2]; //Provides the deivce poll timing void heartbeat() { polltick=true; } //Provides the timeout for ethernet to serial communications void nettimeout(netsys *net2) { if (net2->isactive==true) { if (net2->sockalivetime==30) { net2->closeconnection=true; net2->sockalivetime=0; } else { net2->sockalivetime++; } } } //Ethernet to Serial transmit data void datanrx(netsys *net2) { char rxbuf[256]; int rxlen=0; while (1) { while (net2->isactive) { rxlen=net2->clt_sock.recv(rxbuf, sizeof(rxbuf)); net2->sockalivetime=0; sport0.write(rxbuf,rxlen); } } } //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->sockalivetime=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) { if (server->accept(client_socket, client_address) < 0) { printf("Connection Failed.\r\n"); return; } printf("Server Port %d\r\n", net2->tcpport); if (net2->tcpport==80) { printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port()); client_socket->send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); client_socket->close(); } else { printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port()); net2->isactive=true; client_socket->send("Hello", strlen("Hello")); } } } int main() { printf("Basic HTTP server example\r\n"); 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()); /* Error Messages */ nsapi_error_t ret; /* Open the server on ethernet stack */ net1.srv.open(ð); net1.isactive=false; net1.closeconnection=false; http1.srv.open(ð); /* Bind the HTTP port (TCP 80) to the server */ http1.tcpport=80; http1.srv.bind(http1.tcpport); /* Bind port 23 to the server */ net1.tcpport=23; net1.srv.bind(net1.tcpport); /* Can handle 5 simultaneous connections */ net1.srv.listen(5); http1.srv.listen(5); sport0.baud(9600); /* Setup Ethernet to Serial Connection Thread */ conchkthread[0].start(callback(conchk,&net1)); /* Setup Ethernet to Serial Timeout Ticker */ nettimer[0].attach(callback(nettimeout,&net1),1); /* Setup polltick Ticker */ nettimer[1].attach(heartbeat,10); /* 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)); /* Setup Web Server Connection Thread */ conchkthread[1].start(callback(conchk,&http1)); while (true) { if (net1.isactive) { if (net1.closeconnection==true) { net1.clt_sock.send("Connection Timeout", strlen("Connection Timeout")); net1.isactive=false; net1.clt_sock.close(); net1.closeconnection=false; } } if (polltick) { polltick=false; printf("Enter_Tick\r\n"); ret = nets.clt_sock.open(ð); printf("Socket%d\r\n",ret); //nets.clt_sock.set_blocking(true); //nets.clt_sock.set_timeout(10); ret = nets.clt_sock.connect("10.150.1.245",23000); printf("Connected %d\r\n",ret); ret = nets.clt_sock.send("Tick",strlen("Tick")); printf("Send Result %d\r\n",ret); nets.clt_sock.close(); } } }