Asynchronous (event-driven) programming is supported in the standard Python library in the asyncore and asynchat modules, which are very oriented to networking tasks (indeed they internally use the select module, which, on Windows, only supports sockets - though on Unixy OSs it can also support any file descriptor).
Event Driven Program Python
P: n/a | We have some equipment that communicates at 57600 baud RS232. The path from the PC is USB to a Phillips USB hub, then off of that a TUSB3410 USB/Serial converter. The driver for the 3410 chip creates a 'normal' comm port (like COM3). There is a C++ program that has no problem talking to the equipment, but two Python programs have trouble. One is a big, scary, controller program, and the other, since that is the one I'm having trouble with, is a while() loop. The while loop program seems to be able to open and close the serial port all day. It also seems to be able to open, send 'blahblahblah', and close the port for the same length of time. But writing anything to the port quickly (not always right away...like maybe 5-6 iterations through the loop?) causes the win32file.CloseHandle(self.hComPort) statement in the 'def close(self)' function of the serialwin32.py file of the pySerial package to hang for a few seconds, then return. The next open() attempt fails with the 'could not open port: (995, 'CreateFile', 'The I/O operation has been aborted because of either a thread exit or an application request.') exception. The failure mode with the large program is it opens the port successfully completes a bunch of back and forth talking, then hangs for a few seconds when the port is closed, and then can't reopen the port. It never seems to fail in the middle of a bunch of reads and writes, but only when/after the port is closed. It can also run for the better part of an hour with no problems then all of a sudden... Like I said, the C++ program never fails. I've looked at the settings in the C++ program, and in the serialwin32.py file and tried to set them the same as best I can, but no luck. Digging into the win32all stuff gets me lost quite quickly. I see all of the settings, but have no idea what to try. This is all on WinXP, Python 2.4.1, the latest pySerial and win32all. Does this ring any bells with anyone? Thanks! Bob
|
|
Python Event Driven
The serialport can be controlled by RFC 2217 commands. This object will modify the port settings. In Python versions prior 3.x, bytes is a subclass of str. They convert str(17). The actual reader loop driven by the thread. Event Loop¶ Preface. The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Application developers should typically use the high-level asyncio functions, such as asyncio.run, and should rarely need to reference the loop object or call its methods. This section is intended mostly for authors of lower-level code, libraries, and frameworks, who need finer control over the event loop behavior.