Serial Port and Python Scripts

Introduction to Python and Serial Port

I'll start by saying that after years of programming, my favorite language is C/C++, even though I am proficient with all desktop and web programming languages. I also have to acknowledge that Python is today one of the simplest, most efficient, and intuitive interpreted languages available. Some of you or many of you might disagree and might prefer Java among interpreted languages. However, for the speed of writing code and ease of learning, I believe Python has no rivals.

I wanted to make this preamble because Python is powerful, it is cross-platform, but it also hides some small pitfalls. Using Python with the serial port is typically done with the PySerial library, which is not included in the base installation of Python and needs to be imported. It is still a programming language, and for the graphical user interface part, you always have to improvise a bit.

This is where SerialTool and Python comes in and makes life easier. If you want to write your own program for the serial port without complicating things with graphical libraries, packet visualization, data sending, and all that entails, SerialTool combined with Python is the answer.

Let's start by saying that it is the only program (at the time of writing this document) that supports Python scripting. Other software I have tried uses a wrapper with JavaScript (not Java) or VBA. Regarding JavaScript, I don't think it's ideal since it is mostly used for the client-side part of web-based applications. I understand that since there are libraries that interpret JavaScript in C, it was the quickest choice to claim that their serial port software uses scripting through a JSCPP library. As for VBA (Visual Basic for Applications), I think it is overly outdated and anachronistic to still discuss it.

That said, Python is truly the ideal language for integrating scripting within an application. Let's make a preface on how SerialTool integrates Python for detail lovers. Python allows itself to be embedded within software written in C++ to execute arbitrary code, essentially running a program within another program. To read more about this, you can refer to Python Embedded at this link.

I believe the developers of SerialTool have embraced this philosophy, which allows any Python script to be launched from their application. There remains a problem of creating a bridge between SerialTool and Python. Analyzing the official documentation of SerialTool and Python, it is clear that they have developed a specific library in Python for managing the serial port (different, of course, from PySerial), which offers some function calls present in SerialTool. Essentially, it is a real library that needs to be imported (but is already integrated into the main software) and provides some functions that allow the visualization of serial port traffic directly in SerialTool, thus eliminating the need to create your own graphical interface.

Having said all this, what is the purpose of doing this and who might need Python within a serial port program? The sharpest among you might have already guessed where I am going, but let's get there together step by step. SerialTool already offers a wide range of functions, but for example, if we want to write our own program that performs specific computations upon receiving a specific buffer from the serial port, we cannot do it. Let's imagine a practical example of sending a file via the serial port. Sure, SerialTool already makes this possible, but we want to create our own custom program. In this case, we can open the serial port with the SerialTool library, load our file directly in Python, and send it through the serial port opened with SerialTool to our remote device.

In this way, we will have created our very own Python program that uses SerialTool as the graphical interface and manages the serial port. All this quickly, using the extensive documentation available for Python file management, and in a heartbeat, we have our script ready, which we can share with colleagues or publish directly. Clearly, Python is available for Windows, MacOS, and Linux, so we don't have to worry about the operating system since SerialTool is a cross-platform software just like Python.

Personally, among the various functions that SerialTool offers, I believe this is one of the most interesting, if not the most interesting. It is certainly unique and not found in other serial port software. For schools, universities, and hobbyists, the combination of Python with SerialTool is definitely a game-changer because it allows you to quickly write software from scratch for the serial port using the graphical interface of SerialTool.

I was about to omit that your Python script allows you to simultaneously use other functionalities of SerialTool, such as alarms or sending serial port traffic over TCP. From what I have understood and tried, the key point is that every packet sent or received using the SerialTool library in Python is converted into a packet and managed as such by the main interface. For this reason, all the special functions of SerialTool remain available simultaneously, so we effectively have full control of SerialTool's graphical interface as a real working tool.

Isn't it an amazing or an everglow?

SerialTool and Python

Python Editor

 

