Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PySerial delay in reading line from Arduino

I'm using an arduino uno with the basic "DigitalReadSerial" setup as outlined here: http://arduino.cc/en/Tutorial/DigitalReadSerial

If i use the serial monitor included with the arduino IDE, it immediately displays any changes from pressing the button.

This is what i want in pyserial, either a 0 or a 1 depending on whether the button is pressed. (to eventually trigger a timer)

just to test it, i threw this together, not the prettiest, but it seems to read out the pushbutton state, but there is a 20second delay.

    import serial
    ser = serial.Serial()
    ser.setPort("COM2")
    ser.baudrate = 9600
    ser.open()
    while 1==1:
        ser.readline()

Does anyone have any ideas?

like image 403
MikeAyles Avatar asked Dec 18 '25 10:12

MikeAyles


2 Answers

It seems to be a caching/sync problem, similar to those that affects the file sync in common filesystems. I have suffered that problem with my arduino/pyserial... until now?

From http://pyserial.sourceforge.net/pyserial_api.html, if I put the 3 flush commands: ser.flush(), ser.flushInput() and ser.flushOutput() in my program, it seems to work as expected.

like image 200
Minaya Avatar answered Dec 20 '25 01:12

Minaya


Are you using Serial.print or Serial.println in your Arduino code? If the former, its not going to issue a carriage return and the ser.readline() in your code will be waiting for one.

like image 21
donkopotamus Avatar answered Dec 19 '25 23:12

donkopotamus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!