3

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?

3
  • 1
    you are printing out ser.readline(), right? I did that and it is working for me. There is no delay at all. However, I am also using Linux right now (had to change the port of course). Maybe it is a platform problem? I really don't have any other ideas based on what you've mentioned so far. Commented Oct 12, 2011 at 4:30
  • I'm just waiting for the site to let me answer because i'm a new member. but in short, it worked so long as the python code had a higher refresh rate than the arduino and you mapped it to a variable. Commented Oct 12, 2011 at 7:11
  • I just tried it in Windows and didn't have a problem. It was more of a pain to set up (especially on 64-bit), but other than the longer delay for start-up, I didn't see any real problems. I did stupidly press the reset button on the board for a moment which confused me momentarily. I am using an older board though than you - the Duemilanove. Commented Oct 12, 2011 at 17:22

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

the link is broken
1

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.

10 Comments

it's println. To clarify things a bit, it outputs a constant stream of 0's for about 15-20sec and then it changes state. The actual output is '0/r/n' or '1/r/n'
How does it behave if you use Serial.print on the Arduino side and ser.read(1) on the python side?
It's neatened it up, the output is now only 0 or 1, but i still get about 20s of '0' before it picks up on a button press.
This is certainly because of caching issues, try to put a little sleep in the loop and see it it improves. How much time does your 20 '0' take?
LC2817, there are about 20seconds of 0's before it picks up on the button press. Would you put the delay in the Arduino side or the Python side?
|
1

I just met the same problem and I'm sure there is no delay in PySerial.

The delay was caused by the delay in my PyQT Thread .I print through serial port in arduino with one line/0.1sec but I read the serial output in QThread with a 0.5sec delay,that's the problem.As time goes by,the delay will increase.

I verified that by extracting pyserial reading code from my project.Just remember,the read frequency should not be less than the write frequency.

From your code,I assume that your python enviroment if not fast enough to receive the data from arduino in time.

Try to slow down the serial print speed by insert a small delay between two print.

Comments

1

Only commence a readline if there is something to read otherwise it will either block while waiting for eol, or it may time out half way through reading the serial buffer, truncating your string. This speeds up the loop, and allows you to use a short timeout, great for cycling through multiple ports. Using pyserial3.0...

while 1:
    if ser.in_waiting > 0:
        data = ser.readline()
        print(data)

Also try something like

while 1:
    if ser.in_waiting > 0:
        data = ser.read(32)
        print(data)

Which will not care if the buffer contains less than the specified bytes, I sometimes do as it will read/flush out extra data that has builds up in the buffer.

1 Comment

woops I thought this was a new question! way old sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.