Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PySerial delay
#1
Geeting! I stumbled across this forum in search of help so hopefully someone here has the magic answer.
I have a python script that pulls JSON data, and spit it to an Arduino. The arduino returns a number in about 1/4 of a second but somehow its lost and doesn't show up on the input buffer if I used pyserial write within the last few seconds.

Here is a tidbit of the code.
This is settings for the serial connection.
ser = serial.Serial('/dev/ttyUSB0', baudrate = 115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=0.1, write_timeout=0.1)
And here is the portion of the write followed by read

def serial_transmit(data):
    waittimer = time.time() + 5
    devid = int(data["networkID"])
    print (devid)
    tempdata = str(data)
    print (tempdata)
    transmitbuffer = tempdata.encode()
    print(ser.write(transmitbuffer))
    sleep(0.1)
    readserdata = b''
    print ("sending")
    print (ser.out_waiting)
    while len(readserdata) < 1:
        if (time.time() > waittimer):
            return False
        readserdata = ser.read(1)
        sleep(0.1)
The above code fails to receive anything from the arduino even those the arduino replies in 1/4 of a second.
Now if I tell the arduino to wait 2 seconds before sending the number, it works fine.

I added a bunch of print lines to make see how long it takes to reach the ser.read (which it makes it to the read in less than 1/10 of a second) but no matter what I do, the receive buffer never receives that number if its sent within a second of pyserial sending the first data.

I poked around. added various pauses but no matter what I do best I can tell us I'm using pyserial wrong where if I send data, the receive port isn't listening in time and that number is lost.

As another test I used miniterm.py to make sure I can send that string and receive the number which I do just fine.

Thank you all for you help!!!
Reply
#2
Never mind. I figured it out as what I was doing wrong. I created serial read and write threads which helped me troubleshoot my error. The end of the string was missing '\n' before encoding...... sometimes asking for help helps me realize my own errors. :)

sorry to bother everyone!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mesure delay time in network (graph) leemao 0 4,756 Jul-02-2020, 04:06 PM
Last Post: leemao
  Sending response delay michalt38 1 23,241 Feb-13-2018, 03:19 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020