Inconsistent serial between Teensy 3.2 and PySerial

Status
Not open for further replies.

costelljoe

New member
I am using usb serial to send data from a Teensy 3.2 to a Ubuntu computer. The teensy reads in data from an SD file, then repeatedly sends over serial. The receiving computer uses PySerial to read in the data (code below).

However, PySerial often times-out and doesn't read in any data, or reads in some then throws the error "serial.serialutil.SerialException: device reports readiness to read but returned no data".
The arduino serial monitor reads in the data consistently.

Is there any way to make the connection more consistent? The end goal is to eventually stream recorded audio over serial.
Thanks!

Teensy Code
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

File audiofile;
const int chipSelect = 10;

const int N = 512;
byte buffer1[N];
bool fileLoaded = false;


void setup() {
  Serial.begin(115200);

  // Initialize SD card
  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(chipSelect))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  // Make sure file exists
  if (!SD.exists("record.txt")) {
    while (1) {
      Serial.println("No audio data saved on teensy");
      delay(500);
    }
  }

  // load file
  audiofile = SD.open("record.txt", FILE_READ);
  audiofile.read(buffer1, N);
  fileLoaded = true;
}


void loop() {
  if (fileLoaded) {
    Serial.write(buffer1, N);
    delay(1);
  }
}

Python Code
Code:
import serial

# setup serial connection
ser = serial.Serial('/dev/tty.ACM0', 115200, timeout=1)
ser.reset_input_buffer()
print("serial connected")

N = 512

while True:
	if ser.in_waiting > N:
		# print available data
		response = ser.read(N)
		print(response)

ser.close()
 
Wonder if adding '=' to this :: if ser.in_waiting >= N:

changes anything … or then trying this might change the behavior:: N = 256
 
Are the data to be transferred ascii or binary?
in case of binary, can you set the python code to ignore xon/offf handshaking, or set data type to binary?
 
What computer are you running PySerial on? I am not a python user, but did find with ROS running on RPI boards that pyserial did not appear to work overly well. Or at least not for me... But that was a few years ago.

That is what device is /dev/tty.ACM0? Is that usb or serial port? I assume USB port? But don/t know about the . in the name?

Also not sure about if anything the 115200 baud rate does here. The USB sends at USB speed.

Now assuming that PySerial does set the baud rate to 115200, wonder how many characters it can receive in 1 millisecond? If my quick calculations are correct, it would take something like 44ms to send 512 bytes at 115200...

Wonder if you are completely overrunning the buffers on either side...

If it were me, I would put in a few more things on the Teensy side to get an idea what is going on. Like maybe each time in the loop, change the state of the LED, like:
digitalWrite(13, !digitalRead(13));

You will obviously need a pinMode in setup. Won't work when you are using Audio, but might give you a hint if it is hanging.

Also maybe on Teensy side, Maybe try to output the buffer in chunks. In particular, ask serial how many bytes are available for write? And only output that many characters. Then stay in the loop, and again ask for how many bytes available and output the next part, until the while buffer is output... Again not sure if that will help here or not, but...
 
Last edited:
Why specify a timeout of 1 second on the serial port? Your error sounds like it may be a timeout error.
 
On Ubuntu with T3.2 sending 512 characters, I don't get any error messags, but it hangs with delay(1); Seems to keep running with delay(10);
 
Status
Not open for further replies.
Back
Top