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 13:4d533751f951, committed 2019-10-07
- Comitter:
- masterkookus
- Date:
- Mon Oct 07 17:32:01 2019 +0000
- Parent:
- 12:4bb088c27838
- Child:
- 14:3d437acb92e3
- Commit message:
- Completed net class and fixed message lock error;
Changed in this revision
--- a/main.cpp Mon Oct 07 12:44:38 2019 +0000 +++ b/main.cpp Mon Oct 07 17:32:01 2019 +0000 @@ -25,6 +25,8 @@ Timer netDevTimers[6]; struct netsys net1; +netDevStatus netStat(0,0,0,0,0); + struct vRegData vReg1; analogAverager voltageAvg(120,true,true,0.9,true,true,1.1); @@ -59,32 +61,27 @@ setSendTick(true); } //Server Unsolicited Message Timeout - //if (bit_isset(net1.statusReg,srvIsActive)) - if (bit_isset(srvIsActive)) + if (netStat.bit_isset(srvIsActive)) { if (netDevTimers[1].read_ms()>=setservertimeout) { - netDevTimers[1].reset(); + //netDevTimers[1].reset(); netDevTimers[1].stop(); - //bit_toset(net1.statusReg,srvCloseConnection); - bit_toset(srvCloseConnection); + netStat.bit_toset(srvCloseConnection); } } - //if (bit_isset(net1.statusReg,devOnline)) - if (bit_isset(devOnline)) + if (netStat.bit_isset(devOnline)) { //Fast Demand Poll Tick - //if (bit_isset(net1.statusReg,fmdEnabled)) - if (bit_isset(fmdEnabled)) + if (netStat.bit_isset(fmdEnabled)) { if (netDevTimers[2].read_ms()>=setfmdemandpollinterval) { netDevTimers[2].reset(); - //bit_toset(net1.statusReg,fmdPollReq); - bit_toset(fmdPollReq); - voltageAvg.resetNum(); + netStat.bit_toset(fmdPollReq); + /*voltageAvg.resetNum(); voltageMinMax.resetNum(); - tapMinMax.resetNum(); + tapMinMax.resetNum();*/ } } else @@ -92,19 +89,16 @@ if (netDevTimers[2].read_ms()>=8000) { netDevTimers[2].reset(); - //bit_toset(net1.statusReg,fmdPollReq); - bit_toset(fmdPollReq); + netStat.bit_toset(fmdPollReq); } } //Fast Meter Poll Tick - //if (bit_isset(net1.statusReg,fmEnabled)) - if (bit_isset(fmEnabled)) + if (netStat.bit_isset(fmEnabled)) { if (netDevTimers[3].read_ms()>=setfmpollinterval) { netDevTimers[3].reset(); - //bit_toset(net1.statusReg,fmPollReq); - bit_toset(fmPollReq); + netStat.bit_toset(fmPollReq); } } else @@ -112,8 +106,7 @@ if (netDevTimers[3].read_ms()>=8000) { netDevTimers[3].reset(); - //bit_toset(net1.statusReg,fmPollReq); - bit_toset(fmPollReq); + netStat.bit_toset(fmPollReq); } } } @@ -122,43 +115,61 @@ if (netDevTimers[3].read_ms()>=8000) { netDevTimers[3].reset(); - //bit_toset(net1.statusReg,fmCfgReq); - bit_toset(fmCfgReq); + netStat.bit_toset(fmCfgReq); } } //Fast Message Timeout - //if ((bit_isset(net1.statusReg,fmPollInProg)) || (bit_isset(net1.statusReg,fmdPollInProg))) - if ((bit_isset(fmPollInProg)) || (bit_isset(fmdPollInProg))) + if ((netStat.bit_isset(fmPollInProg)) || (netStat.bit_isset(fmdPollInProg))) { if (netDevTimers[4].read_ms()>=2000) { - netDevTimers[4].reset(); + //netDevTimers[4].reset(); netDevTimers[4].stop(); - //bit_toset(net1.statusReg,fmCloseConnection); - //bit_toset(net1.statusReg,fmTimeout); - bit_toset(fmCloseConnection); - bit_toset(fmTimeout); + netStat.bit_toset(fmCloseConnection); + netStat.bit_toset(fmTimeout); } } //Serial Message Timeout - //if (bit_isset(net1.statusReg,serPollInProg)) - if (bit_isset(serPollInProg)) + if (netStat.bit_isset(serPollInProg)) { if (netDevTimers[5].read_ms()>=net1.serMsgTimeout) { - netDevTimers[5].reset(); + //netDevTimers[5].reset(); netDevTimers[5].stop(); - //bit_toset(net1.statusReg,serCloseConnection); - //bit_toset(net1.statusReg,serTimeout); - bit_toset(serCloseConnection); - bit_toset(serTimeout); + netStat.bit_toset(serCloseConnection); + netStat.bit_toset(serTimeout); } } } +void sendCmd(char cmdNum) +{ + sendLength.push(2); + for (char txc=0;txc<2;txc++) + { + sendBuffer.push(fmCmd[cmdNum][txc]); + } +} + +void sendSerCmd(char cmdNum, char cmdCnt) +{ + sendLength.push(cmdCnt); + for (char txc=0;txc<cmdCnt;txc++) + { + sendBuffer.push(serCmd[cmdNum][txc]); + } +} + void confignetdevices(EthernetInterface *eth) { - //net1.statusReg=0; + #ifdef sportEnabled + netStat.bit_toset(serEnabled); + #endif + + #ifdef setPollEnabled + netStat.bit_toset(pollEnabled); + #endif + net1.cltPort=setclientport; net1.srv_addr=setclientaddress; net1.srv_sock.set_blocking(true); @@ -167,9 +178,7 @@ net1.srv.set_timeout(100); net1.sendRetryCount=0; net1.messageFailCount=0; - net1.txMessageCount=0; - net1.rxMessageCount=0; - net1.serMsgTimeout=150; + net1.serMsgTimeout=2000; /* Open the server on ethernet stack */ net1.srv.open(eth); @@ -203,19 +212,19 @@ clen++; } //If a Poll Request has been made and data has been received set flag - //bit_sis(bit_isset(net2->statusReg,serPollInProg),net2->statusReg,serRespRx); - //bit_sis(bit_isset(net2->statusReg,fmPollInProg),net2->statusReg,fmRespRx); - //bit_sis(bit_isset(net2->statusReg,fmdPollInProg),net2->statusReg,fmdRespRx); - bit_sis(serPollInProg,serRespRx); - bit_sis(fmPollInProg,fmRespRx); - bit_sis(fmdPollInProg,fmdRespRx); + netStat.bit_sis(serPollInProg,serRespRx); + netStat.bit_sis(fmPollInProg,fmRespRx); + netStat.bit_sis(fmdPollInProg,fmdRespRx); //Check to see if bulk data and reports being sent from device due to serial command - //if (bit_isset(net2->statusReg,serMsgRx)) - if (bit_isset(serMsgRx)) + if (netStat.bit_isset(serMsgRx)) { - sport0.write(cbuf,clen); - clen=0; + if (clen>0) + { + sport0.write(cbuf,clen); + netDevTimers[5].reset(); + clen=0; + } } //Check to see if at least 4 characters have been received else if (clen>3) @@ -227,24 +236,21 @@ if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC0)) { sport0.write(cbuf,clen); - //bit_toclear(net1.statusReg,fmCfgInProg); - //bit_toset(net1.statusReg,devOnline); - bit_toclear(fmCfgInProg); - bit_toset(devOnline); + netStat.bit_toclear(fmCfgInProg); + netStat.bit_toset(devOnline); clen=0; break; } //Check to see if serial commands are currently being received - //if (bit_isset(net2->statusReg,serPollInProg)) - if (bit_isset(serPollInProg)) + if (netStat.bit_isset(serPollInProg)) { //If relay acknowledges max metering command set bulk receive flag if ((cbuf[cbc]==0x4d) && (cbuf[cbc+1]==0x45) && (cbuf[cbc+2]==0x54)) { sport0.write(cbuf,clen); - //bit_toset(net2->statusReg,serMsgRx); - bit_toset(serMsgRx); - net2->devMsgReq=0; + netStat.bit_toset(serMsgRx); + netDevTimers[5].reset(); + netStat.setDevMsgReq(0); clen=0; break; } @@ -252,9 +258,9 @@ if ((cbuf[cbc]==0x54) && (cbuf[cbc+1]==0x41) && (cbuf[cbc+2]==0x50)) { sport0.write(cbuf,clen); - //bit_toset(net2->statusReg,serMsgRx); - bit_toset(serMsgRx); - net2->devMsgReq=0; + netStat.bit_toset(serMsgRx); + netDevTimers[5].reset(); + netStat.setDevMsgReq(0); clen=0; break; } @@ -262,7 +268,9 @@ if ((cbuf[cbc]==0x41) && (cbuf[cbc+1]==0x43) && (cbuf[cbc+2]==0x43)) { sport0.write(cbuf,clen); - net2->devMsgPos=1; + netStat.setDevMsgPos(1); + sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); + netDevTimers[5].reset(); clen=0; break; } @@ -270,7 +278,9 @@ if (cbuf[cbc]==0x2a) { sport0.write(cbuf,clen); - net2->devMsgPos=2; + netStat.setDevMsgPos(2); + sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); + netDevTimers[5].reset(); clen=0; break; } @@ -278,7 +288,9 @@ if ((cbuf[cbc]==0x3d) && (cbuf[cbc+1]==0x3e)) { sport0.write(cbuf,clen); - net2->devMsgPos=4; + netStat.setDevMsgPos(4); + sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); + netDevTimers[5].reset(); clen=0; break; } @@ -286,13 +298,15 @@ if ((cbuf[cbc]==0x3d)) { sport0.write(cbuf,clen); - net2->devMsgPos=0; + netStat.setDevMsgPos(0); + sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); + netDevTimers[5].reset(); clen=0; break; } } //if (bit_isset(net2->statusReg,fmPollInProg)) - if (bit_isset(fmPollInProg)) + if (netStat.bit_isset(fmPollInProg)) { //Check fast meter configuration if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC1)) @@ -334,7 +348,7 @@ anum = anum + 11; } //bit_toset(net2->statusReg,fmEnabled); - bit_toset(fmEnabled); + netStat.bit_toset(fmEnabled); clen=0; break; } @@ -374,8 +388,7 @@ break; } } - //if (bit_isset(net2->statusReg,fmdPollInProg)) - if (bit_isset(fmdPollInProg)) + if (netStat.bit_isset(fmdPollInProg)) { //Check fast demand meter configuration if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC2)) @@ -407,8 +420,7 @@ } anum = anum + 11; } - //bit_toset(net2->statusReg,fmdEnabled); - bit_toset(fmdEnabled); + netStat.bit_toset(fmdEnabled); clen=0; break; } @@ -446,6 +458,10 @@ sport0.printf("%.2f\r\n",vReg1.calculated[2].analog1Value); sport0.printf("%.2f\r\n",vReg1.calculated[3].analog1Value); sport0.printf("%.2f\r\n",vReg1.calculated[4].analog1Value); + + voltageAvg.resetNum(); + voltageMinMax.resetNum(); + tapMinMax.resetNum(); break; } } @@ -466,15 +482,13 @@ { while(1) { - //if (bit_isset(net2->statusReg,cltIsActive)) - if (bit_isset(cltIsActive)) + if (netStat.bit_isset(cltIsActive)) { //Attempt to connect to server while (net2->sendState==3) { //delay connect attempt if previous attempt unsuccessful - //if (bit_isset(net2->statusReg,connectRetry)) - if (bit_isset(connectRetry)) + if (netStat.bit_isset(connectRetry)) { wait_ms(100); } @@ -483,8 +497,7 @@ //If connect successful proceed to send data if (ret==0) { - //bit_toclear(net2->statusReg,connectRetry); - bit_toclear(connectRetry); + netStat.bit_toclear(connectRetry); #ifdef netmsgdebug printf("Connected %d\r\n",ret); #endif @@ -496,15 +509,13 @@ #ifdef netmsgdebug printf("May already be connected, attempting send.\r\n"); #endif - //bit_toclear(net2->statusReg,connectRetry); - bit_toclear(connectRetry); + netStat.bit_toclear(connectRetry); net2->sendState=4; } //If connect attempt failed on other error attempt reconnect else { - //bit_toset(net2->statusReg,connectRetry); - bit_toset(connectRetry); + netStat.bit_toset(connectRetry); net2->sendRetryCount++; #ifdef netmsgdebug printf("Connect Attempt Failed, Code: %d\r\n",ret); @@ -515,12 +526,10 @@ #ifdef netmsgdebug printf("Communication Failed, Closing\r\n"); #endif - //bit_toclear(net2->statusReg,connectRetry); - bit_toclear(connectRetry); + netStat.bit_toclear(connectRetry); net2->sendRetryCount = 0; net2->messageFailCount++; - //bit_toset(net2->statusReg,cltCloseConnection); - bit_toset(cltCloseConnection); + netStat.bit_toset(cltCloseConnection); net2->sendState=0; } } @@ -529,8 +538,7 @@ while (net2->sendState==4) { //Delay send data attempt if previous attempt failed - //if (bit_isset(net2->statusReg,sendRetry)) - if (bit_isset(sendRetry)) + if (netStat.bit_isset(sendRetry)) { wait_ms(100); } @@ -542,16 +550,15 @@ #ifdef netmsgdebug printf("Send Result %d\r\n",ret); #endif - //bit_toclear(net2->statusReg,sendRetry); - bit_toclear(sendRetry); - net2->txMessageCount++; + netStat.bit_toclear(sendRetry); + //net2->txMessageCount++; + netStat.incCount(1); net2->sendState=5; } //If send attempt failed attempt to re-send data else { - //bit_toset(net2->statusReg,sendRetry); - bit_toset(sendRetry); + netStat.bit_toset(sendRetry); net2->sendRetryCount++; #ifdef netmsgdebug printf("Send Attempt Failed, Code: %d\r\n",ret); @@ -562,12 +569,10 @@ #ifdef netmsgdebug printf("Communication Failed, Closing\r\n"); #endif - //bit_toclear(net2->statusReg,sendRetry); - bit_toclear(sendRetry); + netStat.bit_toclear(sendRetry); net2->sendRetryCount = 0; net2->messageFailCount++; - //bit_toset(net2->statusReg,cltCloseConnection); - bit_toset(cltCloseConnection); + netStat.bit_toset(cltCloseConnection); net2->sendState=0; } } @@ -585,8 +590,7 @@ while (1) { //If the client is active check to see if data has been received - //while (bit_isset(net2->statusReg,cltIsActive)) - while (bit_isset(cltIsActive)) + while (netStat.bit_isset(cltIsActive)) { //Store the length of the received data rxlen=net2->srv_sock.recv(rxbuf, sizeof(rxbuf)); @@ -601,7 +605,7 @@ printf("Client Received Data\r\n"); #endif //Increment received message counter - net2->rxMessageCount++; + netStat.incCount(2); } } } @@ -616,8 +620,7 @@ while (1) { //If the server is active check to see if data has been received - //while (bit_isset(net2->statusReg,srvIsActive)) - while (bit_isset(srvIsActive)) + while (netStat.bit_isset(srvIsActive)) { //Store the length of the received data rxlen=net2->clt_sock.recv(rxbuf, sizeof(rxbuf)); @@ -632,7 +635,7 @@ printf("Server Received Data\r\n"); #endif //Increment received message counter - net2->rxMessageCount++; + netStat.incCount(2); } } } @@ -666,8 +669,6 @@ } //Store the current cursor location in the buffer rxindex=rxindex+rxlen; - //printf("%d\r\n",rxlen); - //printf("%d\r\n",rxindex); //Set the received length to zero rxlen=0; //If there is data in the buffer and a carriage return has been detected search for a command @@ -681,15 +682,15 @@ cmdpack.bytes[2]=rxbuf[rxc+1]; cmdpack.bytes[1]=rxbuf[rxc+2]; cmdpack.bytes[0]=rxbuf[rxc+3]; - //printf("%08x\r\n",cmdpack.cmdint); //Search for the Min/Max command if (cmdpack.cmdint==0x4d45544d) { #ifdef netmsgdebug printf("MIN MAX METERING\r\n"); #endif - net2->devMsgReq=0; - net2->devMsgPos=2; + netStat.setDevMsgReq(0); + netStat.setDevMsgPos(2); + netStat.bit_toset(serPollReq); rxindex=0; } //Search for the Tap Report Command @@ -698,8 +699,18 @@ #ifdef netmsgdebug printf("TAP REPORT\r\n"); #endif - net2->devMsgReq=1; - net2->devMsgPos=2; + netStat.setDevMsgReq(1); + netStat.setDevMsgPos(2); + netStat.bit_toset(serPollReq); + rxindex=0; + } + //Search for the Tap Report Command + if (cmdpack.cmdint==0x53544154) + { + #ifdef netmsgdebug + printf("STATS\r\n"); + #endif + sport0.printf("Status %08x\r\n",netStat.getStatus()); rxindex=0; } } @@ -734,26 +745,9 @@ printf("Server Port %d\r\n", net2->srvPort); printf("accept %s:%d\r\n", client_address->get_ip_address(), client_address->get_port()); #endif - //bit_toset(net2->statusReg,srvIsActive); - bit_toset(srvIsActive); - } -} - -void sendCmd(char cmdNum) -{ - sendLength.push(2); - for (char txc=0;txc<2;txc++) - { - sendBuffer.push(fmCmd[cmdNum][txc]); - } -} - -void sendSerCmd(char cmdNum, char cmdCnt) -{ - sendLength.push(cmdCnt); - for (char txc=0;txc<cmdCnt;txc++) - { - sendBuffer.push(serCmd[cmdNum][txc]); + netStat.bit_toset(srvIsActive); + netDevTimers[1].reset(); + netDevTimers[1].start(); } } @@ -794,148 +788,121 @@ if (polltick) { setPollTick(false); - //if (bit_isset(net1.statusReg,srvCloseConnection)) - if (bit_isset(srvCloseConnection)) + + if (netStat.bit_isset(srvCloseConnection)) { #ifdef netmsgdebug printf("Closed\r\n"); #endif net1.clt_sock.close(); - //bit_toclear(net1.statusReg,srvIsActive); - //bit_toclear(net1.statusReg,srvCloseConnection); - bit_toclear(srvIsActive); - bit_toclear(srvCloseConnection); + netStat.bit_toclear(srvIsActive); + netStat.bit_toclear(srvCloseConnection); } - //if (bit_isset(net1.statusReg,serCloseConnection)) - if (bit_isset(serCloseConnection)) + if (netStat.bit_isset(serCloseConnection)) { #ifdef netmsgdebug printf("Serial Connection Closed\r\n"); #endif - net1.srv_sock.close(); - net1.sendState=0; - //if (bit_isset(net1.statusReg,serRespRx)) - if (bit_isset(serRespRx)) + if (netStat.bit_isset(serRespRx)) { - net1.rxMessageCount++; + netStat.incCount(2); } else { - net1.serTimeoutCount++; + netStat.incCount(4); } - //bit_toclear(net1.statusReg,serclose); - bit_toclear(serclose); + netStat.bit_toclear(serclose); + netStat.bit_toset(cltCloseConnection); } - //if (bit_isset(net1.statusReg,fmCloseConnection)) - if (bit_isset(fmCloseConnection)) + if (netStat.bit_isset(fmCloseConnection)) { #ifdef netmsgdebug printf("Fast Meter Connection Closed\r\n"); #endif - net1.srv_sock.close(); - //bit_toclear(net1.statusReg,cltIsActive); - bit_toclear(cltIsActive); - net1.sendState=0; - //if ((bit_isset(net1.statusReg,fmRespRx)) || (bit_isset(net1.statusReg,fmdRespRx))) - if ((bit_isset(fmRespRx)) || (bit_isset(fmdRespRx))) + if ((netStat.bit_isset(fmRespRx)) || (netStat.bit_isset(fmdRespRx))) { - net1.rxMessageCount++; + netStat.incCount(2); } else { - net1.fmTimeoutCount++; + netStat.incCount(3); } - //bit_toclear(net1.statusReg,fmclose); - bit_toclear(fmclose); - //printf("Status %08x\r\n",net1.statusReg); + netStat.bit_toclear(fmclose); + netStat.bit_toset(cltCloseConnection); } - //if (bit_isset(net1.statusReg,cltCloseConnection)) - if (bit_isset(cltCloseConnection)) + if (netStat.bit_isset(cltCloseConnection)) { + sport0.printf("Status %08x\r\n",netStat.getStatus()); #ifdef netmsgdebug printf("Connection Closed\r\n"); #endif net1.srv_sock.close(); net1.sendState=0; - //bit_toclear(net1.statusReg,cltclose); - bit_toclear(cltclose); + netStat.bit_toclear(cltclose); } - //if ((bit_isclear(net1.statusReg,serPollInProg)) && (bit_isclear(net1.statusReg,fmPollInProg)) && (bit_isclear(net1.statusReg,fmdPollInProg)) && (bit_isset(net1.statusReg,devOnline))) - if ((bit_isclear(serPollInProg)) && (bit_isclear(fmPollInProg)) && (bit_isclear(fmdPollInProg)) && (bit_isset(devOnline))) + if ((netStat.bit_isclear(serPollInProg)) && (netStat.bit_isclear(fmPollInProg)) && (netStat.bit_isclear(fmdPollInProg)) && (netStat.bit_isset(devOnline))) { - //if (bit_isset(net1.statusReg,fmdPollReq)) - if (bit_isset(fmdPollReq)) + if (netStat.bit_isset(fmdPollReq)) { - //if (bit_isset(net1.statusReg,fmdEnabled)) - if (bit_isset(fmdEnabled)) + if (netStat.bit_isset(fmdEnabled)) { printf("Demand Reading\r\n"); sendCmd(4); - //bit_toclear(net1.statusReg,fmdPollReq); - //bit_toset(net1.statusReg,fmdPollInProg); - bit_toclear(fmdPollReq); - bit_toset(fmdPollInProg); + netStat.bit_toclear(fmdPollReq); + netStat.bit_toset(fmdPollInProg); } else { printf("Demand Configuration\r\n"); sendCmd(3); - //bit_toclear(net1.statusReg,fmdPollReq); - //bit_toset(net1.statusReg,fmdPollInProg); - bit_toclear(fmdPollReq); - bit_toset(fmdPollInProg); + netStat.bit_toclear(fmdPollReq); + netStat.bit_toset(fmdPollInProg); } + netDevTimers[4].reset(); netDevTimers[4].start(); } - //else if (bit_isset(net1.statusReg,fmPollReq)) - else if (bit_isset(fmPollReq)) + else if (netStat.bit_isset(fmPollReq)) { - //if (bit_isset(net1.statusReg,fmEnabled)) - if (bit_isset(fmEnabled)) + if (netStat.bit_isset(fmEnabled)) { printf("Meter Reading\r\n"); sendCmd(2); - //bit_toclear(net1.statusReg,fmPollReq); - //bit_toset(net1.statusReg,fmPollInProg); - bit_toclear(fmPollReq); - bit_toset(fmPollInProg); + netStat.bit_toclear(fmPollReq); + netStat.bit_toset(fmPollInProg); } else { printf("Meter Configuration\r\n"); sendCmd(1); - //bit_toclear(net1.statusReg,fmPollReq); - //bit_toset(net1.statusReg,fmPollInProg); - bit_toclear(fmPollReq); - bit_toset(fmPollInProg); + netStat.bit_toclear(fmPollReq); + netStat.bit_toset(fmPollInProg); } + netDevTimers[4].reset(); netDevTimers[4].start(); } - //else if (bit_isset(net1.statusReg,serPollReq)) - else if (bit_isset(serPollReq)) + else if (netStat.bit_isset(serPollReq)) { - //if (bit_isset(net1.statusReg,serEnabled)) - if (bit_isset(serEnabled)) + if (netStat.bit_isset(serEnabled)) { - sendSerCmd(serCmdSeq[net1.devMsgReq][net1.devMsgPos],serCmdlen[net1.devMsgReq][net1.devMsgPos]); - //bit_toclear(net1.statusReg,serPollReq); - //bit_toset(net1.statusReg,serPollInProg); - bit_toclear(serPollReq); - bit_toset(serPollInProg); + sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); + netStat.bit_toclear(serPollReq); + netStat.bit_toset(serPollInProg); + netDevTimers[5].reset(); + netDevTimers[5].start(); } - netDevTimers[5].start(); + else + { + netStat.bit_toclear(serPollReq); + } } } - //if ((bit_isclear(net1.statusReg,devOnline)) && (bit_isclear(net1.statusReg,fmCfgInProg)) && (bit_isset(net1.statusReg,fmCfgReq))) - if ((bit_isclear(devOnline)) && (bit_isclear(fmCfgInProg)) && (bit_isset(fmCfgReq))) + if ((netStat.bit_isclear(devOnline)) && (netStat.bit_isclear(fmCfgInProg)) && (netStat.bit_isset(fmCfgReq))) { sendCmd(0); - //bit_toclear(net1.statusReg,fmCfgReq); - //bit_toset(net1.statusReg,fmCfgInProg); - bit_toclear(fmCfgReq); - bit_toset(fmCfgInProg); + netStat.bit_toclear(fmCfgReq); + netStat.bit_toset(fmCfgInProg); net1.sendState=1; } } @@ -947,12 +914,13 @@ if (sendtick) { setSendTick(false); - sport0.printf("Status %08x\r\n",getStatus()); + //sport0.printf("Status %08x\r\n",netStat.getStatus()); switch (net1.sendState) { case 0: if (!sendLength.empty()) { + sport0.printf("Status %08x\r\n",netStat.getStatus()); sendLength.pop(net1.sendLen); for (sxc=0;sxc<net1.sendLen;sxc++) { @@ -962,18 +930,6 @@ } break; 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(ð); #ifdef netmsgdebug printf("Socket%d\r\n",ret); @@ -988,16 +944,14 @@ } else { - //bit_toset(net1.statusReg,attachRetry); - bit_toset(attachRetry); + netStat.bit_toset(attachRetry); net1.sendRetryCount=0; net1.sendState=2; break; } } net1.sendState=3; - //bit_toset(net1.statusReg,cltIsActive); - bit_toset(cltIsActive); + netStat.bit_toset(cltIsActive); break; case 2: ret=net1.srv_sock.open(ð); @@ -1006,8 +960,7 @@ #endif if (ret < 0) { - //bit_toset(net1.statusReg,attachRetry); - bit_toset(attachRetry); + netStat.bit_toset(attachRetry); net1.sendRetryCount++; #ifdef netmsgdebug printf("Attach Attempt Failed, Code: %d\r\n",ret); @@ -1017,8 +970,7 @@ #ifdef netmsgdebug printf("Communication Failed, Closing\r\n"); #endif - //bit_toclear(net1.statusReg,attachRetry); - bit_toclear(attachRetry); + netStat.bit_toclear(attachRetry); net1.sendRetryCount = 0; net1.messageFailCount++; net1.sendState=0; @@ -1026,14 +978,13 @@ break; } net1.sendState=3; - //bit_toclear(net1.statusReg,attachRetry); - //bit_toset(net1.statusReg,cltIsActive); - bit_toclear(attachRetry); - bit_toset(cltIsActive); + netStat.bit_toclear(attachRetry); + netStat.bit_toset(cltIsActive); break; case 5: if (!sendLength.empty()) { + sport0.printf("Status %08x\r\n",netStat.getStatus()); sendLength.pop(net1.sendLen); for (sxc=0;sxc<net1.sendLen;sxc++) {
--- a/mydevices.h Mon Oct 07 12:44:38 2019 +0000 +++ b/mydevices.h Mon Oct 07 17:32:01 2019 +0000 @@ -1,3 +1,7 @@ + +#define sportEnabled +#define setPollEnabled + /*Client Information*/ #define setclientport 23000 #define setclientaddress "10.150.1.242" @@ -6,7 +10,6 @@ #define setpollinterval 15 //In 10ms intervals #define setfmpollinterval 5000 #define setfmdemandpollinterval 60000 -#define setpollenabled true #define setserverport 23 #define setseveraddress "10.150.1.241"
--- a/netStatusReg.cpp Mon Oct 07 12:44:38 2019 +0000 +++ b/netStatusReg.cpp Mon Oct 07 17:32:01 2019 +0000 @@ -1,69 +1,160 @@ #include "netStatusReg.h" -static volatile unsigned int statusReg = 0; +//static volatile unsigned int statusReg = 0; -unsigned int getStatus(void) +netDevStatus::netDevStatus(unsigned int setStatus,unsigned int settxMC,unsigned int setrxMC,unsigned int setfmTC,unsigned int setsTC) +{ + statusReg = setStatus; + txMessageCount=settxMC; + rxMessageCount=setrxMC; + fmTimeoutCount=setfmTC; + serTimeoutCount=setsTC; +} + +unsigned int netDevStatus::getStatus(void) { return statusReg; } -bool bit_isset(unsigned int m) +//Get count Value +unsigned int netDevStatus::getCount(char counterNum) +{ + switch (counterNum) + { + case 1: + return txMessageCount; + case 2: + return rxMessageCount; + case 3: + return fmTimeoutCount; + case 4: + return serTimeoutCount; + default: + return 0; + } +} + +//Increment count Value +void netDevStatus::incCount(char counterNum) +{ + switch (counterNum) + { + case 1: + txMessageCount++; + break; + case 2: + rxMessageCount++; + break; + case 3: + fmTimeoutCount++; + break; + case 4: + serTimeoutCount++; + break; + default: + break; + } +} + +//Set count Value +void netDevStatus::setCount(char counterNum, unsigned int newCount) +{ + switch (counterNum) + { + case 1: + txMessageCount=newCount; + break; + case 2: + rxMessageCount=newCount; + break; + case 3: + fmTimeoutCount=newCount; + break; + case 4: + serTimeoutCount=newCount; + break; + default: + break; + } +} + +void netDevStatus::setDevMsgReq(char setReq) +{ + devMsgReq=setReq; +} +char netDevStatus::getDevMsgReq(void) +{ + return devMsgReq; +} +void netDevStatus::setDevMsgPos(char setPos) +{ + devMsgPos=setPos; +} +char netDevStatus::getDevMsgPos(void) +{ + return devMsgPos; +} +//Return true if bit is set +bool netDevStatus::bit_isset(unsigned int m) { return (((statusReg) & (m)) ? true : false); } -bool bit_isclear(unsigned int m) +//Return true if bit is clear +bool netDevStatus::bit_isclear(unsigned int m) { return (((statusReg) & (m)) ? false : true); } -void bit_toset(unsigned int m) +//Set bit +void netDevStatus::bit_toset(unsigned int m) { ((statusReg) |= (m)); } -void bit_toclear(unsigned int m) +//Clear bit +void netDevStatus::bit_toclear(unsigned int m) { ((statusReg) &= ~(m)); } //Bit set if set, do nothing if not set -void bit_sis(unsigned int c,unsigned int m) +void netDevStatus::bit_sis(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? ((statusReg) |= (m)) : (statusReg = statusReg)); } //Bit clear if set, do nothing if not set -void bit_cis(unsigned int c,unsigned int m) +void netDevStatus::bit_cis(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? ((statusReg) &= ~(m)) : (statusReg = statusReg)); } //Bit set if set, clear if not set -void bit_sisc(unsigned int c,unsigned int m) +void netDevStatus::bit_sisc(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? ((statusReg) |= (m)) : ((statusReg) &= ~(m))); } //Bit clear if set, set if not set -void bit_ciss(unsigned int c,unsigned int m) +void netDevStatus::bit_ciss(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? ((statusReg) &= ~(m)) : ((statusReg) |= (m))); } //Bit set if clear, do nothing if set -void bit_sic(unsigned int c,unsigned int m) +void netDevStatus::bit_sic(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? (statusReg = statusReg) : ((statusReg) |= (m))); } //Bit clear if clear, do nothing if set -void bit_cic(unsigned int c,unsigned int m) +void netDevStatus::bit_cic(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? (statusReg = statusReg) : ((statusReg) &= ~(m))); } //Bit set if clear, clear if set -void bit_sicc(unsigned int c,unsigned int m) +void netDevStatus::bit_sicc(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? ((statusReg) &= ~(m)) : ((statusReg) |= (m))); } //Bit clear if clear, set if set -void bit_cics(unsigned int c,unsigned int m) +void netDevStatus::bit_cics(unsigned int c,unsigned int m) { (((statusReg) & (c)) ? ((statusReg) |= (m)) : ((statusReg) &= ~(m))); } \ No newline at end of file
--- a/netStatusReg.h Mon Oct 07 12:44:38 2019 +0000 +++ b/netStatusReg.h Mon Oct 07 17:32:01 2019 +0000 @@ -1,28 +1,66 @@ #ifndef MKNETATATREG_H #define MKNETATATREG_H -unsigned int getStatus(void); - -bool bit_isset(unsigned int); -bool bit_isclear(unsigned int); -void bit_toset(unsigned int); -void bit_toclear(unsigned int); - -//Bit set if set, do nothing if not set -void bit_sis(unsigned int,unsigned int); -//Bit clear if set, do nothing if not set -void bit_cis(unsigned int,unsigned int); -//Bit set if set, clear if not set -void bit_sisc(unsigned int,unsigned int); -//Bit clear if set, set if not set -void bit_ciss(unsigned int,unsigned int); - -//Bit set if clear, do nothing if set -void bit_sic(unsigned int,unsigned int); -//Bit clear if clear, do nothing if set -void bit_cic(unsigned int,unsigned int); -//Bit set if clear, clear if set -void bit_sicc(unsigned int,unsigned int); -//Bit clear if clear, set if set -void bit_cics(unsigned int,unsigned int); +class netDevStatus +{ + unsigned int statusReg; + + unsigned int txMessageCount; //1 + unsigned int rxMessageCount; //2 + unsigned int fmTimeoutCount; //3 + unsigned int serTimeoutCount; //4 + + char devMsgReq; + char devMsgPos; + + public: + + netDevStatus(unsigned int,unsigned int,unsigned int,unsigned int,unsigned int); + + unsigned int getStatus(void); + + //Get count Value + unsigned int getCount(char); + + //Increment count Value + void incCount(char); + + //Set count Value + void setCount(char, unsigned int); + + void setDevMsgReq(char); + + char getDevMsgReq(void); + + void setDevMsgPos(char); + + char getDevMsgPos(void); + + //Return true if bit is set + bool bit_isset(unsigned int); + //Return true if bit is clear + bool bit_isclear(unsigned int); + //Set bit + void bit_toset(unsigned int); + //Clear bit + void bit_toclear(unsigned int); + + //Bit set if set, do nothing if not set + void bit_sis(unsigned int,unsigned int); + //Bit clear if set, do nothing if not set + void bit_cis(unsigned int,unsigned int); + //Bit set if set, clear if not set + void bit_sisc(unsigned int,unsigned int); + //Bit clear if set, set if not set + void bit_ciss(unsigned int,unsigned int); + + //Bit set if clear, do nothing if set + void bit_sic(unsigned int,unsigned int); + //Bit clear if clear, do nothing if set + void bit_cic(unsigned int,unsigned int); + //Bit set if clear, clear if set + void bit_sicc(unsigned int,unsigned int); + //Bit clear if clear, set if set + void bit_cics(unsigned int,unsigned int); +}; #endif \ No newline at end of file
--- a/netdevices.h Mon Oct 07 12:44:38 2019 +0000 +++ b/netdevices.h Mon Oct 07 17:32:01 2019 +0000 @@ -54,32 +54,23 @@ TCPSocket srv_sock; unsigned int cltPort; char* srv_addr; - + /* unsigned int fmTimeoutCount; - unsigned int serTimeoutCount; + unsigned int serTimeoutCount;*/ unsigned int sendState; - //volatile unsigned int statusReg; - - unsigned int deivceType; - unsigned int serMsgTimeout; - + /* char devMsgReq; char devMsgPos; + */ unsigned int sendRetryCount; unsigned int messageFailCount; + /* unsigned int txMessageCount; - unsigned int rxMessageCount; - unsigned int deviceConfig; - - unsigned int fmpolltimer; - unsigned int fmdpolltimer; - unsigned int fmcfgpolltimer; - unsigned int fmtimeout; - + unsigned int rxMessageCount;*/ char sendString[256]; char sendLen;