SerialTool with Python®.


Table of Contents


Whats is Python?

Python® is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together.
Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
For more detailed information about Python®, please refer to their website: www.python.org

Legal notice: "Python" is a registered trademark of the PSF. SerialTool is not affiliated with or sponsored by PSF. SerialTool uses Python as a compatible programming language to run scripts and requires an official version of Python to be installed on your PC.

SerialTool and Phyton

Python Code Editor for SerialTool.


Have you ever wanted to use the serial port and have a fantastic graphical interface available that could display data to and from the serial port?
If you need this and want to create your own program to easily control the serial port without having to install libraries in a simple language accessible to everyone, SerialTool with Python is the tool you were looking for.
Some programs that use the serial port offer some scripting solutions, meaning you can write your code and have it executed, but very often you do not know the language or you have to spend hours figuring out how to integrate a few lines of your code! With SerialTool and embedded Python, you can write your script in Python and take advantage of all the features of SerialTool without any limits.
SerialTool integrates basic functions in the form of a library (already imported and integrated), and with a few lines, you can search for the desired serial port, open it, read, and write. SerialTool will handle all the graphic management of incoming and outgoing packets and allows you to use your Python code to perform the functions you desire without any limits.
To facilitate writing and testing in Python, SerialTool provides a text editor where you can upload and modify your scripts. The integrated editor recognizes Python syntax and allows for easy drafting of the code.

More in detail about serial port and Python

Delving deeper into the features offered by SerialTool and its integrated Python library, let's start with a practical example.
You intend to deliver to a client or a colleague software that allows the uploading of firmware on a device created by us and updatable via the serial port. A fairly common scenario in which one must create software from scratch, possibly multi-platform, and integrate a series of aspects starting from the graphical interface and user interaction, file selection, and error or confirmation messages.
In this case, unlike other languages, Python makes the work easy but at the same time requires external libraries to be installed and followed up on updates.
SerialTool solves the problem at its base by offering some simple functions that allow for quick and intuitive serial port selection, Dialog Boxes for file loading and saving, and Message Boxes to inform the user about the program execution.
These are very simple functions that are quickly called, native on the platforms where SerialTool can be installed (Windows, MacOS, and Linux). This completely eliminates dependencies on external libraries, and the basic functions of interaction with the user are made simple, fast, and intuitive in very few lines of code.

Returning to our basic example, we start by closing all active ports and by searching for available serial ports using the SerialTool.scan function and the user selection of the serial port to use with the SerialTool.gui_selectport function.
At this point, we know the port that our user wants to use, all we have to do is open it using the SerialTool.open function with the parameter settings or with parameters already existing in the GUI. Once the serial port is open, we can request the file to be loaded using the SerialTool.gui_loadfile function, open the file using the native functions in Python, and request confirmation for the execution of the firmware download with the SerialTool.gui_showmessage function.
We have reached the moment when we write the buffer present in our file with SerialTool.write indicating the number of bytes to write and wait for the response (if expected) from the device to be updated with the SerialTool.read command until the end of our update.
Few and simple commands that allow in a few minutes to write your own program to update your device or a third-party device.
Does it sound complicated? SerialTool provides integrated well commentented examples.

Integrated Examples for Serial Port in Python

To facilitate understanding and achieving results, SerialTool integrates directly functional and commented examples that promote the immediate use of the integrated functions. A huge advantage is given by the fact that the integrated functions do not require any external Python libraries to operate a serial port.
With the basic installation of Python, our example is already functional. In the examples provided, all functions available in the SerialTool library for Python integrated into our software are available. It will be enough to adapt some parts of the proposed code for the customization of your scripts and in a few minutes, your program for the serial port is developed.
The examples already proposed in SerialTool will be integrated version by version to always have a direct overview of the available functions and their implementation without having to search on the Internet.
Dedicated documentation on SerialTool functions is available on our SerialTool API for Python page.

Installation of Python

