Skip to content

Commit 6c11ace

Browse files
implement bytesToRead from Serial protocol
1 parent 245d106 commit 6c11ace

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated by Jeff Hoefs: August 4, 2015
23+
Last updated by Jeff Hoefs: August 16, 2015
2424
*/
2525

2626
#include <Servo.h>
@@ -98,6 +98,7 @@ Stream *swSerial2 = NULL;
9898
Stream *swSerial3 = NULL;
9999

100100
byte reportSerial[MAX_SERIAL_PORTS];
101+
int serialBytesToRead[12];
101102
signed char serialIndex = -1;
102103

103104
/* i2c data */
@@ -203,13 +204,16 @@ Stream* getPortFromId(byte portId)
203204
void checkSerial()
204205
{
205206
byte portId, serialData;
207+
int bytesToRead = 0;
208+
int numBytesToRead = 0;
206209
Stream* serialPort;
207210

208211
if (serialIndex > -1) {
209212

210213
// loop through all reporting (READ_CONTINUOUS) serial ports
211214
for (byte i = 0; i < serialIndex + 1; i++) {
212215
portId = reportSerial[i];
216+
bytesToRead = serialBytesToRead[portId];
213217
serialPort = getPortFromId(portId);
214218
if (serialPort == NULL) {
215219
continue;
@@ -224,11 +228,19 @@ void checkSerial()
224228
Firmata.write(START_SYSEX);
225229
Firmata.write(SERIAL_MESSAGE);
226230
Firmata.write(SERIAL_REPLY | portId);
231+
232+
if (bytesToRead == 0 || (serialPort->available() <= bytesToRead)) {
233+
numBytesToRead = serialPort->available();
234+
} else {
235+
numBytesToRead = bytesToRead;
236+
}
237+
227238
// relay serial data to the serial device
228-
while (serialPort->available() > 0) {
239+
while (numBytesToRead > 0) {
229240
serialData = serialPort->read();
230241
Firmata.write(serialData & 0x7F);
231242
Firmata.write((serialData >> 7) & 0x7F);
243+
numBytesToRead--;
232244
}
233245
Firmata.write(END_SYSEX);
234246
}
@@ -725,9 +737,10 @@ void sysexCallback(byte command, byte argc, byte *argv)
725737
case SERIAL_CONFIG:
726738
{
727739
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
729740
byte txPin, rxPin;
730741

742+
serialBytesToRead[portId] = (int)argv[4] | ((int)argv[5] << 7);
743+
731744
if (portId > 7 && argc > 6) {
732745
rxPin = argv[6];
733746
txPin = argv[7];

0 commit comments

Comments
 (0)