|
12 | 12 | var txMsgStart = false; |
13 | 13 | var rxMsgStart = false; |
14 | 14 | var radioRunning = false; |
15 | | - var rxMsgBufSize = 255; |
| 15 | + var rxMsgBufSize = 1024; |
| 16 | + var rxMsgBufThreshold = 768; |
16 | 17 | var rxMsgBuffer = Buffer.alloc(rxMsgBufSize); |
17 | 18 | var rxMsgOffset = 0; |
18 | 19 | var messageBitRate = 1200; |
|
111 | 112 | if (rxMsgPipe == null) { |
112 | 113 | this._errorMessage("Radio Not Running"); |
113 | 114 | callback(""); |
| 115 | + } else if (rxMsgOffset >= rxMsgBufThreshold) { |
| 116 | + this._processRxMessage(callback); |
114 | 117 | } else { |
115 | 118 | fs.read(rxMsgPipe, rxMsgBuffer, rxMsgOffset, rxMsgBufSize-rxMsgOffset, null, |
116 | 119 | function(err, len, buf) { |
117 | 120 | if (err == null) { |
118 | 121 | if (len == 0) { |
119 | | - this._receiveMessage(callback); |
| 122 | + setTimeout(this._receiveMessage, 1000, callback); |
120 | 123 | } else { |
121 | | - var rxMsgString; |
122 | 124 | rxMsgOffset += len; |
123 | | - for (var i = 0; i < rxMsgOffset; i++) { |
124 | | - |
125 | | - // On detecting an end of line character, copy |
126 | | - // the line to the received message string and |
127 | | - // shift the residual buffer contents down. |
128 | | - if (buf[i] == 10) { |
129 | | - rxMsgString = buf.toString('ascii', 0, i); |
130 | | - if (i == rxMsgOffset-1) { |
131 | | - rxMsgOffset = 0; |
132 | | - } else { |
133 | | - buf.copy(buf, 0, i+1, rxMsgOffset-i-1); |
134 | | - rxMsgOffset -= i+1; |
135 | | - } |
136 | | - break; |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - // Invoke callback or retry. |
141 | | - if (rxMsgString == null) { |
142 | | - if (rxMsgOffset >= rxMsgBufSize) { |
143 | | - rxMsgOffset -= 1; |
144 | | - } |
145 | | - this._receiveMessage(callback); |
146 | | - } else { |
147 | | - callback(rxMsgString); |
148 | | - } |
| 125 | + this._processRxMessage(callback); |
149 | 126 | } |
150 | 127 | } else if (err.code == "EAGAIN") { |
151 | | - this._receiveMessage(callback); |
| 128 | + if (rxMsgOffset > 0) { |
| 129 | + this._processRxMessage(callback); |
| 130 | + } else { |
| 131 | + setTimeout(this._receiveMessage, 1000, callback); |
| 132 | + } |
152 | 133 | } else { |
153 | 134 | this._errorMessage("Rx Message Error : " + err.code); |
154 | 135 | callback(""); |
|
158 | 139 | } |
159 | 140 | } |
160 | 141 |
|
| 142 | + // Process a received message. |
| 143 | + this._processRxMessage = function(callback) { |
| 144 | + var rxMsgString; |
| 145 | + for (var i = 0; i < rxMsgOffset; i++) { |
| 146 | + |
| 147 | + // On detecting an end of line character, copy the line to |
| 148 | + // the received message string and shift the residual buffer |
| 149 | + // contents down. |
| 150 | + if (rxMsgBuffer[i] == 10) { |
| 151 | + rxMsgString = rxMsgBuffer.toString('ascii', 0, i); |
| 152 | + if (i == rxMsgOffset-1) { |
| 153 | + rxMsgOffset = 0; |
| 154 | + } else { |
| 155 | + rxMsgBuffer.copy(rxMsgBuffer, 0, i+1, rxMsgOffset); |
| 156 | + rxMsgOffset -= i+1; |
| 157 | + } |
| 158 | + break; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Invoke callback or retry. |
| 163 | + if (rxMsgString == null) { |
| 164 | + if (rxMsgOffset >= rxMsgBufSize) { |
| 165 | + rxMsgOffset -= 1; |
| 166 | + } |
| 167 | + setTimeout(this._receiveMessage, 1000, callback); |
| 168 | + } else { |
| 169 | + callback(rxMsgString); |
| 170 | + } |
| 171 | + } |
| 172 | + |
161 | 173 | // Cleanup function when the extension is unloaded. |
162 | 174 | ext._shutdown = function() { |
163 | 175 | if (commandPipe != null) { |
|
226 | 238 | txMsgPipe = null; |
227 | 239 | } |
228 | 240 | if (rxMsgPipe != null) { |
| 241 | + // TODO: This discards the local message buffer but we don't |
| 242 | + // currently discard any residual input FIFO file contents. |
| 243 | + rxMsgOffset = 0; |
229 | 244 | fs.closeSync(rxMsgPipe); |
230 | 245 | rxMsgPipe = null; |
231 | 246 | } |
|
0 commit comments