Python Event Driven Seriale

Active2 months ago
  1. Event Driven Strategy
  2. Event Driven Program Python

Event-driven programming depends upon an event loop that is always listening for the new incoming events. The working of event-driven programming is dependent upon events. Once an event loops, then events decide what to execute and in what order. Following flowchart will help you understand how this works − Python Module – Asyncio.

The port is immediately opened on object creation, when a port is given. It is not opened when port is None and a successive call to open is required. Port is a device name: depending on operating system. /dev/ttyUSB0 on GNU/Linux or COM3 on Windows. Circuits is a Lightweight Event driven and Asynchronous Application Framework for the Python Programming Language with a strong Component Architecture. Circuits also includes a lightweight, high performance and scalable HTTP/WSGI compliant web server as well as various I/O and Networking components.

I am reading serial data like this:

The problem is that it prevents anything else from executing including bottle py web framework. Adding sleep() won't help.

Changing 'while True' to 'while ser.readline():' doesn't print 'test', which is strange since it worked in Python 2.7. Any ideas what could be wrong?

Ideally I should be able to read serial data only when it's available. Data is being sent every 1,000 ms.

dda
5,5242 gold badges20 silver badges33 bronze badges

Event Driven Strategy

DominicMDominicM
1,82712 gold badges31 silver badges55 bronze badges

4 Answers

Put it in a separate thread, for example:

Chiramisu
3,3106 gold badges39 silver badges71 bronze badges
Fredrik HåårdFredrik Håård

Using a separate thread is totally unnecessary. Just do this for your infinite while loop instead (Tested in Python 3.2.3):

This way you only read and print if something is there. You said, 'Ideally I should be able to read serial data only when it's available.' This is exactly what the code above does. If nothing is available to read, it skips on to the rest of your code in the while loop. Totally non-blocking.

(This answer originally posted & debugged here: Python 3 non-blocking read with pySerial (Cannot get pySerial's 'in_waiting' property to work))

pySerial documentation: http://pyserial.readthedocs.io/en/latest/pyserial_api.html

UPDATE:

  • 27 Dec. 2018: added comment about in_waiting vs inWaiting(). Thanks to @FurkanTürkal for pointing that out in the comments below. See documentation here: https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.in_waiting.
  • 27 Oct. 2018: Add sleep to let other threads run.
    • Documentation: https://docs.python.org/3/library/time.html#time.sleep
    • Thanks to @RufusV2 for bringing this point up in the comments.

Note on multi-threading:

Even though reading serial data, as shown above, does not require using multiple threads, reading keyboard input in a non-blocking manner does. Therefore, to accomplish non-blocking keyboard input reading, I've written this answer: How to read keyboard-input?.

Gabriel StaplesGabriel Staples
3,2243 gold badges21 silver badges48 bronze badges

I would warn against using blocking IO in a thread. Remember Python has a GIL and at one time only one thread can execute. Now please note that pyserial module is a wrapper over an OS implementation of accessing the serial port. That means it calls code external to the Python. If that code blocks, then the interpreter also get blocked and nothing will execute in the Python program, even the main thread.

This can even happen when using non-blocking IO or timeout based polling if the underlying device driver does not implement timeout well.

A more robust approach is to use multiprocessing module with a queue. Run serial read code in a separate process. This will make sure main and other threads don't block and the program can exit in clean way.

Mohammad AzimMohammad Azim

Use a timer driven event to test and read the serial port.Untested example:

Keren Caelen
1,3462 gold badges15 silver badges33 bronze badges
William KuipersWilliam Kuipers
Python Event Driven Seriale

Event Driven Program Python

Not the answer you're looking for? Browse other questions tagged pythonpython-3.xnonblockingpyserial or ask your own question.

This two-part course is designed to help students with very little or no computing background learn the basics of building simple interactive applications. Our language of choice, Python, is an easy-to learn, high-level computer language that is used in many of the computational courses offered on Coursera. To make learning Python easy, we have developed a new browser-based programming environment that makes developing interactive applications in Python simple. These applications will involve windows whose contents are graphical and respond to buttons, the keyboard and the mouse.In part 1 of this course, we will introduce the basic elements of programming (such as expressions, conditionals, and functions) and then use these elements to create simple interactive applications such as a digital stopwatch. Part 1 of this class will culminate in building a version of the classic arcade game 'Pong'.

Posted on