Dependencies: BufferedSerial analogAverager voltageRegulator netStatReg analogMinMax CounterMinMax
main.cpp
- Committer:
- masterkookus
- Date:
- 2019-09-19
- Revision:
- 6:9f97716eae76
- Parent:
- 5:c656fd08007b
- Child:
- 7:be13a9037d41
File content as of revision 6:9f97716eae76:
#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; 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) { net1.cltPort=23; net1.srv_addr="10.150.1.241"; net1.cltIsActive=false; net1.cltCloseConnection=false; net1.pollTimeout=200; net1.pollTime=0; net1.pollInterval=200; net1.srv_sock.set_blocking(false); net1.aliveTimeout=50; net1.srv.set_blocking(false); /* Open the server on ethernet stack */ net1.srv.open(eth); net1.srvIsActive=false; net1.srvCloseConnection=false; /* Bind port 23 to the server */ net1.srvPort=23; net1.srv.bind(net1.srvPort); /* 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->cltIsActive) { while (net2->pollState==3) { if (net2->connectRetry == true) { wait_ms(100); } ret = net2->srv_sock.connect(net2->srv_addr,net2->cltPort); if (ret==0) { net2->connectRetry = false; printf("Connected %d\r\n",ret); net2->cltCloseConnection=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->cltCloseConnection=true; net2->pollState=0; } } } while (net2->pollState==4) { if (net2->sendRetry == true) { wait_ms(100); } ret = net2->srv_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->cltCloseConnection=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->cltCloseConnection=true; net2->pollState=0; } } } if (net2->cltCloseConnection==true) { net2->srv_sock.close(); net2->cltIsActive=false; } } } } //Ethernet to Serial transmit data void datanrx(netsys *net2) { char rxbuf[256]; int rxlen=0; int rxc; while (1) { while (net2->srvIsActive) { 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->srvIsActive) { 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); net2->txMessageCount++; } } } } //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->srvPort); printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port()); net2->aliveTime=0; net2->srvIsActive=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,&net1)); while (true) { polltick=getTick(); if (polltick) { setTick(false); incTime(); currenttime=getTime(); net1.pollTime++; if (net1.srvIsActive) { net1.aliveTime++; if (net1.aliveTime>net1.aliveTimeout) { printf("Closed\r\n"); net1.clt_sock.close(); net1.srvIsActive=false; net1.srvCloseConnection=false; } } if (net1.pollTime >= net1.pollInterval) { switch (net1.pollState) { case 1: 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; break; } net1.pollState=3; net1.pollTime=0; net1.cltIsActive=true; break; case 2: ret=net1.srv_sock.open(ð); printf("Socket%d\r\n",ret); if (ret < 0) { net1.pollRetry = true; net1.pollRetryCount++; net1.pollTime=net1.pollInterval-10; printf("Attach Attempt Failed, Code: %d\r\n",ret); if (net1.pollRetryCount>3) { printf("Communication Failed, Closing\r\n"); net1.pollRetry = false; net1.pollRetryCount = 0; net1.messageFailCount++; net1.pollState=0; net1.pollTime=0; } break; } net1.pollState=0; net1.pollRetry = false; net1.pollTime=0; net1.cltIsActive=true; break; default: break; } } } } }