20
20
21
21
See file LICENSE.txt for further informations on licensing terms.
22
22
23
- Last updated by Jeff Hoefs: August 4 , 2015
23
+ Last updated by Jeff Hoefs: August 16 , 2015
24
24
*/
25
25
26
26
#include < Servo.h>
@@ -98,6 +98,7 @@ Stream *swSerial2 = NULL;
98
98
Stream *swSerial3 = NULL ;
99
99
100
100
byte reportSerial[MAX_SERIAL_PORTS];
101
+ int serialBytesToRead[12 ];
101
102
signed char serialIndex = -1 ;
102
103
103
104
/* i2c data */
@@ -203,13 +204,16 @@ Stream* getPortFromId(byte portId)
203
204
void checkSerial ()
204
205
{
205
206
byte portId, serialData;
207
+ int bytesToRead = 0 ;
208
+ int numBytesToRead = 0 ;
206
209
Stream* serialPort;
207
210
208
211
if (serialIndex > -1 ) {
209
212
210
213
// loop through all reporting (READ_CONTINUOUS) serial ports
211
214
for (byte i = 0 ; i < serialIndex + 1 ; i++) {
212
215
portId = reportSerial[i];
216
+ bytesToRead = serialBytesToRead[portId];
213
217
serialPort = getPortFromId (portId);
214
218
if (serialPort == NULL ) {
215
219
continue ;
@@ -224,11 +228,19 @@ void checkSerial()
224
228
Firmata.write (START_SYSEX);
225
229
Firmata.write (SERIAL_MESSAGE);
226
230
Firmata.write (SERIAL_REPLY | portId);
231
+
232
+ if (bytesToRead == 0 || (serialPort->available () <= bytesToRead)) {
233
+ numBytesToRead = serialPort->available ();
234
+ } else {
235
+ numBytesToRead = bytesToRead;
236
+ }
237
+
227
238
// relay serial data to the serial device
228
- while (serialPort-> available () > 0 ) {
239
+ while (numBytesToRead > 0 ) {
229
240
serialData = serialPort->read ();
230
241
Firmata.write (serialData & 0x7F );
231
242
Firmata.write ((serialData >> 7 ) & 0x7F );
243
+ numBytesToRead--;
232
244
}
233
245
Firmata.write (END_SYSEX);
234
246
}
@@ -725,9 +737,10 @@ void sysexCallback(byte command, byte argc, byte *argv)
725
737
case SERIAL_CONFIG:
726
738
{
727
739
long baud = (long )argv[1 ] | ((long )argv[2 ] << 7 ) | ((long )argv[3 ] << 14 );
728
- int bytesToRead = (int )argv[4 ] | ((int )argv[5 ] << 7 ); // not yet used
729
740
byte txPin, rxPin;
730
741
742
+ serialBytesToRead[portId] = (int )argv[4 ] | ((int )argv[5 ] << 7 );
743
+
731
744
if (portId > 7 && argc > 6 ) {
732
745
rxPin = argv[6 ];
733
746
txPin = argv[7 ];
0 commit comments