As a developer who has extensively used SerialTool, I can confidently say that the Python script editor integrated into SerialTool is a remarkable feature that significantly enhances the user experience. It's user-friendly, professional, and designed with developers in mind, making it an indispensable tool for anyone working with serial communication. When you first open the Python script editor in SerialTool, you immediately notice its clean and intuitive interface. The editor is designed to be straightforward, allowing even those new to Python scripting to get started quickly. The layout is simple, with a large script window that gives you plenty of space to write and edit your code. This is particularly beneficial when working on complex scripts, as you don't feel cramped or restricted.

The script editor also includes a Python Output window, which is incredibly useful for debugging and monitoring your script's execution. As you write your code, you can see real-time feedback and output directly within SerialTool, which means you don't have to switch between different applications or terminals. This seamless integration saves time and reduces the cognitive load, allowing you to focus entirely on your development work.

One of the standout features of this editor is the ability to load and save scripts effortlessly. You can easily save your work and load existing scripts, which is essential for iterative development and testing. This functionality ensures that you can manage your scripts efficiently, keeping your workflow smooth and uninterrupted. Additionally, the font size adjustment is a small but thoughtful feature that adds to the overall user experience. It allows you to customize the editor to your comfort, which is crucial when spending long hours coding. The ability to increase or decrease the font size helps reduce eye strain and makes the editor more accessible.

The editor is also designed to be robust and professional, catering to the needs of both novice and experienced developers. The comprehensive documentation and comments provided in the example scripts are immensely helpful. They guide you through the process of writing your own scripts, explaining the functions and methods used in SerialTool. This is particularly beneficial for those who might be new to Python or to SerialTool's specific libraries.

Another impressive aspect is the integration of SerialTool's powerful features within the Python scripts. For instance, you can use the script to scan and assign serial ports, making the setup process more automated and efficient. The example script shown in the editor demonstrates how to select a serial port after scanning all available ports. This kind of functionality is invaluable when developing and testing serial communication applications, as it simplifies and accelerates the process.

Moreover, the editor includes an important note about using time.sleep(0.1) to allow SerialTool to process other messages. This kind of detail shows how deeply integrated the Python scripting is with the overall functionality of SerialTool. It ensures that your scripts run smoothly without causing the GUI to freeze, which is crucial for maintaining a productive development environment.

In conclusion, the Python script editor in SerialTool is a game-changer for anyone working with serial communication. Its user-friendly design, professional features, and seamless integration with SerialTool's functionalities make it an essential tool for developers. Whether you're writing simple scripts or developing complex applications, this editor provides all the tools you need to be productive and efficient. It's not just a script editor; it's a comprehensive development environment tailored for serial communication, and it has significantly improved my workflow and productivity.

But now let's go more into details….

Serial Port and Python Examples

SerialTool comes with a set of integrated Python script examples that are designed to make it easier for users to manage the serial port and interact with the graphical user interface (GUI). These examples demonstrate how to perform various tasks such as loading files, displaying pop-ups, and handling confirmation dialog boxes, all within the SerialTool environment.

Integrated Examples

These Python script examples are particularly useful for both beginners and experienced developers. They provide a practical starting point for understanding how to leverage SerialTool’s capabilities. For instance, one of the examples showcases how to scan for all available serial ports and select one for communication. This is done using the SerialTool.scan function to identify all ports, followed by SerialTool.list to display them, and then SerialTool.assign to assign the selected port. This workflow is crucial for setting up serial communication in a structured and efficient manner.

Example Code

To give you a glimpse of what these scripts look like, here is an excerpt from one of the example scripts:

