Skip to content

Commit

Permalink
Merge pull request #49 from firmata/rx-buffer-per-port
Browse files Browse the repository at this point in the history
setup ability to enable rx buffering per serial port
  • Loading branch information
soundanalogous committed Mar 7, 2016
2 parents b7433f4 + f35dc24 commit a587e4d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/SerialFirmata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
See file LICENSE.txt for further informations on licensing terms.
Last updated by Jens B.: March 5th, 2016
Last updated March 6th, 2016
*/

#include "SerialFirmata.h"
Expand Down Expand Up @@ -55,11 +55,17 @@ boolean SerialFirmata::handleSysex(byte command, byte argc, byte *argv)
{
long baud = (long)argv[1] | ((long)argv[2] << 7) | ((long)argv[3] << 14);
serial_pins pins;
#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING)
lastAvailableBytes[portId] = 0;
lastReceive[portId] = 0;
// this ifdef will be removed once a command to enable RX buffering has been added to the protocol
#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING)
// 8N1 = 10 bits per char, max. 50 bits -> 50000 = 50bits * 1000ms/s
// char delay value (ms) to detect the end of a message, defaults to 50 bits * 1000 / baud rate
// a value of 0 will disable RX buffering, resulting in single byte transfers to the host with
// baud rates below approximately 56k (varies with CPU speed)
maxCharDelay[portId] = 50000 / baud;
#else
maxCharDelay[portId] = 0;
#endif
if (portId < 8) {
serialPort = getPortFromId(portId);
Expand Down Expand Up @@ -288,9 +294,7 @@ void SerialFirmata::checkSerial()

if (serialIndex > -1) {

#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING)
unsigned long currentMillis = millis();
#endif

// loop through all reporting (READ_CONTINUOUS) serial ports
for (byte i = 0; i < serialIndex + 1; i++) {
Expand All @@ -310,7 +314,6 @@ void SerialFirmata::checkSerial()
if (availableBytes > 0) {
bool read = true;

#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING)
// check if reading should be delayed to collect some bytes before
// forwarding (for baud rates significantly below 57600 baud)
if (maxCharDelay[portId]) {
Expand All @@ -322,7 +325,6 @@ void SerialFirmata::checkSerial()
lastAvailableBytes[portId] = availableBytes;
}
}
#endif

if (read) {
Firmata.write(START_SYSEX);
Expand All @@ -335,9 +337,11 @@ void SerialFirmata::checkSerial()
numBytesToRead = bytesToRead;
}

#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING)
lastAvailableBytes[portId] -= numBytesToRead;
#endif
if (lastAvailableBytes[portId] - numBytesToRead >= 0) {
lastAvailableBytes[portId] -= numBytesToRead;
} else {
lastAvailableBytes[portId] = 0;
}

// relay serial data to the serial device
while (numBytesToRead > 0) {
Expand Down
4 changes: 1 addition & 3 deletions src/SerialFirmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
See file LICENSE.txt for further informations on licensing terms.
Last updated by Jens B.: March 5th, 2016
Last updated March 6th, 2016
*/

#ifndef SerialFirmata_h
Expand Down Expand Up @@ -147,11 +147,9 @@ class SerialFirmata: public FirmataFeature
int serialBytesToRead[SERIAL_READ_ARR_LEN];
signed char serialIndex;

#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING)
unsigned long lastReceive[SERIAL_READ_ARR_LEN];
unsigned char maxCharDelay[SERIAL_READ_ARR_LEN];
int lastAvailableBytes[SERIAL_READ_ARR_LEN];
#endif

Stream *swSerial0;
Stream *swSerial1;
Expand Down

0 comments on commit a587e4d

Please sign in to comment.