Random freezes when outputting data over USB

Status
Not open for further replies.

Jimi_Hendrik

New member
Dear all,
I have a Teensy 3.6 which is receiving sensor data from an Arduino Nano via UART (Baud 250000, 12 Bytes data package with a main loop running at 200 Hz) and is connected to a computer via USB (BAUD 230400, transmits 62 Bytes via Serial.write(buffer, length) at 400 Hz).

In this example I use Serial.print() but in my implementation I would use Serial.write for the full set of data (which also includes data from some analog sensors), which creates the same error.

Code:
//This is the Teensy implementation

float forces[3], torques[3];
                 
Sensor Sensor(PinTx, PinRx);

void setup() {
  Serial.begin(230400);   // initialize a serial connection to computer
  Sensor.begin();
}

void loop() {
    if  (Sensor.getForces(forces, torques) ) { // Reads the Arduino Nano datastream coming in at UART and parses to float array (confirmed working)
  Serial.print(forces[0]); 
//  Serial.print(forces[1]); //If I want to print more than one field here it crashes.
 }
delay(2); //If I take a higher number it works, if I have "2" it works randomly after I reflash the teensy a few times
}

Now I have the following issue: Sometimes I only receive a couple of bytes after opening the usb port and then the transmission stops (see figure). Then I flash the firmware again, and then it stops again after a few bytes. But when I repeat the flashing operation a few times, at some point it works and i get a continuous stream of data. Even after closing and reopening the usb port (but not after reconnecting the USB cable). My best guess is that some buffer is overflowing but the error seems to appear randomly...

Since I am a novice in this field I would be happy to receive some hints on how to fix or what to improve.

usb_serial.jpg
 
I think there is some bits missing from your example code since it does not compile for me but it sounds like you have a syncing problem with parsing serial data.

Basically you need to handle the cases of:
a, your code starts running while sensor is partway through sending data (needs to dump bytes till it finds some end or start indicator)
b, your loop returns while the sensor has not yet finished sending all bytes - suspect this is what happens with your delay (needs to put bytes somewhere and wait some more)
c, you get a corrupt byte changing the length or removing an endline (simple solution is detect length errors, checksums and lost end lines and if so dump the entire message, wait for next message)
 
Maybe this could be the Linux default tty line discipline problem again? Or other not-yet-understood issues with some Linux systems?

First, make sure you have Teensyduino 1.42 installed. In Arduino, click Help > About to check.

As a quick first check, after uploading try opening the port with the Arduino Serial Monitor. This should put the port into raw mode, which turns off any tty line discipline setting your system might have. Then close the serial monitor and try opening with whatever software you've using. If opening first with Arduino makes a difference, then this is almost certainly the tty line discipline setting.

Here's another recent conversation.

https://forum.pjrc.com/threads/52907-unable-to-read-serial-data
 
I'd also suggest testing with a slower baud rate between the sensor board and Teensy. For 12 bytes 200 times per second, 38400 baud should be adequate.

While we can't see the "Sensor" code you're using, the few lines you did show look a lot like libs we've seen which are actually based on the horrible SoftwareSerial library. If that sort of code is in play, blocking all other interrupts for lengthy times, it really should be replaced with using the actual hardware serial port.
 
Thank you for the helpful comments. Here is what I concluded.
1. I updated to Teensyduino 1.42 (I had 1.41 before) -> error persisted
2. I think it was not related to tty line setting in this case. I opened the port with the arduino terminal before i switched to my other portsniffer tools. Tried this a couple of times. -> error persisted

I went back then to redesign parts of the code and it looks like my main loop was running too quickly (in the example code).
On my implementation I really made sure now that the expected, coded and measured timings match. For instance, I was sending a few more bytes than i initially thought from the arduino nano which caused the processing on the teensy to slow down. Together with another timer I set on the teensy for sending data packages out, my mainloop returned while the processing was not yet finished. Similar to suggestion b) from GremlinWrangler.

Thanks again for your help
 
Status
Not open for further replies.
Back
Top