# ===============================================================================
# @@@@@@@@
# @ @@@@@@@ ____ _ _ _____ _
# @@@@@@@@@@ / ___| ___ _ __(_) __ _| |_ _|__ ___ | |
# @@@@@@@@@@@@@@.... \___ \ / _ \ `__| |/ _` | | | |/ _ \ / _ \| |
# @@@@@@@@@@@@@@@..... ___) | __/ | | | (_| | | | | (_) | (_) | |
# @@@@@@ ....... |____/ \___|_| |_|\__,_|_| |_|\___/ \___/|_|
# @@@@@...............
# @@@@.............. SerialTool Python Embedded Script
# .......... (C) 2024 by SerialTool Dev Team
# ....... . www.serialtool.com
# ......
# ===============================================================================
#
# Script: Basic Script
# Script Version: 1.0.0 dated April 27th, 2024
# Author: SerialTool Dev Team
#
# Description:
# This script demonstrates how to select a serial port after scanning for all available serial ports.
# It uses the function SerialTool.scan to scan all ports, which automatically unassigns all previously assigned SerialPorts.
# After listing all serial ports using the function SerialTool.list, it assigns the selected serial port using the function SerialTool.assign.
#
# IMPORTANT NOTE: Remember to always use time.sleep(0.1) to allow SerialTool to process other messages.
# Otherwise, the GUI will completely freeze and be unable to handle any user actions, requiring you to
# force the application to exit from an external operating system command or action.
#
# Functions used in this script:
#
# SerialTool.scan
# SerialTool.list
# SerialTool.gui_showmessage
# SerialTool.gui_selectport
# SerialTool.assignPort
# SerialTool.stopscript
# SerialTool.open (with parameters)
# SerialTool.write
# SerialTool.close
#
import SerialTool # Always import internal SerialTool library
import time # Always import time to execute time.sleep function
import sys
SerialTool.init() # Always init SerialTool library before any other action.
# Show Library Version
version = SerialTool.version()
print("SerialTool Python library version:", version)
# Scan Serial ports. This function scans all ports and automatically unassigns and closes all previously assigned SerialPorts.
SerialTool.scan()
# List Existing Serial ports function
SerialPorts = SerialTool.list()
num_ports, ports_list = SerialPorts
# Print Serial ports details
if num_ports > 0:
# Pop up a blocking Information Message Box
messageType = 1
result = SerialTool.gui_showmessage ("Available Serial Ports", "There are " + str(num_ports) + " Serial Ports avalable.\r\n\r\n" , messageType)
# Print ports detials
print(f"Ports Found {num_ports} list:\n")
# Assuming 'return_string' is a byte string, decode it to a regular string
return_string = ports_list.decode('utf-8')
# Split the string at each '|' character to get a list of items
items = return_string.split('|')
# Iterate over the list of items and print each one
for item in items:
# COM String
print(item)
# Split each item at the ',' character to get its components
components = item.split(',')
# Create a formatted string for the desired result
formatted_result = "Assigned Port:{}, Port Name: {}, Baudrate: {}, Bits: {}, Parity: {}, StopBits: {}, Opened: {}".format(
components[0].strip(), components[1].strip(), components[2].strip(), components[3].strip(), components[4].strip(), components[5].strip(), components[6].strip())
# Print the formatted result
print(formatted_result)
print("")
else:
# Pop up a blocking Critical Message Box
messageType = 3
result = SerialTool.gui_showmessage ("No ports", "There are no Serial Ports available on this PC.\r\n\r\nScript is terminated." , messageType)
print(f"No ports")
SerialTool.stopscript() # gently exit if no port is availale

#select port to operate
result = SerialTool.gui_selectport()
# Wait until we get the respose and catch it
while result == -1:
time.sleep(0.1) # Sleep for a short interval to allow GUI to process other messages
# Unpack the tuple
SelectedPortPosition, SelectedPortName = result
# Print the values
if SelectedPortPosition >= 0:
assignedValue = 1
print(f"Selected port in position {SelectedPortPosition}: {SelectedPortName}")
# Convert Bytes to string
PortName = SelectedPortName.decode('utf-8')
# assign selected port to serial 1
result = SerialTool.assignPort(PortName, assignedValue)
if result == 1:
print(f"Serial Port {PortName} assigned to serial {assignedValue}")
else:
print(f"Port assignment error: {result}")
else:
print(f"No port selected. Exiting from script")
SerialTool.stopscript() # gently exit if no port is availale

