Python Serial Write Timeout
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
PySerial Documentation, Release 3.4. Python -m serial.tools.listportswill print a list of available ports. It is also possible to add a regexp as first argument and the list will only include entries that matched. writetimeout(float) – Set a write timeout value. The following are code examples for showing how to use serial.Serial.They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. Python serial port access library. Contribute to pyserial/pyserial development by creating an account on GitHub.
Sign upHave a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that readlines only works with a timeout. Readlines depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly. The following are code examples for showing how to use serial.Serial.They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that readlines only works with a timeout. Readlines depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.
Already on GitHub? Sign in to your account
Comments
commented Oct 4, 2015
When I set a timeout like this: pyserial just retries reads like this: I need to be able to know, when the read has timed out to resend my last message. Please raise a TimeoutError, so I can use pyserial like this: I think that it is unpythonic to silently drop errors like a timeout. When a user of pyserial invokes a read(), the expected outcome is that it returns the data that has been read. So hitting the timeout is an error. |
commented Oct 4, 2015
I found a workaround of using threads that communicate over queues: |
Python Serial Readline Timeout
commented Oct 4, 2015
You can detect that there was a timeout by checking the length of returned bytes versus the number of bytes requested. It is more like an EOF in Python, rather than a timeout error. That is also how some protocols are using the timeout, as packet delimiter. An exception would be inconvenient in many cases. E.g. consider reading more than one byte but fewer arrive, the timeout strikes. Then you'd still expect to get the bytes, but how? Attached to the exception? Probably not where most people would expect to find data. When they would be returned with the current |
commented Oct 6, 2015
Python Serial Write Timeout
Thanks for your reply, @zsquareplusc. Checking the length of the returned bytes did not work for me. If I requested a specified number of bytes with a timeout, I would either expect that exact number of bytes or an exception. I am not sure if it would also be OK to return all bytes that have been read up to the timeout. That would definitely need to be documented and users would need to check whether the received data is complete. I think there is little value in aborted data, because the timeout can strike at any given instance, so users would need to program every eventuality. In practice this means to handle as many cases as characters requested. Restarting the communication should be easier to program in most cases. |