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@19:cae910c9c06a, 2020-01-23 (annotated)
- Committer:
- masterkookus
- Date:
- Thu Jan 23 12:49:02 2020 +0000
- Revision:
- 19:cae910c9c06a
- Parent:
- 18:69846c990e04
Committed library URL publish changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masterkookus | 0:03ab7f7596e2 | 1 | #if !FEATURE_LWIP |
masterkookus | 0:03ab7f7596e2 | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
masterkookus | 0:03ab7f7596e2 | 3 | #endif |
masterkookus | 0:03ab7f7596e2 | 4 | |
masterkookus | 17:a69f6e51b3cb | 5 | //#define netmsgdebug |
masterkookus | 11:d40adc7de05f | 6 | |
masterkookus | 0:03ab7f7596e2 | 7 | #include "mbed.h" |
masterkookus | 0:03ab7f7596e2 | 8 | #include "EthernetInterface.h" |
masterkookus | 0:03ab7f7596e2 | 9 | #include "TCPServer.h" |
masterkookus | 0:03ab7f7596e2 | 10 | #include "TCPSocket.h" |
masterkookus | 0:03ab7f7596e2 | 11 | #include "BufferedSerial.h" |
masterkookus | 3:ac1f2af8bd0f | 12 | #include "nettime.h" |
masterkookus | 3:ac1f2af8bd0f | 13 | #include "netdevices.h" |
masterkookus | 7:be13a9037d41 | 14 | #include "mydevices.h" |
masterkookus | 5:c656fd08007b | 15 | #include "platform/CircularBuffer.h" |
masterkookus | 8:fa2a2c3a16ce | 16 | #include "netDataTypes.h" |
masterkookus | 8:fa2a2c3a16ce | 17 | #include "selMsg.h" |
masterkookus | 8:fa2a2c3a16ce | 18 | #include "string.h" |
masterkookus | 11:d40adc7de05f | 19 | #include "analogAverager.h" |
masterkookus | 11:d40adc7de05f | 20 | #include "analogMinMax.h" |
masterkookus | 11:d40adc7de05f | 21 | #include "counterMinMax.h" |
masterkookus | 12:4bb088c27838 | 22 | #include "netStatusReg.h" |
masterkookus | 17:a69f6e51b3cb | 23 | #include "voltageRegulator.h" |
masterkookus | 0:03ab7f7596e2 | 24 | |
masterkookus | 7:be13a9037d41 | 25 | Ticker nettimer; |
masterkookus | 14:3d437acb92e3 | 26 | Timer netDevTimers[7]; |
masterkookus | 0:03ab7f7596e2 | 27 | |
masterkookus | 6:9f97716eae76 | 28 | struct netsys net1; |
masterkookus | 15:9ad261a27883 | 29 | netDevStatus netStat(0,0,0,0,0,setConsFailCount); |
masterkookus | 13:4d533751f951 | 30 | |
masterkookus | 16:018bed9abe89 | 31 | voltageRegulator myDevice; |
masterkookus | 11:d40adc7de05f | 32 | |
masterkookus | 1:a57cbebba3fd | 33 | bool polltick; |
masterkookus | 12:4bb088c27838 | 34 | bool sendtick; |
masterkookus | 0:03ab7f7596e2 | 35 | |
masterkookus | 18:69846c990e04 | 36 | BufferedSerial sport0(sport0tx, sport0rx, rxtxbuffer, sport0mult); // UART2 |
masterkookus | 0:03ab7f7596e2 | 37 | |
masterkookus | 18:69846c990e04 | 38 | CircularBuffer<char, rxtxbuffer> receiveBuffer; |
masterkookus | 17:a69f6e51b3cb | 39 | CircularBuffer<char, 256> unSolReceiveBuffer; |
masterkookus | 7:be13a9037d41 | 40 | CircularBuffer<char, 64> sendLength; |
masterkookus | 12:4bb088c27838 | 41 | CircularBuffer<char, 256> sendBuffer; |
masterkookus | 5:c656fd08007b | 42 | |
masterkookus | 17:a69f6e51b3cb | 43 | Thread rxtxdatathread[4]; |
masterkookus | 4:98cdccfb17d6 | 44 | |
masterkookus | 4:98cdccfb17d6 | 45 | unsigned int currenttime=0; |
masterkookus | 0:03ab7f7596e2 | 46 | |
masterkookus | 5:c656fd08007b | 47 | nsapi_error_t ret; |
masterkookus | 5:c656fd08007b | 48 | |
masterkookus | 2:ec972966689e | 49 | //Provides the deivce poll timing |
masterkookus | 1:a57cbebba3fd | 50 | void heartbeat() |
masterkookus | 1:a57cbebba3fd | 51 | { |
masterkookus | 12:4bb088c27838 | 52 | setPollTick(true); |
masterkookus | 12:4bb088c27838 | 53 | |
masterkookus | 12:4bb088c27838 | 54 | //Provide Sender Task Tick |
masterkookus | 12:4bb088c27838 | 55 | if (netDevTimers[0].read_ms()>=100) |
masterkookus | 12:4bb088c27838 | 56 | { |
masterkookus | 12:4bb088c27838 | 57 | netDevTimers[0].reset(); |
masterkookus | 12:4bb088c27838 | 58 | setSendTick(true); |
masterkookus | 12:4bb088c27838 | 59 | } |
masterkookus | 17:a69f6e51b3cb | 60 | if (netStat.bit_isset(pollEnabled)) |
masterkookus | 12:4bb088c27838 | 61 | { |
masterkookus | 17:a69f6e51b3cb | 62 | if (netStat.bit_isset(devOnline)) |
masterkookus | 12:4bb088c27838 | 63 | { |
masterkookus | 17:a69f6e51b3cb | 64 | //Fast Demand Poll Tick |
masterkookus | 17:a69f6e51b3cb | 65 | if (netStat.bit_isset(fmdEnabled)) |
masterkookus | 12:4bb088c27838 | 66 | { |
masterkookus | 17:a69f6e51b3cb | 67 | if (netStat.bit_isset(fmdOnline)) |
masterkookus | 17:a69f6e51b3cb | 68 | { |
masterkookus | 17:a69f6e51b3cb | 69 | if (netDevTimers[2].read_ms()>=setfmdemandpollinterval) |
masterkookus | 17:a69f6e51b3cb | 70 | { |
masterkookus | 17:a69f6e51b3cb | 71 | netDevTimers[2].reset(); |
masterkookus | 17:a69f6e51b3cb | 72 | netStat.bit_toset(fmdPollReq); |
masterkookus | 17:a69f6e51b3cb | 73 | } |
masterkookus | 17:a69f6e51b3cb | 74 | } |
masterkookus | 17:a69f6e51b3cb | 75 | else |
masterkookus | 17:a69f6e51b3cb | 76 | { |
masterkookus | 17:a69f6e51b3cb | 77 | if (netDevTimers[2].read_ms()>=8000) |
masterkookus | 17:a69f6e51b3cb | 78 | { |
masterkookus | 17:a69f6e51b3cb | 79 | netDevTimers[2].reset(); |
masterkookus | 17:a69f6e51b3cb | 80 | netStat.bit_toset(fmdPollReq); |
masterkookus | 17:a69f6e51b3cb | 81 | } |
masterkookus | 17:a69f6e51b3cb | 82 | } |
masterkookus | 12:4bb088c27838 | 83 | } |
masterkookus | 17:a69f6e51b3cb | 84 | //Fast Meter Poll Tick |
masterkookus | 17:a69f6e51b3cb | 85 | if (netStat.bit_isset(fmEnabled)) |
masterkookus | 12:4bb088c27838 | 86 | { |
masterkookus | 17:a69f6e51b3cb | 87 | if (netStat.bit_isset(fmOnline)) |
masterkookus | 17:a69f6e51b3cb | 88 | { |
masterkookus | 17:a69f6e51b3cb | 89 | if (netDevTimers[3].read_ms()>=setfmpollinterval) |
masterkookus | 17:a69f6e51b3cb | 90 | { |
masterkookus | 17:a69f6e51b3cb | 91 | netDevTimers[3].reset(); |
masterkookus | 17:a69f6e51b3cb | 92 | netStat.bit_toset(fmPollReq); |
masterkookus | 17:a69f6e51b3cb | 93 | } |
masterkookus | 17:a69f6e51b3cb | 94 | } |
masterkookus | 17:a69f6e51b3cb | 95 | else |
masterkookus | 17:a69f6e51b3cb | 96 | { |
masterkookus | 17:a69f6e51b3cb | 97 | if (netDevTimers[3].read_ms()>=8000) |
masterkookus | 17:a69f6e51b3cb | 98 | { |
masterkookus | 17:a69f6e51b3cb | 99 | netDevTimers[3].reset(); |
masterkookus | 17:a69f6e51b3cb | 100 | netStat.bit_toset(fmPollReq); |
masterkookus | 17:a69f6e51b3cb | 101 | } |
masterkookus | 17:a69f6e51b3cb | 102 | } |
masterkookus | 17:a69f6e51b3cb | 103 | } |
masterkookus | 17:a69f6e51b3cb | 104 | if (netStat.bit_isset(recEnabled)) |
masterkookus | 17:a69f6e51b3cb | 105 | { |
masterkookus | 17:a69f6e51b3cb | 106 | if (netDevTimers[6].read_ms()>=setDataRecInterval) |
masterkookus | 17:a69f6e51b3cb | 107 | { |
masterkookus | 17:a69f6e51b3cb | 108 | netStat.bit_toset(recDataFile); |
masterkookus | 17:a69f6e51b3cb | 109 | netDevTimers[6].stop(); |
masterkookus | 17:a69f6e51b3cb | 110 | } |
masterkookus | 12:4bb088c27838 | 111 | } |
masterkookus | 12:4bb088c27838 | 112 | } |
masterkookus | 12:4bb088c27838 | 113 | else |
masterkookus | 12:4bb088c27838 | 114 | { |
masterkookus | 12:4bb088c27838 | 115 | if (netDevTimers[3].read_ms()>=8000) |
masterkookus | 12:4bb088c27838 | 116 | { |
masterkookus | 12:4bb088c27838 | 117 | netDevTimers[3].reset(); |
masterkookus | 17:a69f6e51b3cb | 118 | netStat.bit_toset(fmCfgReq); |
masterkookus | 12:4bb088c27838 | 119 | } |
masterkookus | 12:4bb088c27838 | 120 | } |
masterkookus | 12:4bb088c27838 | 121 | } |
masterkookus | 12:4bb088c27838 | 122 | //Fast Message Timeout |
masterkookus | 16:018bed9abe89 | 123 | if ((netStat.bit_isset(fmCfgInProg)) || (netStat.bit_isset(fmPollInProg)) || (netStat.bit_isset(fmdPollInProg))) |
masterkookus | 12:4bb088c27838 | 124 | { |
masterkookus | 12:4bb088c27838 | 125 | if (netDevTimers[4].read_ms()>=2000) |
masterkookus | 12:4bb088c27838 | 126 | { |
masterkookus | 12:4bb088c27838 | 127 | netDevTimers[4].stop(); |
masterkookus | 13:4d533751f951 | 128 | netStat.bit_toset(fmCloseConnection); |
masterkookus | 13:4d533751f951 | 129 | netStat.bit_toset(fmTimeout); |
masterkookus | 12:4bb088c27838 | 130 | } |
masterkookus | 12:4bb088c27838 | 131 | } |
masterkookus | 12:4bb088c27838 | 132 | //Serial Message Timeout |
masterkookus | 13:4d533751f951 | 133 | if (netStat.bit_isset(serPollInProg)) |
masterkookus | 12:4bb088c27838 | 134 | { |
masterkookus | 12:4bb088c27838 | 135 | if (netDevTimers[5].read_ms()>=net1.serMsgTimeout) |
masterkookus | 12:4bb088c27838 | 136 | { |
masterkookus | 12:4bb088c27838 | 137 | netDevTimers[5].stop(); |
masterkookus | 13:4d533751f951 | 138 | netStat.bit_toset(serCloseConnection); |
masterkookus | 13:4d533751f951 | 139 | netStat.bit_toset(serTimeout); |
masterkookus | 12:4bb088c27838 | 140 | } |
masterkookus | 12:4bb088c27838 | 141 | } |
masterkookus | 17:a69f6e51b3cb | 142 | |
masterkookus | 1:a57cbebba3fd | 143 | } |
masterkookus | 1:a57cbebba3fd | 144 | |
masterkookus | 13:4d533751f951 | 145 | void sendCmd(char cmdNum) |
masterkookus | 13:4d533751f951 | 146 | { |
masterkookus | 13:4d533751f951 | 147 | sendLength.push(2); |
masterkookus | 13:4d533751f951 | 148 | for (char txc=0;txc<2;txc++) |
masterkookus | 13:4d533751f951 | 149 | { |
masterkookus | 13:4d533751f951 | 150 | sendBuffer.push(fmCmd[cmdNum][txc]); |
masterkookus | 13:4d533751f951 | 151 | } |
masterkookus | 13:4d533751f951 | 152 | } |
masterkookus | 13:4d533751f951 | 153 | |
masterkookus | 13:4d533751f951 | 154 | void sendSerCmd(char cmdNum, char cmdCnt) |
masterkookus | 13:4d533751f951 | 155 | { |
masterkookus | 13:4d533751f951 | 156 | sendLength.push(cmdCnt); |
masterkookus | 13:4d533751f951 | 157 | for (char txc=0;txc<cmdCnt;txc++) |
masterkookus | 13:4d533751f951 | 158 | { |
masterkookus | 13:4d533751f951 | 159 | sendBuffer.push(serCmd[cmdNum][txc]); |
masterkookus | 13:4d533751f951 | 160 | } |
masterkookus | 13:4d533751f951 | 161 | } |
masterkookus | 13:4d533751f951 | 162 | |
masterkookus | 4:98cdccfb17d6 | 163 | void confignetdevices(EthernetInterface *eth) |
masterkookus | 4:98cdccfb17d6 | 164 | { |
masterkookus | 13:4d533751f951 | 165 | #ifdef sportEnabled |
masterkookus | 13:4d533751f951 | 166 | netStat.bit_toset(serEnabled); |
masterkookus | 13:4d533751f951 | 167 | #endif |
masterkookus | 13:4d533751f951 | 168 | |
masterkookus | 13:4d533751f951 | 169 | #ifdef setPollEnabled |
masterkookus | 13:4d533751f951 | 170 | netStat.bit_toset(pollEnabled); |
masterkookus | 13:4d533751f951 | 171 | #endif |
masterkookus | 13:4d533751f951 | 172 | |
masterkookus | 17:a69f6e51b3cb | 173 | #ifdef fastMeterEnabled |
masterkookus | 17:a69f6e51b3cb | 174 | netStat.bit_toset(fmEnabled); |
masterkookus | 17:a69f6e51b3cb | 175 | #endif |
masterkookus | 17:a69f6e51b3cb | 176 | |
masterkookus | 17:a69f6e51b3cb | 177 | #ifdef fastDemandEnabled |
masterkookus | 17:a69f6e51b3cb | 178 | netStat.bit_toset(fmdEnabled); |
masterkookus | 17:a69f6e51b3cb | 179 | #endif |
masterkookus | 17:a69f6e51b3cb | 180 | |
masterkookus | 17:a69f6e51b3cb | 181 | #ifdef recordDataEnabled |
masterkookus | 17:a69f6e51b3cb | 182 | netStat.bit_toset(recEnabled); |
masterkookus | 17:a69f6e51b3cb | 183 | #endif |
masterkookus | 17:a69f6e51b3cb | 184 | |
masterkookus | 7:be13a9037d41 | 185 | net1.cltPort=setclientport; |
masterkookus | 7:be13a9037d41 | 186 | net1.srv_addr=setclientaddress; |
masterkookus | 15:9ad261a27883 | 187 | net1.srv_sock.set_blocking(false); |
masterkookus | 8:fa2a2c3a16ce | 188 | net1.srv_sock.set_timeout(100); |
masterkookus | 8:fa2a2c3a16ce | 189 | net1.sendRetryCount=0; |
masterkookus | 8:fa2a2c3a16ce | 190 | net1.messageFailCount=0; |
masterkookus | 13:4d533751f951 | 191 | net1.serMsgTimeout=2000; |
masterkookus | 4:98cdccfb17d6 | 192 | |
masterkookus | 4:98cdccfb17d6 | 193 | /* Open the server on ethernet stack */ |
masterkookus | 4:98cdccfb17d6 | 194 | net1.srv.open(eth); |
masterkookus | 7:be13a9037d41 | 195 | sport0.baud(sport0baud); |
masterkookus | 4:98cdccfb17d6 | 196 | } |
masterkookus | 4:98cdccfb17d6 | 197 | |
masterkookus | 7:be13a9037d41 | 198 | void dataprocess(netsys *net2) |
masterkookus | 0:03ab7f7596e2 | 199 | { |
masterkookus | 5:c656fd08007b | 200 | char cchar; |
masterkookus | 18:69846c990e04 | 201 | char cbuf[rxtxbuffer]; |
masterkookus | 5:c656fd08007b | 202 | unsigned int clen=0; |
masterkookus | 5:c656fd08007b | 203 | unsigned int cbc; |
masterkookus | 5:c656fd08007b | 204 | |
masterkookus | 4:98cdccfb17d6 | 205 | while(1) |
masterkookus | 0:03ab7f7596e2 | 206 | { |
masterkookus | 10:9da863a6da3e | 207 | //Check to see if the Receive Buffer has anything |
masterkookus | 18:69846c990e04 | 208 | /*while (!receiveBuffer.empty()) |
masterkookus | 5:c656fd08007b | 209 | { |
masterkookus | 7:be13a9037d41 | 210 | receiveBuffer.pop(cchar); |
masterkookus | 8:fa2a2c3a16ce | 211 | cbuf[clen]=cchar; |
masterkookus | 5:c656fd08007b | 212 | clen++; |
masterkookus | 18:69846c990e04 | 213 | }*/ |
masterkookus | 10:9da863a6da3e | 214 | //If a Poll Request has been made and data has been received set flag |
masterkookus | 13:4d533751f951 | 215 | netStat.bit_sis(serPollInProg,serRespRx); |
masterkookus | 13:4d533751f951 | 216 | netStat.bit_sis(fmPollInProg,fmRespRx); |
masterkookus | 13:4d533751f951 | 217 | netStat.bit_sis(fmdPollInProg,fmdRespRx); |
masterkookus | 12:4bb088c27838 | 218 | |
masterkookus | 10:9da863a6da3e | 219 | //Check to see if bulk data and reports being sent from device due to serial command |
masterkookus | 13:4d533751f951 | 220 | if (netStat.bit_isset(serMsgRx)) |
masterkookus | 9:d6e7981dfc89 | 221 | { |
masterkookus | 18:69846c990e04 | 222 | while (!receiveBuffer.empty()) |
masterkookus | 18:69846c990e04 | 223 | { |
masterkookus | 18:69846c990e04 | 224 | receiveBuffer.pop(cchar); |
masterkookus | 18:69846c990e04 | 225 | sport0.putc(cchar); |
masterkookus | 18:69846c990e04 | 226 | //cbuf[clen]=cchar; |
masterkookus | 18:69846c990e04 | 227 | //clen++; |
masterkookus | 18:69846c990e04 | 228 | } |
masterkookus | 18:69846c990e04 | 229 | /*if (clen>0) |
masterkookus | 13:4d533751f951 | 230 | { |
masterkookus | 13:4d533751f951 | 231 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 232 | netDevTimers[5].reset(); |
masterkookus | 13:4d533751f951 | 233 | clen=0; |
masterkookus | 15:9ad261a27883 | 234 | netStat.iedOnline(true); |
masterkookus | 18:69846c990e04 | 235 | }*/ |
masterkookus | 18:69846c990e04 | 236 | } |
masterkookus | 18:69846c990e04 | 237 | else |
masterkookus | 18:69846c990e04 | 238 | { |
masterkookus | 18:69846c990e04 | 239 | while (!receiveBuffer.empty()) |
masterkookus | 18:69846c990e04 | 240 | { |
masterkookus | 18:69846c990e04 | 241 | receiveBuffer.pop(cchar); |
masterkookus | 18:69846c990e04 | 242 | cbuf[clen]=cchar; |
masterkookus | 18:69846c990e04 | 243 | clen++; |
masterkookus | 13:4d533751f951 | 244 | } |
masterkookus | 9:d6e7981dfc89 | 245 | } |
masterkookus | 10:9da863a6da3e | 246 | //Check to see if at least 4 characters have been received |
masterkookus | 18:69846c990e04 | 247 | if (clen>3) |
masterkookus | 5:c656fd08007b | 248 | { |
masterkookus | 10:9da863a6da3e | 249 | //Cycle through received data to search for a command |
masterkookus | 7:be13a9037d41 | 250 | for (cbc=0;cbc<clen-3;cbc++) |
masterkookus | 5:c656fd08007b | 251 | { |
masterkookus | 12:4bb088c27838 | 252 | //Check fast message configuration |
masterkookus | 12:4bb088c27838 | 253 | if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC0)) |
masterkookus | 12:4bb088c27838 | 254 | { |
masterkookus | 12:4bb088c27838 | 255 | sport0.write(cbuf,clen); |
masterkookus | 16:018bed9abe89 | 256 | if (myDevice.chkCfgMsg(cbuf,clen)) |
masterkookus | 16:018bed9abe89 | 257 | { |
masterkookus | 16:018bed9abe89 | 258 | //netStat.bit_toclear(fmCfgInProg); |
masterkookus | 16:018bed9abe89 | 259 | netStat.bit_toset(devOnline); |
masterkookus | 16:018bed9abe89 | 260 | clen=0; |
masterkookus | 16:018bed9abe89 | 261 | netStat.iedOnline(true); |
masterkookus | 16:018bed9abe89 | 262 | } |
masterkookus | 12:4bb088c27838 | 263 | break; |
masterkookus | 12:4bb088c27838 | 264 | } |
masterkookus | 10:9da863a6da3e | 265 | //Check to see if serial commands are currently being received |
masterkookus | 13:4d533751f951 | 266 | if (netStat.bit_isset(serPollInProg)) |
masterkookus | 8:fa2a2c3a16ce | 267 | { |
masterkookus | 10:9da863a6da3e | 268 | //If relay acknowledges max metering command set bulk receive flag |
masterkookus | 9:d6e7981dfc89 | 269 | if ((cbuf[cbc]==0x4d) && (cbuf[cbc+1]==0x45) && (cbuf[cbc+2]==0x54)) |
masterkookus | 8:fa2a2c3a16ce | 270 | { |
masterkookus | 9:d6e7981dfc89 | 271 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 272 | netStat.bit_toset(serMsgRx); |
masterkookus | 13:4d533751f951 | 273 | netDevTimers[5].reset(); |
masterkookus | 13:4d533751f951 | 274 | netStat.setDevMsgReq(0); |
masterkookus | 9:d6e7981dfc89 | 275 | clen=0; |
masterkookus | 15:9ad261a27883 | 276 | netStat.iedOnline(true); |
masterkookus | 9:d6e7981dfc89 | 277 | break; |
masterkookus | 8:fa2a2c3a16ce | 278 | } |
masterkookus | 10:9da863a6da3e | 279 | //If relay acknowledges tap report command set bulk receive flag |
masterkookus | 9:d6e7981dfc89 | 280 | if ((cbuf[cbc]==0x54) && (cbuf[cbc+1]==0x41) && (cbuf[cbc+2]==0x50)) |
masterkookus | 9:d6e7981dfc89 | 281 | { |
masterkookus | 9:d6e7981dfc89 | 282 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 283 | netStat.bit_toset(serMsgRx); |
masterkookus | 13:4d533751f951 | 284 | netDevTimers[5].reset(); |
masterkookus | 13:4d533751f951 | 285 | netStat.setDevMsgReq(0); |
masterkookus | 9:d6e7981dfc89 | 286 | clen=0; |
masterkookus | 15:9ad261a27883 | 287 | netStat.iedOnline(true); |
masterkookus | 9:d6e7981dfc89 | 288 | break; |
masterkookus | 9:d6e7981dfc89 | 289 | } |
masterkookus | 10:9da863a6da3e | 290 | //If relay acknowledges level 1 login attempt increment command pointer to password |
masterkookus | 9:d6e7981dfc89 | 291 | if ((cbuf[cbc]==0x41) && (cbuf[cbc+1]==0x43) && (cbuf[cbc+2]==0x43)) |
masterkookus | 7:be13a9037d41 | 292 | { |
masterkookus | 8:fa2a2c3a16ce | 293 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 294 | netStat.setDevMsgPos(1); |
masterkookus | 13:4d533751f951 | 295 | sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); |
masterkookus | 13:4d533751f951 | 296 | netDevTimers[5].reset(); |
masterkookus | 9:d6e7981dfc89 | 297 | clen=0; |
masterkookus | 15:9ad261a27883 | 298 | netStat.iedOnline(true); |
masterkookus | 9:d6e7981dfc89 | 299 | break; |
masterkookus | 9:d6e7981dfc89 | 300 | } |
masterkookus | 10:9da863a6da3e | 301 | //If relay sends mask character increment command pointer to send \r\n |
masterkookus | 9:d6e7981dfc89 | 302 | if (cbuf[cbc]==0x2a) |
masterkookus | 9:d6e7981dfc89 | 303 | { |
masterkookus | 9:d6e7981dfc89 | 304 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 305 | netStat.setDevMsgPos(2); |
masterkookus | 13:4d533751f951 | 306 | sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); |
masterkookus | 13:4d533751f951 | 307 | netDevTimers[5].reset(); |
masterkookus | 9:d6e7981dfc89 | 308 | clen=0; |
masterkookus | 15:9ad261a27883 | 309 | netStat.iedOnline(true); |
masterkookus | 9:d6e7981dfc89 | 310 | break; |
masterkookus | 9:d6e7981dfc89 | 311 | } |
masterkookus | 10:9da863a6da3e | 312 | //If relay is in access level 1 or higher increment command pointer to desired command |
masterkookus | 9:d6e7981dfc89 | 313 | if ((cbuf[cbc]==0x3d) && (cbuf[cbc+1]==0x3e)) |
masterkookus | 9:d6e7981dfc89 | 314 | { |
masterkookus | 9:d6e7981dfc89 | 315 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 316 | netStat.setDevMsgPos(4); |
masterkookus | 13:4d533751f951 | 317 | sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); |
masterkookus | 13:4d533751f951 | 318 | netDevTimers[5].reset(); |
masterkookus | 9:d6e7981dfc89 | 319 | clen=0; |
masterkookus | 15:9ad261a27883 | 320 | netStat.iedOnline(true); |
masterkookus | 9:d6e7981dfc89 | 321 | break; |
masterkookus | 9:d6e7981dfc89 | 322 | } |
masterkookus | 10:9da863a6da3e | 323 | //If relay in level 0 increment command pointer to access command |
masterkookus | 9:d6e7981dfc89 | 324 | if ((cbuf[cbc]==0x3d)) |
masterkookus | 9:d6e7981dfc89 | 325 | { |
masterkookus | 9:d6e7981dfc89 | 326 | sport0.write(cbuf,clen); |
masterkookus | 13:4d533751f951 | 327 | netStat.setDevMsgPos(0); |
masterkookus | 13:4d533751f951 | 328 | sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); |
masterkookus | 13:4d533751f951 | 329 | netDevTimers[5].reset(); |
masterkookus | 8:fa2a2c3a16ce | 330 | clen=0; |
masterkookus | 15:9ad261a27883 | 331 | netStat.iedOnline(true); |
masterkookus | 8:fa2a2c3a16ce | 332 | break; |
masterkookus | 7:be13a9037d41 | 333 | } |
masterkookus | 9:d6e7981dfc89 | 334 | } |
masterkookus | 12:4bb088c27838 | 335 | //if (bit_isset(net2->statusReg,fmPollInProg)) |
masterkookus | 13:4d533751f951 | 336 | if (netStat.bit_isset(fmPollInProg)) |
masterkookus | 9:d6e7981dfc89 | 337 | { |
masterkookus | 12:4bb088c27838 | 338 | //Check fast meter configuration |
masterkookus | 12:4bb088c27838 | 339 | if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC1)) |
masterkookus | 7:be13a9037d41 | 340 | { |
masterkookus | 12:4bb088c27838 | 341 | sport0.write(cbuf,clen); |
masterkookus | 16:018bed9abe89 | 342 | if (myDevice.chkMeterMsg(cbuf,clen)) |
masterkookus | 12:4bb088c27838 | 343 | { |
masterkookus | 17:a69f6e51b3cb | 344 | netStat.bit_toset(fmOnline); |
masterkookus | 12:4bb088c27838 | 345 | clen=0; |
masterkookus | 16:018bed9abe89 | 346 | netStat.iedOnline(true); |
masterkookus | 12:4bb088c27838 | 347 | break; |
masterkookus | 12:4bb088c27838 | 348 | } |
masterkookus | 8:fa2a2c3a16ce | 349 | break; |
masterkookus | 7:be13a9037d41 | 350 | } |
masterkookus | 12:4bb088c27838 | 351 | //Receive data |
masterkookus | 12:4bb088c27838 | 352 | if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xD1)) |
masterkookus | 9:d6e7981dfc89 | 353 | { |
masterkookus | 16:018bed9abe89 | 354 | myDevice.setMeterData(cbuf, cbc,clen); |
masterkookus | 12:4bb088c27838 | 355 | clen=0; |
masterkookus | 15:9ad261a27883 | 356 | netStat.iedOnline(true); |
masterkookus | 12:4bb088c27838 | 357 | break; |
masterkookus | 9:d6e7981dfc89 | 358 | } |
masterkookus | 12:4bb088c27838 | 359 | } |
masterkookus | 13:4d533751f951 | 360 | if (netStat.bit_isset(fmdPollInProg)) |
masterkookus | 12:4bb088c27838 | 361 | { |
masterkookus | 12:4bb088c27838 | 362 | //Check fast demand meter configuration |
masterkookus | 12:4bb088c27838 | 363 | if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xC2)) |
masterkookus | 9:d6e7981dfc89 | 364 | { |
masterkookus | 12:4bb088c27838 | 365 | sport0.write(cbuf,clen); |
masterkookus | 17:a69f6e51b3cb | 366 | if (myDevice.chkDemMeterMsg(cbuf,clen)) |
masterkookus | 12:4bb088c27838 | 367 | { |
masterkookus | 17:a69f6e51b3cb | 368 | netStat.bit_toset(fmdOnline); |
masterkookus | 12:4bb088c27838 | 369 | clen=0; |
masterkookus | 16:018bed9abe89 | 370 | netStat.iedOnline(true); |
masterkookus | 12:4bb088c27838 | 371 | break; |
masterkookus | 12:4bb088c27838 | 372 | } |
masterkookus | 10:9da863a6da3e | 373 | break; |
masterkookus | 10:9da863a6da3e | 374 | } |
masterkookus | 12:4bb088c27838 | 375 | //Receive demand data |
masterkookus | 12:4bb088c27838 | 376 | if ((cbuf[cbc]==0xA5) && (cbuf[cbc+1]==0xD2)) |
masterkookus | 10:9da863a6da3e | 377 | { |
masterkookus | 16:018bed9abe89 | 378 | myDevice.setDemMeterData(cbuf, cbc,clen); |
masterkookus | 15:9ad261a27883 | 379 | netStat.iedOnline(true); |
masterkookus | 12:4bb088c27838 | 380 | break; |
masterkookus | 10:9da863a6da3e | 381 | } |
masterkookus | 10:9da863a6da3e | 382 | } |
masterkookus | 5:c656fd08007b | 383 | } |
masterkookus | 7:be13a9037d41 | 384 | if (clen>0) |
masterkookus | 7:be13a9037d41 | 385 | { |
masterkookus | 11:d40adc7de05f | 386 | #ifdef netmsgdebug |
masterkookus | 7:be13a9037d41 | 387 | printf("Command not Received\r\n"); |
masterkookus | 11:d40adc7de05f | 388 | #endif |
masterkookus | 7:be13a9037d41 | 389 | clen=0; |
masterkookus | 7:be13a9037d41 | 390 | } |
masterkookus | 7:be13a9037d41 | 391 | } |
masterkookus | 7:be13a9037d41 | 392 | } |
masterkookus | 7:be13a9037d41 | 393 | } |
masterkookus | 7:be13a9037d41 | 394 | |
masterkookus | 7:be13a9037d41 | 395 | //Ethernet to Ethernet Send Data |
masterkookus | 7:be13a9037d41 | 396 | void datantx(netsys *net2) |
masterkookus | 7:be13a9037d41 | 397 | { |
masterkookus | 7:be13a9037d41 | 398 | while(1) |
masterkookus | 7:be13a9037d41 | 399 | { |
masterkookus | 13:4d533751f951 | 400 | if (netStat.bit_isset(cltIsActive)) |
masterkookus | 0:03ab7f7596e2 | 401 | { |
masterkookus | 10:9da863a6da3e | 402 | //Attempt to connect to server |
masterkookus | 7:be13a9037d41 | 403 | while (net2->sendState==3) |
masterkookus | 5:c656fd08007b | 404 | { |
masterkookus | 12:4bb088c27838 | 405 | //Attempt to connect |
masterkookus | 6:9f97716eae76 | 406 | ret = net2->srv_sock.connect(net2->srv_addr,net2->cltPort); |
masterkookus | 10:9da863a6da3e | 407 | //If connect successful proceed to send data |
masterkookus | 5:c656fd08007b | 408 | if (ret==0) |
masterkookus | 5:c656fd08007b | 409 | { |
masterkookus | 11:d40adc7de05f | 410 | #ifdef netmsgdebug |
masterkookus | 5:c656fd08007b | 411 | printf("Connected %d\r\n",ret); |
masterkookus | 11:d40adc7de05f | 412 | #endif |
masterkookus | 8:fa2a2c3a16ce | 413 | net2->sendState=4; |
masterkookus | 8:fa2a2c3a16ce | 414 | } |
masterkookus | 10:9da863a6da3e | 415 | //If connect attempt failed check to see if may already be connected |
masterkookus | 8:fa2a2c3a16ce | 416 | else if (ret==-3015) |
masterkookus | 8:fa2a2c3a16ce | 417 | { |
masterkookus | 11:d40adc7de05f | 418 | #ifdef netmsgdebug |
masterkookus | 8:fa2a2c3a16ce | 419 | printf("May already be connected, attempting send.\r\n"); |
masterkookus | 11:d40adc7de05f | 420 | #endif |
masterkookus | 7:be13a9037d41 | 421 | net2->sendState=4; |
masterkookus | 5:c656fd08007b | 422 | } |
masterkookus | 5:c656fd08007b | 423 | else |
masterkookus | 5:c656fd08007b | 424 | { |
masterkookus | 11:d40adc7de05f | 425 | #ifdef netmsgdebug |
masterkookus | 15:9ad261a27883 | 426 | printf("Communication Failed, Closing\r\n"); |
masterkookus | 11:d40adc7de05f | 427 | #endif |
masterkookus | 15:9ad261a27883 | 428 | net2->sendRetryCount = 0; |
masterkookus | 15:9ad261a27883 | 429 | netStat.iedOnline(false); |
masterkookus | 15:9ad261a27883 | 430 | netStat.bit_toset(cltCloseConnection); |
masterkookus | 15:9ad261a27883 | 431 | net2->sendState=0; |
masterkookus | 5:c656fd08007b | 432 | } |
masterkookus | 5:c656fd08007b | 433 | } |
masterkookus | 10:9da863a6da3e | 434 | //Attempt to send data |
masterkookus | 7:be13a9037d41 | 435 | while (net2->sendState==4) |
masterkookus | 5:c656fd08007b | 436 | { |
masterkookus | 16:018bed9abe89 | 437 | if (net2->sendLen>0) |
masterkookus | 5:c656fd08007b | 438 | { |
masterkookus | 16:018bed9abe89 | 439 | //Attempt to send data |
masterkookus | 16:018bed9abe89 | 440 | ret = net2->srv_sock.send(net2->sendString,net2->sendLen); |
masterkookus | 16:018bed9abe89 | 441 | //If data send successful proceed to wait for response |
masterkookus | 16:018bed9abe89 | 442 | if (ret>=0) |
masterkookus | 15:9ad261a27883 | 443 | { |
masterkookus | 16:018bed9abe89 | 444 | net2->sendLen=0; |
masterkookus | 16:018bed9abe89 | 445 | #ifdef netmsgdebug |
masterkookus | 16:018bed9abe89 | 446 | printf("Send Result %d\r\n",ret); |
masterkookus | 16:018bed9abe89 | 447 | #endif |
masterkookus | 16:018bed9abe89 | 448 | netStat.incCount(1); |
masterkookus | 16:018bed9abe89 | 449 | net2->sendState=5; |
masterkookus | 15:9ad261a27883 | 450 | } |
masterkookus | 16:018bed9abe89 | 451 | else |
masterkookus | 15:9ad261a27883 | 452 | { |
masterkookus | 16:018bed9abe89 | 453 | #ifdef netmsgdebug |
masterkookus | 16:018bed9abe89 | 454 | printf("Communication Failed, Closing\r\n"); |
masterkookus | 16:018bed9abe89 | 455 | #endif |
masterkookus | 16:018bed9abe89 | 456 | net2->sendRetryCount = 0; |
masterkookus | 16:018bed9abe89 | 457 | netStat.iedOnline(false); |
masterkookus | 16:018bed9abe89 | 458 | netStat.bit_toset(cltCloseConnection); |
masterkookus | 16:018bed9abe89 | 459 | net2->sendState=0; |
masterkookus | 16:018bed9abe89 | 460 | } |
masterkookus | 5:c656fd08007b | 461 | } |
masterkookus | 5:c656fd08007b | 462 | else |
masterkookus | 5:c656fd08007b | 463 | { |
masterkookus | 16:018bed9abe89 | 464 | net2->sendState=5; |
masterkookus | 5:c656fd08007b | 465 | } |
masterkookus | 5:c656fd08007b | 466 | } |
masterkookus | 8:fa2a2c3a16ce | 467 | } |
masterkookus | 8:fa2a2c3a16ce | 468 | } |
masterkookus | 8:fa2a2c3a16ce | 469 | } |
masterkookus | 8:fa2a2c3a16ce | 470 | |
masterkookus | 8:fa2a2c3a16ce | 471 | //Ethernet receive data and send to aux devices (serial, etc...) |
masterkookus | 8:fa2a2c3a16ce | 472 | void datancrx(netsys *net2) |
masterkookus | 8:fa2a2c3a16ce | 473 | { |
masterkookus | 8:fa2a2c3a16ce | 474 | char rxbuf[256]; |
masterkookus | 8:fa2a2c3a16ce | 475 | int rxlen=0; |
masterkookus | 8:fa2a2c3a16ce | 476 | int rxc; |
masterkookus | 8:fa2a2c3a16ce | 477 | while (1) |
masterkookus | 8:fa2a2c3a16ce | 478 | { |
masterkookus | 10:9da863a6da3e | 479 | //If the client is active check to see if data has been received |
masterkookus | 13:4d533751f951 | 480 | while (netStat.bit_isset(cltIsActive)) |
masterkookus | 8:fa2a2c3a16ce | 481 | { |
masterkookus | 10:9da863a6da3e | 482 | //Store the length of the received data |
masterkookus | 8:fa2a2c3a16ce | 483 | rxlen=net2->srv_sock.recv(rxbuf, sizeof(rxbuf)); |
masterkookus | 10:9da863a6da3e | 484 | //if there is data then push data into received buffer |
masterkookus | 8:fa2a2c3a16ce | 485 | if (rxlen>0) |
masterkookus | 5:c656fd08007b | 486 | { |
masterkookus | 8:fa2a2c3a16ce | 487 | for (rxc = 0;rxc<rxlen;rxc++) |
masterkookus | 8:fa2a2c3a16ce | 488 | { |
masterkookus | 8:fa2a2c3a16ce | 489 | receiveBuffer.push(rxbuf[rxc]); |
masterkookus | 8:fa2a2c3a16ce | 490 | } |
masterkookus | 11:d40adc7de05f | 491 | #ifdef netmsgdebug |
masterkookus | 8:fa2a2c3a16ce | 492 | printf("Client Received Data\r\n"); |
masterkookus | 11:d40adc7de05f | 493 | #endif |
masterkookus | 10:9da863a6da3e | 494 | //Increment received message counter |
masterkookus | 13:4d533751f951 | 495 | netStat.incCount(2); |
masterkookus | 5:c656fd08007b | 496 | } |
masterkookus | 0:03ab7f7596e2 | 497 | } |
masterkookus | 0:03ab7f7596e2 | 498 | } |
masterkookus | 0:03ab7f7596e2 | 499 | } |
masterkookus | 0:03ab7f7596e2 | 500 | |
masterkookus | 8:fa2a2c3a16ce | 501 | //Serial device to server |
masterkookus | 0:03ab7f7596e2 | 502 | void datasrx(netsys *net2) |
masterkookus | 0:03ab7f7596e2 | 503 | { |
masterkookus | 0:03ab7f7596e2 | 504 | char rxbuf[256]; |
masterkookus | 0:03ab7f7596e2 | 505 | int rxlen=0; |
masterkookus | 9:d6e7981dfc89 | 506 | int rxindex=0; |
masterkookus | 0:03ab7f7596e2 | 507 | int rxc; |
masterkookus | 9:d6e7981dfc89 | 508 | bool crRx = false; |
masterkookus | 9:d6e7981dfc89 | 509 | int4byte cmdpack; |
masterkookus | 9:d6e7981dfc89 | 510 | |
masterkookus | 0:03ab7f7596e2 | 511 | while (1) |
masterkookus | 0:03ab7f7596e2 | 512 | { |
masterkookus | 10:9da863a6da3e | 513 | //Check to see if there is any data received by serial port and store length |
masterkookus | 9:d6e7981dfc89 | 514 | rxlen=sport0.readable(); |
masterkookus | 10:9da863a6da3e | 515 | //If data is ready push into receive buffer |
masterkookus | 9:d6e7981dfc89 | 516 | if (rxlen>0) |
masterkookus | 0:03ab7f7596e2 | 517 | { |
masterkookus | 9:d6e7981dfc89 | 518 | for (rxc=0;rxc<rxlen;rxc++) |
masterkookus | 9:d6e7981dfc89 | 519 | { |
masterkookus | 18:69846c990e04 | 520 | #ifdef netmsgdebug |
masterkookus | 18:69846c990e04 | 521 | printf("Serial Data Received\r\n"); |
masterkookus | 18:69846c990e04 | 522 | #endif |
masterkookus | 9:d6e7981dfc89 | 523 | rxbuf[rxindex+rxc]=sport0.getc(); |
masterkookus | 10:9da863a6da3e | 524 | //Check to see if a carriage return has been detected |
masterkookus | 9:d6e7981dfc89 | 525 | if (rxbuf[rxindex+rxc]=='\r') |
masterkookus | 9:d6e7981dfc89 | 526 | { |
masterkookus | 9:d6e7981dfc89 | 527 | crRx=true; |
masterkookus | 9:d6e7981dfc89 | 528 | } |
masterkookus | 9:d6e7981dfc89 | 529 | } |
masterkookus | 10:9da863a6da3e | 530 | //Store the current cursor location in the buffer |
masterkookus | 9:d6e7981dfc89 | 531 | rxindex=rxindex+rxlen; |
masterkookus | 10:9da863a6da3e | 532 | //Set the received length to zero |
masterkookus | 9:d6e7981dfc89 | 533 | rxlen=0; |
masterkookus | 10:9da863a6da3e | 534 | //If there is data in the buffer and a carriage return has been detected search for a command |
masterkookus | 9:d6e7981dfc89 | 535 | if ((rxindex>2) && (crRx==true)) |
masterkookus | 0:03ab7f7596e2 | 536 | { |
masterkookus | 10:9da863a6da3e | 537 | //Set the carriage return flag false |
masterkookus | 9:d6e7981dfc89 | 538 | crRx=false; |
masterkookus | 9:d6e7981dfc89 | 539 | for (rxc=0;rxc<=rxindex-3;rxc++) |
masterkookus | 0:03ab7f7596e2 | 540 | { |
masterkookus | 9:d6e7981dfc89 | 541 | cmdpack.bytes[3]=rxbuf[rxc]; |
masterkookus | 9:d6e7981dfc89 | 542 | cmdpack.bytes[2]=rxbuf[rxc+1]; |
masterkookus | 9:d6e7981dfc89 | 543 | cmdpack.bytes[1]=rxbuf[rxc+2]; |
masterkookus | 9:d6e7981dfc89 | 544 | cmdpack.bytes[0]=rxbuf[rxc+3]; |
masterkookus | 10:9da863a6da3e | 545 | //Search for the Min/Max command |
masterkookus | 9:d6e7981dfc89 | 546 | if (cmdpack.cmdint==0x4d45544d) |
masterkookus | 9:d6e7981dfc89 | 547 | { |
masterkookus | 11:d40adc7de05f | 548 | #ifdef netmsgdebug |
masterkookus | 9:d6e7981dfc89 | 549 | printf("MIN MAX METERING\r\n"); |
masterkookus | 11:d40adc7de05f | 550 | #endif |
masterkookus | 13:4d533751f951 | 551 | netStat.setDevMsgReq(0); |
masterkookus | 13:4d533751f951 | 552 | netStat.setDevMsgPos(2); |
masterkookus | 13:4d533751f951 | 553 | netStat.bit_toset(serPollReq); |
masterkookus | 9:d6e7981dfc89 | 554 | rxindex=0; |
masterkookus | 9:d6e7981dfc89 | 555 | } |
masterkookus | 10:9da863a6da3e | 556 | //Search for the Tap Report Command |
masterkookus | 9:d6e7981dfc89 | 557 | if (cmdpack.cmdint==0x54415052) |
masterkookus | 9:d6e7981dfc89 | 558 | { |
masterkookus | 11:d40adc7de05f | 559 | #ifdef netmsgdebug |
masterkookus | 9:d6e7981dfc89 | 560 | printf("TAP REPORT\r\n"); |
masterkookus | 11:d40adc7de05f | 561 | #endif |
masterkookus | 13:4d533751f951 | 562 | netStat.setDevMsgReq(1); |
masterkookus | 13:4d533751f951 | 563 | netStat.setDevMsgPos(2); |
masterkookus | 13:4d533751f951 | 564 | netStat.bit_toset(serPollReq); |
masterkookus | 13:4d533751f951 | 565 | rxindex=0; |
masterkookus | 13:4d533751f951 | 566 | } |
masterkookus | 17:a69f6e51b3cb | 567 | //Search for the Status Command |
masterkookus | 13:4d533751f951 | 568 | if (cmdpack.cmdint==0x53544154) |
masterkookus | 13:4d533751f951 | 569 | { |
masterkookus | 13:4d533751f951 | 570 | #ifdef netmsgdebug |
masterkookus | 13:4d533751f951 | 571 | printf("STATS\r\n"); |
masterkookus | 13:4d533751f951 | 572 | #endif |
masterkookus | 15:9ad261a27883 | 573 | sport0.printf("\r\nStatus %08x\r\n",netStat.getStatus()); |
masterkookus | 15:9ad261a27883 | 574 | sport0.printf("Messages Sent: %d\r\n",netStat.getCount(1)); |
masterkookus | 15:9ad261a27883 | 575 | sport0.printf("Messages Received: %d\r\n",netStat.getCount(2)); |
masterkookus | 15:9ad261a27883 | 576 | sport0.printf("Fast Message Timeouts: %d\r\n",netStat.getCount(3)); |
masterkookus | 15:9ad261a27883 | 577 | sport0.printf("Serial Timeouts: %d\r\n",netStat.getCount(4)); |
masterkookus | 15:9ad261a27883 | 578 | sport0.printf("Messages Failed: %d\r\n\r\n",netStat.getCount(5)); |
masterkookus | 9:d6e7981dfc89 | 579 | rxindex=0; |
masterkookus | 9:d6e7981dfc89 | 580 | } |
masterkookus | 17:a69f6e51b3cb | 581 | //Search for the Target Command |
masterkookus | 17:a69f6e51b3cb | 582 | if (cmdpack.cmdint==0x54415247) |
masterkookus | 17:a69f6e51b3cb | 583 | { |
masterkookus | 17:a69f6e51b3cb | 584 | char *dTargets; |
masterkookus | 17:a69f6e51b3cb | 585 | #ifdef netmsgdebug |
masterkookus | 17:a69f6e51b3cb | 586 | printf("Targets\r\n"); |
masterkookus | 17:a69f6e51b3cb | 587 | #endif |
masterkookus | 17:a69f6e51b3cb | 588 | sport0.printf("\r\nTarget 0 %08x\r\n",dTargets[0]); |
masterkookus | 17:a69f6e51b3cb | 589 | sport0.printf("\r\nTarget 1 %08x\r\n",dTargets[1]); |
masterkookus | 17:a69f6e51b3cb | 590 | sport0.printf("\r\nTarget 2 %08x\r\n",dTargets[2]); |
masterkookus | 17:a69f6e51b3cb | 591 | sport0.printf("\r\nTarget 3 %08x\r\n",dTargets[3]); |
masterkookus | 17:a69f6e51b3cb | 592 | sport0.printf("\r\nTarget 4 %08x\r\n",dTargets[4]); |
masterkookus | 17:a69f6e51b3cb | 593 | sport0.printf("\r\nTarget 5 %08x\r\n",dTargets[5]); |
masterkookus | 17:a69f6e51b3cb | 594 | sport0.printf("\r\nTarget 6 %08x\r\n",dTargets[6]); |
masterkookus | 17:a69f6e51b3cb | 595 | sport0.printf("\r\nTarget 7 %08x\r\n",dTargets[7]); |
masterkookus | 17:a69f6e51b3cb | 596 | sport0.printf("\r\nTarget 8 %08x\r\n",dTargets[8]); |
masterkookus | 17:a69f6e51b3cb | 597 | sport0.printf("\r\nTarget 9 %08x\r\n",dTargets[9]); |
masterkookus | 17:a69f6e51b3cb | 598 | rxindex=0; |
masterkookus | 17:a69f6e51b3cb | 599 | } |
masterkookus | 0:03ab7f7596e2 | 600 | } |
masterkookus | 10:9da863a6da3e | 601 | //If no command found set cursor location to zero |
masterkookus | 9:d6e7981dfc89 | 602 | if (rxindex>0) |
masterkookus | 9:d6e7981dfc89 | 603 | { |
masterkookus | 11:d40adc7de05f | 604 | #ifdef netmsgdebug |
masterkookus | 9:d6e7981dfc89 | 605 | printf("Serial Command Not Found"); |
masterkookus | 11:d40adc7de05f | 606 | #endif |
masterkookus | 9:d6e7981dfc89 | 607 | rxindex=0; |
masterkookus | 9:d6e7981dfc89 | 608 | } |
masterkookus | 0:03ab7f7596e2 | 609 | } |
masterkookus | 0:03ab7f7596e2 | 610 | } |
masterkookus | 0:03ab7f7596e2 | 611 | } |
masterkookus | 0:03ab7f7596e2 | 612 | } |
masterkookus | 0:03ab7f7596e2 | 613 | |
masterkookus | 0:03ab7f7596e2 | 614 | int main() |
masterkookus | 0:03ab7f7596e2 | 615 | { |
masterkookus | 0:03ab7f7596e2 | 616 | EthernetInterface eth; |
masterkookus | 7:be13a9037d41 | 617 | eth.set_network(setseveraddress,setsevermask,setsevergateway); //Use these parameters for static IP |
masterkookus | 0:03ab7f7596e2 | 618 | eth.connect(); |
masterkookus | 0:03ab7f7596e2 | 619 | |
masterkookus | 16:018bed9abe89 | 620 | //initVoltageRegulator(&vReg1); |
masterkookus | 8:fa2a2c3a16ce | 621 | |
masterkookus | 11:d40adc7de05f | 622 | #ifdef netmsgdebug |
masterkookus | 0:03ab7f7596e2 | 623 | printf("The target IP address is '%s'\r\n", eth.get_ip_address()); |
masterkookus | 11:d40adc7de05f | 624 | #endif |
masterkookus | 4:98cdccfb17d6 | 625 | confignetdevices(ð); |
masterkookus | 2:ec972966689e | 626 | /* Setup polltick Ticker */ |
masterkookus | 7:be13a9037d41 | 627 | nettimer.attach_us(heartbeat,10000); |
masterkookus | 2:ec972966689e | 628 | /* Setup Ethernet to Serial transmit data Thread */ |
masterkookus | 17:a69f6e51b3cb | 629 | rxtxdatathread[0].start(callback(datancrx,&net1)); |
masterkookus | 2:ec972966689e | 630 | /* Setup Ethernet to Serial receive data Thread */ |
masterkookus | 17:a69f6e51b3cb | 631 | rxtxdatathread[1].start(callback(datasrx,&net1)); |
masterkookus | 17:a69f6e51b3cb | 632 | rxtxdatathread[2].start(callback(datantx,&net1)); |
masterkookus | 17:a69f6e51b3cb | 633 | rxtxdatathread[3].start(callback(dataprocess,&net1)); |
masterkookus | 12:4bb088c27838 | 634 | |
masterkookus | 12:4bb088c27838 | 635 | unsigned int sxc; |
masterkookus | 7:be13a9037d41 | 636 | |
masterkookus | 12:4bb088c27838 | 637 | netDevTimers[0].start(); |
masterkookus | 12:4bb088c27838 | 638 | netDevTimers[2].start(); |
masterkookus | 12:4bb088c27838 | 639 | netDevTimers[3].start(); |
masterkookus | 14:3d437acb92e3 | 640 | netDevTimers[6].start(); |
masterkookus | 3:ac1f2af8bd0f | 641 | |
masterkookus | 0:03ab7f7596e2 | 642 | while (true) { |
masterkookus | 3:ac1f2af8bd0f | 643 | |
masterkookus | 12:4bb088c27838 | 644 | polltick=getPollTick(); |
masterkookus | 3:ac1f2af8bd0f | 645 | |
masterkookus | 3:ac1f2af8bd0f | 646 | if (polltick) |
masterkookus | 3:ac1f2af8bd0f | 647 | { |
masterkookus | 12:4bb088c27838 | 648 | setPollTick(false); |
masterkookus | 13:4d533751f951 | 649 | if (netStat.bit_isset(serCloseConnection)) |
masterkookus | 12:4bb088c27838 | 650 | { |
masterkookus | 12:4bb088c27838 | 651 | #ifdef netmsgdebug |
masterkookus | 12:4bb088c27838 | 652 | printf("Serial Connection Closed\r\n"); |
masterkookus | 12:4bb088c27838 | 653 | #endif |
masterkookus | 13:4d533751f951 | 654 | if (netStat.bit_isset(serRespRx)) |
masterkookus | 12:4bb088c27838 | 655 | { |
masterkookus | 13:4d533751f951 | 656 | netStat.incCount(2); |
masterkookus | 12:4bb088c27838 | 657 | } |
masterkookus | 12:4bb088c27838 | 658 | else |
masterkookus | 12:4bb088c27838 | 659 | { |
masterkookus | 13:4d533751f951 | 660 | netStat.incCount(4); |
masterkookus | 12:4bb088c27838 | 661 | } |
masterkookus | 13:4d533751f951 | 662 | netStat.bit_toclear(serclose); |
masterkookus | 13:4d533751f951 | 663 | netStat.bit_toset(cltCloseConnection); |
masterkookus | 12:4bb088c27838 | 664 | } |
masterkookus | 13:4d533751f951 | 665 | if (netStat.bit_isset(fmCloseConnection)) |
masterkookus | 4:98cdccfb17d6 | 666 | { |
masterkookus | 12:4bb088c27838 | 667 | #ifdef netmsgdebug |
masterkookus | 12:4bb088c27838 | 668 | printf("Fast Meter Connection Closed\r\n"); |
masterkookus | 12:4bb088c27838 | 669 | #endif |
masterkookus | 13:4d533751f951 | 670 | if ((netStat.bit_isset(fmRespRx)) || (netStat.bit_isset(fmdRespRx))) |
masterkookus | 12:4bb088c27838 | 671 | { |
masterkookus | 13:4d533751f951 | 672 | netStat.incCount(2); |
masterkookus | 12:4bb088c27838 | 673 | } |
masterkookus | 12:4bb088c27838 | 674 | else |
masterkookus | 7:be13a9037d41 | 675 | { |
masterkookus | 13:4d533751f951 | 676 | netStat.incCount(3); |
masterkookus | 12:4bb088c27838 | 677 | } |
masterkookus | 13:4d533751f951 | 678 | netStat.bit_toclear(fmclose); |
masterkookus | 13:4d533751f951 | 679 | netStat.bit_toset(cltCloseConnection); |
masterkookus | 12:4bb088c27838 | 680 | } |
masterkookus | 13:4d533751f951 | 681 | if (netStat.bit_isset(cltCloseConnection)) |
masterkookus | 12:4bb088c27838 | 682 | { |
masterkookus | 13:4d533751f951 | 683 | sport0.printf("Status %08x\r\n",netStat.getStatus()); |
masterkookus | 12:4bb088c27838 | 684 | #ifdef netmsgdebug |
masterkookus | 12:4bb088c27838 | 685 | printf("Connection Closed\r\n"); |
masterkookus | 12:4bb088c27838 | 686 | #endif |
masterkookus | 12:4bb088c27838 | 687 | net1.srv_sock.close(); |
masterkookus | 12:4bb088c27838 | 688 | net1.sendState=0; |
masterkookus | 13:4d533751f951 | 689 | netStat.bit_toclear(cltclose); |
masterkookus | 12:4bb088c27838 | 690 | } |
masterkookus | 12:4bb088c27838 | 691 | |
masterkookus | 18:69846c990e04 | 692 | if ((netStat.bit_isclear(serPollInProg)) && (netStat.bit_isclear(fmPollInProg)) && (netStat.bit_isclear(fmdPollInProg))) |
masterkookus | 12:4bb088c27838 | 693 | { |
masterkookus | 14:3d437acb92e3 | 694 | if (netStat.bit_isset(recDataFile)) |
masterkookus | 14:3d437acb92e3 | 695 | { |
masterkookus | 17:a69f6e51b3cb | 696 | char dataStrLen=0; |
masterkookus | 17:a69f6e51b3cb | 697 | char dataStr[256]; |
masterkookus | 17:a69f6e51b3cb | 698 | dataStrLen=myDevice.getMeterReport(&dataStr[0]); |
masterkookus | 16:018bed9abe89 | 699 | sport0.write(dataStr,dataStrLen); |
masterkookus | 14:3d437acb92e3 | 700 | |
masterkookus | 14:3d437acb92e3 | 701 | netStat.bit_toclear(recDataFile); |
masterkookus | 14:3d437acb92e3 | 702 | netDevTimers[6].reset(); |
masterkookus | 14:3d437acb92e3 | 703 | netDevTimers[6].start(); |
masterkookus | 14:3d437acb92e3 | 704 | } |
masterkookus | 15:9ad261a27883 | 705 | if ((net1.sendState==0) || (net1.sendState==5)) |
masterkookus | 12:4bb088c27838 | 706 | { |
masterkookus | 18:69846c990e04 | 707 | if ((netStat.bit_isset(fmdPollReq)) && (netStat.bit_isset(devOnline))) |
masterkookus | 7:be13a9037d41 | 708 | { |
masterkookus | 17:a69f6e51b3cb | 709 | if (netStat.bit_isset(fmdOnline)) |
masterkookus | 15:9ad261a27883 | 710 | { |
masterkookus | 15:9ad261a27883 | 711 | printf("Demand Reading\r\n"); |
masterkookus | 15:9ad261a27883 | 712 | sendCmd(4); |
masterkookus | 15:9ad261a27883 | 713 | netStat.bit_toclear(fmdPollReq); |
masterkookus | 15:9ad261a27883 | 714 | netStat.bit_toset(fmdPollInProg); |
masterkookus | 15:9ad261a27883 | 715 | } |
masterkookus | 15:9ad261a27883 | 716 | else |
masterkookus | 15:9ad261a27883 | 717 | { |
masterkookus | 15:9ad261a27883 | 718 | printf("Demand Configuration\r\n"); |
masterkookus | 15:9ad261a27883 | 719 | sendCmd(3); |
masterkookus | 15:9ad261a27883 | 720 | netStat.bit_toclear(fmdPollReq); |
masterkookus | 15:9ad261a27883 | 721 | netStat.bit_toset(fmdPollInProg); |
masterkookus | 15:9ad261a27883 | 722 | } |
masterkookus | 15:9ad261a27883 | 723 | netDevTimers[4].reset(); |
masterkookus | 15:9ad261a27883 | 724 | netDevTimers[4].start(); |
masterkookus | 12:4bb088c27838 | 725 | } |
masterkookus | 18:69846c990e04 | 726 | else if (netStat.bit_isset(serPollReq)) |
masterkookus | 18:69846c990e04 | 727 | { |
masterkookus | 18:69846c990e04 | 728 | if (netStat.bit_isset(serEnabled)) |
masterkookus | 18:69846c990e04 | 729 | { |
masterkookus | 18:69846c990e04 | 730 | sendSerCmd(serCmdSeq[netStat.getDevMsgReq()][netStat.getDevMsgPos()],serCmdlen[netStat.getDevMsgReq()][netStat.getDevMsgPos()]); |
masterkookus | 18:69846c990e04 | 731 | netStat.bit_toclear(serPollReq); |
masterkookus | 18:69846c990e04 | 732 | netStat.bit_toset(serPollInProg); |
masterkookus | 18:69846c990e04 | 733 | netDevTimers[5].reset(); |
masterkookus | 18:69846c990e04 | 734 | netDevTimers[5].start(); |
masterkookus | 18:69846c990e04 | 735 | } |
masterkookus | 18:69846c990e04 | 736 | else |
masterkookus | 18:69846c990e04 | 737 | { |
masterkookus | 18:69846c990e04 | 738 | netStat.bit_toclear(serPollReq); |
masterkookus | 18:69846c990e04 | 739 | } |
masterkookus | 18:69846c990e04 | 740 | } |
masterkookus | 18:69846c990e04 | 741 | else if ((netStat.bit_isset(fmPollReq)) && (netStat.bit_isset(devOnline))) |
masterkookus | 12:4bb088c27838 | 742 | { |
masterkookus | 17:a69f6e51b3cb | 743 | if (netStat.bit_isset(fmOnline)) |
masterkookus | 15:9ad261a27883 | 744 | { |
masterkookus | 15:9ad261a27883 | 745 | printf("Meter Reading\r\n"); |
masterkookus | 15:9ad261a27883 | 746 | sendCmd(2); |
masterkookus | 15:9ad261a27883 | 747 | netStat.bit_toclear(fmPollReq); |
masterkookus | 15:9ad261a27883 | 748 | netStat.bit_toset(fmPollInProg); |
masterkookus | 15:9ad261a27883 | 749 | } |
masterkookus | 15:9ad261a27883 | 750 | else |
masterkookus | 15:9ad261a27883 | 751 | { |
masterkookus | 15:9ad261a27883 | 752 | printf("Meter Configuration\r\n"); |
masterkookus | 15:9ad261a27883 | 753 | sendCmd(1); |
masterkookus | 15:9ad261a27883 | 754 | netStat.bit_toclear(fmPollReq); |
masterkookus | 15:9ad261a27883 | 755 | netStat.bit_toset(fmPollInProg); |
masterkookus | 15:9ad261a27883 | 756 | } |
masterkookus | 15:9ad261a27883 | 757 | netDevTimers[4].reset(); |
masterkookus | 15:9ad261a27883 | 758 | netDevTimers[4].start(); |
masterkookus | 12:4bb088c27838 | 759 | } |
masterkookus | 12:4bb088c27838 | 760 | } |
masterkookus | 12:4bb088c27838 | 761 | } |
masterkookus | 13:4d533751f951 | 762 | if ((netStat.bit_isclear(devOnline)) && (netStat.bit_isclear(fmCfgInProg)) && (netStat.bit_isset(fmCfgReq))) |
masterkookus | 12:4bb088c27838 | 763 | { |
masterkookus | 12:4bb088c27838 | 764 | sendCmd(0); |
masterkookus | 13:4d533751f951 | 765 | netStat.bit_toclear(fmCfgReq); |
masterkookus | 13:4d533751f951 | 766 | netStat.bit_toset(fmCfgInProg); |
masterkookus | 12:4bb088c27838 | 767 | net1.sendState=1; |
masterkookus | 16:018bed9abe89 | 768 | netDevTimers[4].reset(); |
masterkookus | 16:018bed9abe89 | 769 | netDevTimers[4].start(); |
masterkookus | 12:4bb088c27838 | 770 | } |
masterkookus | 12:4bb088c27838 | 771 | } |
masterkookus | 12:4bb088c27838 | 772 | |
masterkookus | 12:4bb088c27838 | 773 | |
masterkookus | 12:4bb088c27838 | 774 | //Begin Sending Data |
masterkookus | 12:4bb088c27838 | 775 | sendtick=getSendTick(); |
masterkookus | 12:4bb088c27838 | 776 | |
masterkookus | 12:4bb088c27838 | 777 | if (sendtick) |
masterkookus | 12:4bb088c27838 | 778 | { |
masterkookus | 12:4bb088c27838 | 779 | setSendTick(false); |
masterkookus | 12:4bb088c27838 | 780 | switch (net1.sendState) |
masterkookus | 12:4bb088c27838 | 781 | { |
masterkookus | 12:4bb088c27838 | 782 | case 0: |
masterkookus | 12:4bb088c27838 | 783 | if (!sendLength.empty()) |
masterkookus | 12:4bb088c27838 | 784 | { |
masterkookus | 13:4d533751f951 | 785 | sport0.printf("Status %08x\r\n",netStat.getStatus()); |
masterkookus | 12:4bb088c27838 | 786 | sendLength.pop(net1.sendLen); |
masterkookus | 12:4bb088c27838 | 787 | for (sxc=0;sxc<net1.sendLen;sxc++) |
masterkookus | 9:d6e7981dfc89 | 788 | { |
masterkookus | 12:4bb088c27838 | 789 | sendBuffer.pop(net1.sendString[sxc]); |
masterkookus | 10:9da863a6da3e | 790 | } |
masterkookus | 12:4bb088c27838 | 791 | net1.sendState=1; |
masterkookus | 10:9da863a6da3e | 792 | } |
masterkookus | 12:4bb088c27838 | 793 | break; |
masterkookus | 12:4bb088c27838 | 794 | case 1: |
masterkookus | 12:4bb088c27838 | 795 | ret=net1.srv_sock.open(ð); |
masterkookus | 12:4bb088c27838 | 796 | #ifdef netmsgdebug |
masterkookus | 12:4bb088c27838 | 797 | printf("Socket%d\r\n",ret); |
masterkookus | 12:4bb088c27838 | 798 | #endif |
masterkookus | 12:4bb088c27838 | 799 | if (ret < 0) |
masterkookus | 12:4bb088c27838 | 800 | { |
masterkookus | 12:4bb088c27838 | 801 | if (ret==-3003) |
masterkookus | 10:9da863a6da3e | 802 | { |
masterkookus | 12:4bb088c27838 | 803 | #ifdef netmsgdebug |
masterkookus | 12:4bb088c27838 | 804 | printf("May already be attached, attempting connect.\r\n"); |
masterkookus | 12:4bb088c27838 | 805 | #endif |
masterkookus | 10:9da863a6da3e | 806 | } |
masterkookus | 10:9da863a6da3e | 807 | else |
masterkookus | 10:9da863a6da3e | 808 | { |
masterkookus | 15:9ad261a27883 | 809 | #ifdef netmsgdebug |
masterkookus | 15:9ad261a27883 | 810 | printf("Communication Failed, Closing\r\n"); |
masterkookus | 15:9ad261a27883 | 811 | #endif |
masterkookus | 15:9ad261a27883 | 812 | net1.sendRetryCount = 0; |
masterkookus | 15:9ad261a27883 | 813 | netStat.iedOnline(false); |
masterkookus | 15:9ad261a27883 | 814 | net1.sendState=0; |
masterkookus | 12:4bb088c27838 | 815 | break; |
masterkookus | 10:9da863a6da3e | 816 | } |
masterkookus | 10:9da863a6da3e | 817 | } |
masterkookus | 12:4bb088c27838 | 818 | net1.sendState=3; |
masterkookus | 13:4d533751f951 | 819 | netStat.bit_toset(cltIsActive); |
masterkookus | 12:4bb088c27838 | 820 | break; |
masterkookus | 12:4bb088c27838 | 821 | case 5: |
masterkookus | 12:4bb088c27838 | 822 | if (!sendLength.empty()) |
masterkookus | 12:4bb088c27838 | 823 | { |
masterkookus | 13:4d533751f951 | 824 | sport0.printf("Status %08x\r\n",netStat.getStatus()); |
masterkookus | 12:4bb088c27838 | 825 | sendLength.pop(net1.sendLen); |
masterkookus | 12:4bb088c27838 | 826 | for (sxc=0;sxc<net1.sendLen;sxc++) |
masterkookus | 5:c656fd08007b | 827 | { |
masterkookus | 12:4bb088c27838 | 828 | sendBuffer.pop(net1.sendString[sxc]); |
masterkookus | 5:c656fd08007b | 829 | } |
masterkookus | 12:4bb088c27838 | 830 | net1.sendState=4; |
masterkookus | 12:4bb088c27838 | 831 | } |
masterkookus | 12:4bb088c27838 | 832 | break; |
masterkookus | 12:4bb088c27838 | 833 | default: |
masterkookus | 12:4bb088c27838 | 834 | break; |
masterkookus | 4:98cdccfb17d6 | 835 | } |
masterkookus | 1:a57cbebba3fd | 836 | } |
masterkookus | 0:03ab7f7596e2 | 837 | } |
masterkookus | 0:03ab7f7596e2 | 838 | } |