Hi,
I want to use Teensy 3.2 both to receive streaming LED data over USB and send sensor data to the host.
However, I'm running into an issue where if the host sends data too quickly, communication from the Teensy stops.
Here is a minimum working example:
And sender script:
I get the following:
As you can see, it prints Received and not Finished. And the LED is blinking, which means that availableForWrite() is returning 0. If I change the final sleep in the python to 0.4sec, then it works correctly as the buffer can be emptied in time.
When I look at the Teensy platform code my guess is that usb_malloc() is failing when the receive buffer is full. But I have no idea how to overcome this. I have also tried separating the return data stream to SerialUSB1, but it doesn't fix the issue - both devices share the same usb_malloc().
I want to use Teensy 3.2 both to receive streaming LED data over USB and send sensor data to the host.
However, I'm running into an issue where if the host sends data too quickly, communication from the Teensy stops.
Here is a minimum working example:
Code:
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.println("Hello world");
}
void loop() {
digitalWrite(13, LOW);
while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
Serial.println("Received");
delay(400);
if (Serial.availableForWrite() == 0) {
digitalWrite(13, HIGH);
delay(20);
}
Serial.println("Finished");
break;
}
}
}
And sender script:
Code:
import serial
import time
ser = serial.Serial(port="/dev/tty.usbmodem66941801", baudrate=115200,
timeout=10.0, write_timeout=10.0)
while True:
a = time.time()
n = ser.write(bytes("x"*1500 + "\n", "utf-8"))
print(f"Wrote. {n} bytes in {(time.time() - a)*1000:.3f}ms")
inp = ser.read_all()
if inp:
print(f"Read: {inp}")
time.sleep(0.02)
I get the following:
Code:
Wrote. 1501 bytes in 0.098ms
Wrote. 1501 bytes in 0.192ms
Read: b'Received\r\n'
Wrote. 1501 bytes in 0.153ms
Wrote. 1501 bytes in 1010.470ms
Read: b'Received\r\n'
Wrote. 1501 bytes in 0.180ms
Read: b'Received\r\n'
Wrote. 1501 bytes in 1027.110ms
Read: b'Received\r\n'
Wrote. 1501 bytes in 0.192ms
Read: b'Received\r\n'
Wrote. 1501 bytes in 1027.653ms
Read: b'Received\r\n'
Wrote. 1501 bytes in 0.163ms
As you can see, it prints Received and not Finished. And the LED is blinking, which means that availableForWrite() is returning 0. If I change the final sleep in the python to 0.4sec, then it works correctly as the buffer can be emptied in time.
When I look at the Teensy platform code my guess is that usb_malloc() is failing when the receive buffer is full. But I have no idea how to overcome this. I have also tried separating the return data stream to SerialUSB1, but it doesn't fix the issue - both devices share the same usb_malloc().