# Open Serial Port with parameters (Assigned Port Number, Baudrate, Bits, Parity, Stop Bits, Timeout, DTR, RTS, Flowcontrol)
# Flowcontrol in Windows can be: 0 - None, 1 - Xon/Xoff, 2 - RTS/CTS, 3 - DSR/DTR
# Flowcontrol in Linux/MACOS can be: 0 - None, 1 - Xon/Xoff, 2 - RTS/CTS
result = SerialTool.open(assignedValue, 9600, 8, "N", 1, 50, 1, 1, 0) # Open with settings
time.sleep(0.1)
if result == 1:
print(f"Port {PortName} assigned to serial {assignedValue} opened successfully.")
else:
print(f"ERROR {result}: {PortName} not opened. Exiting script.")
SerialTool.stopscript() # gently exit if no port is availale

#Write to Serial Port
BufferToSend = "Hello World!\n"
result = SerialTool.write(assignedValue, BufferToSend, len(BufferToSend))
if result > 1:
print (f"{result} bytes sent from serial port {PortName} assigned to serial {assignedValue}")
else:
print ("Error sending bytes.")

# remember to close the serial port before exiting
SerialTool.close(assignedValue)
print ("Done.")

GUI Management

Another example focuses on managing the GUI with Python scripts. It shows how to display pop-up messages and capture user responses without relying on external libraries like tkinter. This feature is incredibly useful for creating interactive applications that require user input. The script demonstrates how to use SerialTool.gui_showmessage to display messages and handle user clicks, providing a native way to interact with the user directly from your script.

File Handling

SerialTool also offers examples that handle file loading and saving dialogs. These scripts use the SerialTool.gui_loadfile and SerialTool.gui_savefile functions to open native file dialogs. While these functions return the selected filename, the actual loading or saving of the file can be implemented in your Python code. This integration simplifies file management tasks and avoids the need for additional Python libraries.

Serial Port Control

For more advanced users, there are scripts that demonstrate how to control serial ports directly. These include opening and closing ports, reading from and writing to ports, and setting or clearing DTR and RTS signals. These examples are essential for developers who need precise control over serial communication and hardware interaction.

Practical Use

Personally, I’ve found these integrated examples to be incredibly helpful. They serve as a solid foundation for building more complex scripts and applications. By providing clear and concise examples, SerialTool makes it easy to understand and implement serial communication tasks. The ability to script these interactions using Python, combined with SerialTool’s robust features, offers a powerful toolkit for any developer working with serial ports.

In summary, SerialTool’s integrated Python script examples are a fantastic resource. They simplify the process of setting up and managing serial communication, handling GUI interactions, and managing files, all within a cohesive and user-friendly environment. These examples not only help you get started quickly but also provide the flexibility to extend and customize your scripts to suit specific project needs.

Python Editor and Scription Settings

Python editor Settings

 

The Python editor within SerialTool is packed with features that streamline the coding process. For instance, it comes with auto-parenthesis and auto-indentation functionalities that help maintain clean and readable code structure. These features ensure that your scripts are neatly organized, reducing the likelihood of syntax errors and making your code easier to debug.

Another notable feature is the ability to replace tabs with spaces, with a customizable tab size setting. This is particularly useful for maintaining code consistency, especially when collaborating with others who might have different indentation preferences. The code autocompleter and code highlighter further enhance the coding experience by providing real-time suggestions and syntax highlighting, making it easier to write and understand complex scripts.

On the scripting side, SerialTool offers several practical settings that enhance the execution and management of your scripts. One of the standout features is the option to automatically save your script upon running it. This ensures that you don’t lose any changes and can easily revert to the latest version of your code. Additionally, the tool automatically closes serial ports when the script finishes execution, which helps in preventing resource leaks and potential conflicts with other applications.

A particularly useful feature is the error-handling setting that stops the script in case of errors related to opening or closing serial ports. This proactive error management helps in quickly identifying and addressing issues, ensuring smoother script execution and debugging.

Overall, the Python editor and scripting options in SerialTool provide a robust environment for developing and managing serial communication scripts. These features collectively make the process more efficient, intuitive, and error-free, significantly enhancing productivity and enabling the creation of sophisticated serial communication solutions.

SerialTool API for Python

