Serial Communication Python Class. CUI and GUI both are available.
- Using a thread, TX and RX both are available at the same time.
- Execute
serial.close()
and disconnect safely.
Serial Communication Python Class.
- time: Standard library
- threading: Standard library
- serial (pySerial): Third-Party library (
pip install pyserial
)
SerialCom (class) // Args: baudrate, timeout, writemode
├── devices (variable) // comports list
├── device (variable) // selected comport
├── serial (variable) // serial.Serial instance
├── writemode (variable) // boolean serial write accept
├── find_comports (method) // Find comports and save to `self.devices`
├── select_comport (method) // Select a comport from list
├── register_comport (method) // Save a comport to `self.serial.port`
├── open_comports (method) // Open the comport
├── close_comports (method) // Close ther comport
├── serial_write (method) // Write strings to comport
├── start_serialwrite (method) // Start `serial_write` in another thread
├── serial_read (method) // Read strings from comport
├── start_serialcom (method) // Start serial communication
├── start_serialcom_option (method) // Start serial communication (using option args)
├── get_found_devices (method) // Return `self.devices`
├── get_selected_device (method) // Return `self.device`
└── get_write_available (method) // Return `self.writemode`
-
Select a comport from the combobox. If you connect a device after the startup, push "Reload" button and check the combobox again.
-
After input a baudrate, push "Open" button. If the "Baudrate" entry is empty, the rate is 9600 bps by default or the last bps you open the port.
-
Input string to "Text" entry and push "Send" button. "TX" listbox is a sending history and "RX" listbox is a receiving history. Pushing "TX Export" button or "RX Export" button, you can save these histories as a text file.
- Select a comport.
- Input "yes", "y" or "no", "n".
- Serial communication starts.
- To disconnect, input Ctrl+C.
- 9600 bps serial communication
- Once get a string, send it.