Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Inconsistent serial between Teensy 3.2 and PySerial

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    4

    Inconsistent serial between Teensy 3.2 and PySerial

    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()

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    7,078
    Wonder if adding '=' to this :: if ser.in_waiting >= N:

    changes anything … or then trying this might change the behavior:: N = 256

  3. #3
    Senior Member
    Join Date
    Jul 2014
    Posts
    1,938
    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?

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,699
    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 by KurtE; 12-08-2018 at 02:10 PM.

  5. #5
    Senior Member
    Join Date
    May 2017
    Posts
    148
    Why specify a timeout of 1 second on the serial port? Your error sounds like it may be a timeout error.

  6. #6
    Senior Member
    Join Date
    Jul 2014
    Posts
    1,938
    Quote Originally Posted by rcarr View Post
    Why specify a timeout of 1 second on the serial port? Your error sounds like it may be a timeout error.
    delay(1) means 1 ms

  7. #7
    Senior Member
    Join Date
    May 2017
    Posts
    148
    Quote Originally Posted by WMXZ View Post
    delay(1) means 1 ms
    My note was about the timeout in the python code. ser = serial.Serial('/dev/tty.ACM0', 115200, timeout=1)

  8. #8
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,642
    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •