0

I am working on a project to send data which is on an sdcard in a teensy 4.1, via the micro-usb port to a computer with pyton.

Now I have my procedure can't of working but see that when the files become to big the python serial readline() does not get all the data and always stops after 34816 bytes received. I played around with introducing different sleeps in python but no succes so far. or is it that python used can't keep up with the teensy? If I use the arduino serial monitor I do get all the lines in the terminal.

receiving the data I broke up in two parts, I first request all the filenames and there sizes that are stored on the sdcard. then in python I loop over the file name list and request them one by one from the teensy.

Some code snippets that I am currently using:

Teensy

else if (!strncmp(command, "get,", 4)) {
    ExFile putFile;
    uint8_t r;
    uint32_t count = 0;
    boolean ready = false;
       
    // Open file
    putFile = sd.open(&command[4], O_READ);

    if (putFile == NULL) {
      Serial.println("File not found");
      return;
    }
    while (putFile.available() && !ready) {
      r = putFile.read();

      Serial.write(r);

      // Check for new line
      if (r == 0x0A)
        count++;
    }
    // Close file
    putFile.close();
    Serial.println("EndOfMessage");
    // End of file

Python:

def __init__(self, serial_port, baud_rate=115200, timeout=1):
        self.serial_port = serial_port
        self.baud_rate = baud_rate
        self.timeout = timeout
        self.ser = None
        self.bytesize = 8
        self.stopbit = 1

    def connect(self):
        self.ser = serial.Serial(
            self.serial_port, baudrate=self.baud_rate, timeout=self.timeout, bytesize=self.bytesize, stopbits=self.stopbit)

        # time.sleep(1)  # Allow time for the connection to stabilize
        # self.ser.set_buffer_size(rx_size=1600000, tx_size=1600000)
        self.ser.reset_input_buffer()
def get_file(self, filename):
        self.write_command(f"get, {filename}\n")
        time.sleep(0.2)
        lines = []
        while True:
            line = self.ser.readline()

            if not line:
                # self.ser.flush()
                break
            if line.strip() == b'EndOfMessage':
                # self.ser.flush()
                break
            else:
                lines.append(line)
                # self.ser.flush()

        return lines

anyone an idea what I should change so that all data from the files are send to python.

RvN
  • 31
  • 1
  • 4
  • It is likely that you are hitting the limit of a buffer. Apparently in some platforms you can increase it: https://stackoverflow.com/a/50024408/1643946. But otherwise reorganising your transmission into blocks that are smaller than 32KB means that the ser.readline won't over-fill. Or just read byte by byte, https://stackoverflow.com/a/44639890/1643946 – Bonlenfum Aug 20 '23 at 08:53
  • @Bonlenfum, thanks for your answer, I have tried to increase the buffer size but did not change anything. So think my system does not allow it. Will look into breaking up the transmission. Bit strange to that I don't have the problem with the arduino monitor, any idea what it does differently then the ser.readline? – RvN Aug 20 '23 at 18:43

0 Answers0