0
votes

The Problem

While switching from TCP over Ethernet to Serial, the Arduino Mega wasn't receiving signals, and wasn't sending things back to the computer. After reducing the frequency to one packet every 800 ms, the problem went away, but we need to send signals more frequently than this. Switching to an Arduino Uno lets us use frequencies <800 ms, but none of these affected the minimum delay on the Mega:

  • Increasing the baud rate
  • Shortening the message sent has no effect.
  • Removing calls to functions such as Serial.available

How can I increase the throughput over the serial port?

Python Code

import serial  # pip install pyserial
import time

s = serial.Serial('COM6')
while True:
    while s.in_waiting:
        ch = s.read()
        print(ch)

    s.write(b'abc')
    time.sleep(0.1)   # this number works if it's >0.8

Node Code

var SerialPort = require('serialport'); // npm i serialport

var port = new SerialPort('COM6', {
  baudRate: 9600
});

setInterval(function() {
    port.write('0 0 0 0');
    if (port) {
        let s = port.read();
        if(s) {
            console.log(s.toString());
        }
    }
    console.log("sent");
}, 100); // this number works if it's >0.8

Arduino Code

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.read();
    Serial.println("blah");
}
1

1 Answers

0
votes

The Arduino needs time to set up the serial connection. It requires ~750 ms, which explains the minimum latency. The solution is to add a delay after setting up the serial port.

Python

s = serial.Serial('COM6')
time.sleep(1)  # required to reset the Serial connection on Megas
...

Node

var port = new SerialPort('COM6', {
  baudRate: 9600
});
setTimeout(
  /* ... */
  , 1000);  // required to reset the Serial connection on Megas

Source: https://github.com/noopkat/avrgirl-arduino/issues/43