Many tutorials start with the installation of Python on your PC if it is not already available, and the configuration to be able to arrive at a minimum of usability. We have left this part for last because SerialTool through the Python Environment menu will search and test the existing configuration for you once Python is installed on your PC.
As mentioned, Python is a free and interpreted language and requires the interpreter installed on your PC. For those familiar with this language, there are not many explanations to give, however for those who should try for the first time this amazing language and its development environment, the steps to take are quite simple.
For Windows, just download the latest version of Python (3.12 at the time of writing this document) and run the executable until its complete installation.
For MacOS, Python should already be installed probably referring to version 2 of Python and it is necessary to update it to the latest version (3.12 at the time of writing this document) by downloading the executable directly from the official Python website and proceeding with the installation (as in Windows).

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 want to use a different version installed on your system, you can change the settings from the "Python Environment" menu.

For Linux, it will be necessary to install it via command line Ubuntu sudo apt-get install python3 or Fedora sudo dnf install python3
If you have any doubts or difficulties in installing Python, you can always refer to the official site or the dozens of tutorials available online.

Python Enviroment setup and configuration.


Once installed and verified the installation from the "Python Environment" menu, it will be enough to press the "Check Installation" button to import the current Python setting and save it for subsequent starts of SerialTool.

You are ready! Load the first examples and press the [RUN] button to start your adventure with SerialTool and Python writing your first program for the serial port.

Some additional information

To facilitate the writing of scripts, the incorporated editor has the typical features of an IDE but does not allow debugging for the moment.
Among the available options, it is advisable to always select the option "Automatically close serial ports when the script finishes execution." If your script encounters writing errors, SerialTool provides for the closure of any open serial ports to avoid having to close them at subsequent script startups.
To stop the script and exit, it is not advisable to use the sys.exit() function as it would cause the total closure of SerialTool as Python is integrated. If necessary to stop the script execution via source code, the SerialTool.stopscript function is provided.
If you find yourself inside a loop, it is possible to stop the execution via the [STOP] button directly from the GUI.
Since Python is directly integrated into SerialTool, it's important to remember that your script becomes part of the SerialTool processes. In this regard, when you include While True loops in your cycle, it is crucial to pair them with a small sleep (typically 0.1 seconds) using the time.sleep(0.1) (from time library) function to allow the various main GUI processes to be executed; otherwise, you risk completely freezing the application.
Do not worry excessively, as our examples highlight the critical points where to insert these small delays.

F.A.Q.

Is the integration of Python limited to some functionality?
NO. All functionalities of Python scripts are directly available including the loading of external libraries (if present in the system).

Does SerialTool and Python work on all operating systems?
YES. On all platforms where SerialTool is available and Python is available, its use is possible, i.e. Windows, MacOS, and Linux.

Do I necessarily have to download and install Python?
YES. The Python interpreter must be present on the PC where it is intended to be used. SerialTool is based on Python3 and backward compatibility with Python2 is directly dependent on the compatibility of the two versions. It is recommended for Python2 users to use Python3 in any case.

How does Python interact with SerialTool?
The Python script interacts through the SerialTool library directly integrated into our software by calling the desired functions. The parameters set via Python including opening and closing the serial port are directly linked to the main user interface. Essentially you pilot SerialTool directly from Python through the SerialTool library and its functions.

Can I continue to use the functions of SerialTool?
YES. All SerialTool functions remain available, AutoAnswer, Alarm, SerialScope, Packet display, Data Recording, Packet sending to the network, etc. are and remain available and selectable from the user interface.

Is it possible to use multiple ports simultaneously?
YES. The basic feature of SerialTool is the management of multiple serial ports simultaneously. From Python, it is possible to assign the serial number to be used to the serial port you intend to use via the SerialTool.assignPort function.

Do I always have to use the GUI to open and set the ports before use?
NO. As reiterated, the management of serial ports is directly available directly from Python.

Can I also set the RTS and DTR parameters?
YES. The library allows you to directly pilot the setting of RTS and DTR pins when opening the serial port and during script execution.

Can I request the implementation of other functions in the SerialTool library?
YES. Any further implementation of additional functions for the SerialTool library on Python will be taken into consideration and implemented, where possible and based on the development dynamics of SerialTool.

What kind of support can I have?
Our Team is available to help you integrate your scripts to facilitate the drafting of your scripts or identify any errors. Support is not included in the drafting of actual Python scripts or on commission. The support is related to the integration of Python with SerialTool.