I can hazard a guess, but without being able to play with your IO myself I'm really shooting in the dark: remember that x=d2.read(d2.inWaiting())
is a blocking call, so your program will wait here until some data is received. Furthermore, I believe that inWaiting
is called before read, and will return 0 until some bytes are actually received, so in effect your method call is d2.read(0)
the first time you run your program. From the documentation , I actually don't see anything to indicate the behavior of this call, but it's probably not what you want. Then, when you start the program a second time, inWaiting will actually return the number of bytes in buffer (that were not read on the previous run), and your read call will actually begin returning bytes.
http://pyserial.readthedocs.io/en/lates ... l_api.html
If d2 is going to be receiving discrete packets of known and consistent size, I would use that known size as your arg to read rather than InWaiting(). If packetsize is not known exactly, you can use an arbitrary number greater than maximum packet size and rely on a timeout
to packetize your data. (if your data includes delimiters, you can read one byte at a time and construct packets in another function). You can specify an interval after which read will return whatever it's got (an empty byte field if nothing has been received), or set timeout to 0 to read in non-blocking mode, such as: s = serial.Serial('/dev/ttyUSB0', timeout=0')
or whatever is going on in your open_port function.
I am much more confident that you should be using PySerial to control your RTS line, not GPIO. I'm guessing the IOError is being raised because you are not actually controlling the RTS line.
ps: use python3