0
votes

I'm working on a simple project which requires me to write a Python script that will send data through a serial port from a PC to an Arduino. As part of this project I want to be able to check how many bytes are in the 64 byte input buffer of the Arduino. The issue is that when I try to write data and then use pySerial's out_waiting method, it always returns zero. This is a toy model I have been using for testing.

Arduino code:

void setup() {
  Serial.begin(9600);
}

void loop() {    
}

Python code:

import serial
import time

Arduino = serial.Serial('COM5',9600)

for i in range(20):
    Arduino.write('1'.encode())

time.sleep(1)

print(Arduino.out_waiting)

Arduino.flushOutput()
Arduino.flushInput()

print(Arduino.out_waiting)

Arduino.close()

I would expect the first print statement to output 20 and the second to output 0. They both output 0, and I really can't figure out why.

Here is a link to the pySerial documentation: pySerial docs

Thanks in advance for any help.

1
Hi, your Arduino code doesn't nothing but init the serial port. Did you try with a loop() that sends something to the serial port and reads if some data is present?rene-d
How would your Python code running on your PC know how many bytes are in the Arduino's buffer?kindall
Hi rene-d. I realize that my Arduino code only initializes the serial buffer. Once initialized can't I write data to it with Python? I can read if data is present on the Arduino with a loop(), but once I have written data to the buffer I can't check with Python what is in the buffer.Spencer
Hi Kindall. Maybe I am misunderstanding buffers in general here. I don't know how my PC would know how many bytes are in the Arduino's buffer. I was hoping that out_waiting would tell me that. Maybe to help me understand could you give me a simple example of when the out_waiting would not return 0?Spencer

1 Answers

0
votes

you can initialise write_timeout=0 before start writing message to port