I’ve found its Python API library to be an incredibly powerful feature that enhances both flexibility and functionality when working with serial ports. The integration of Python scripting into SerialTool allows for seamless control and automation of serial communication tasks, making it an invaluable tool for anyone needing precise and programmable interaction with serial devices.

The Python API for SerialTool is designed to be user-friendly and intuitive. It provides a range of functions that allow you to manage serial ports directly through your Python scripts. This includes initializing the library, scanning for available ports, opening and closing ports, and performing read and write operations. For instance, you can easily open a serial port, send data, and read the response all within a Python script. This makes it simple to integrate serial communication into larger automation workflows or custom applications without needing extensive setup.

One of the standout features is the ability to control serial port signals such as DTR and RTS, and to handle advanced configurations like setting baud rate, data bits, parity, and stop bits directly through the API. This level of control is crucial for developing robust applications that need to interact with various hardware devices over serial connections.

Moreover, the embedded Python environment in SerialTool comes with a built-in editor that supports syntax highlighting and real-time script execution. This editor allows you to write, test, and debug your scripts within the same interface, streamlining the development process. The editor’s integration with SerialTool means you can leverage the tool’s graphical interface to visualize incoming and outgoing data packets, making debugging more intuitive.

Using Python with SerialTool eliminates the need for external libraries for serial communication, as everything is built into the tool. This not only simplifies the setup process but also ensures compatibility across different operating systems, including Windows, MacOS, and Linux. The provided example scripts and comprehensive documentation further reduce the learning curve, enabling you to quickly get up and running with your projects.

For more detailed information on the Python API and its functions, you can visit SerialTool’s official Python API documentation page. This resource offers in-depth descriptions and examples of how to use the various functions provided by the library, making it easier to implement and troubleshoot your Python scripts.

Python Installation

To fully utilize the powerful features of SerialTool, you need to have Python installed on your Windows or Linux PC. The installation process is straightforward and can be completed in just a few steps, ensuring you can start scripting and automating your serial communication tasks with ease.

Installing Python on Windows and Linux

For Windows:

  1. Download Python Installer: Visit the official Python website at python.org and download the latest Python installer for Windows.
  2. Run Installer: Launch the downloaded installer. During the installation, make sure to check the box that says “Add Python to PATH” to ensure Python can be accessed from the command line.
  3. Complete Installation: Follow the on-screen instructions to complete the installation.

For Linux:

  1. Update Package Lists: Open a terminal and update your package lists using sudo apt update (for Debian-based distributions like Ubuntu) or sudo dnf update (for Red Hat-based distributions like Fedora).
  2. Install Python: Install Python using your package manager. For instance, on Ubuntu, you would run sudo apt install python3. On Fedora, use sudo dnf install python3.

Note for MacOS Users

In the current version of SerialTool for MacOS, the Python 3.12 framework is already integrated to comply with Apple's requirements regarding code signing. If you prefer to use a different version of Python installed on your system, you can change the settings from the “Python Environment” menu.

 

 
SerialTool Python Installation Environment

 

With SerialTool, you can easily verify if Python is successfully installed directly within the application. This convenient feature ensures that you can check your Python environment without needing to switch between different tools or command lines.

To check if Python is installed correctly:

  1. Open SerialTool and go to Python Icon navigate to the “Python Environment” settings.
  2. View the Discovered Python Version: The application will display the version of Python that it has detected on your system. For instance, in the screenshot, Python 3.11 is shown.
  3. Check the Python Install Path: This field shows where Python is installed on your system, helping you ensure that the path is correct.
  4. Verify Python Environment Paths: The list of paths included in the Python environment is displayed. These paths are essential for Python to locate its libraries and dependencies.
  5. Test Python Installation: By clicking the “Test Python” button, SerialTool will run a test to confirm that Python is functioning properly. This test can help you troubleshoot any issues with your Python setup.

Additionally, you can check and configure the installation by using the “Check Installation” button, which will reassess the Python setup and update the details if necessary. You can also add or modify the paths using the “Add Paths” button and save the current configuration for future use.