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

Revision:
0:03ab7f7596e2
Child:
1:a57cbebba3fd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 11 11:40:34 2019 +0000
@@ -0,0 +1,183 @@
+#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,http1;
+
+BufferedSerial sport0(PD_5, PD_6, 256, 4);    // UART2
+
+Thread conchkthread[2];
+Thread rxtxdatathread[2];
+
+void nettimeout(netsys *net2)
+{
+    if (net2->isactive==true)
+    {
+        if (net2->sockalivetime==30)
+        {
+            net2->closeconnection=true;
+            net2->sockalivetime=0;
+        }
+        else
+        {
+            net2->sockalivetime++;
+        }
+    }
+}
+
+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);
+        }
+    }
+}
+
+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);
+            }
+        }
+    }
+}
+
+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());
+    
+    /* Open the server on ethernet stack */
+    net1.srv.open(&eth);
+    net1.isactive=false;
+    net1.closeconnection=false;
+    http1.srv.open(&eth);
+    
+    /* Bind the HTTP port (TCP 80) to the server */
+    http1.tcpport=80;
+    http1.srv.bind(http1.tcpport);
+    
+    /* Bind port 23000 to the server */
+    net1.tcpport=23;
+    net1.srv.bind(net1.tcpport);
+    
+    /* Can handle 5 simultaneous connections */
+    net1.srv.listen(1);
+    http1.srv.listen(5);
+    
+    sport0.baud(9600);
+    
+    conchkthread[0].start(callback(conchk,&net1));
+    nettimer[0].attach(callback(nettimeout,&net1),1);
+    rxtxdatathread[0].start(callback(datanrx,&net1));
+    rxtxdatathread[1].start(callback(datasrx,&net1));
+    
+    conchkthread[1].start(callback(conchk,&http1));
+    
+    while (true) {
+        while (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;
+            }
+            
+        }
+    }
+}