Differences Between Serial Ports on Linux and Windows
A serial port is an interface that allows external devices to communicate with a PC. For example, older devices such as mice and modems use this port for connections. In Windows, this interface is referred to as a “COM port,” but in Linux, it is handled differently and is identified with names like /dev/ttyS0
as part of the /dev/tty
system. Understanding this distinction is key to properly managing serial devices connected to a Linux system.
Software for Operating Serial Ports
To operate a serial port in Linux, a terminal software is used. One example is gtkterm, a lightweight serial terminal application, but there are other tools available as well. Below are some common options:
- minicom: A more advanced tool for serial communication, offering features like log saving and detailed communication settings.
- gtkterm: A lightweight software ideal for basic serial port operations. It is relatively easy to use, even for beginners.
What is /dev/tty
?
In Linux, every device is treated as a “file.” This means that devices are recognized in the file system as if they were files. Serial ports are no exception and exist under names such as /dev/ttyS0
or /dev/ttyUSB0
.
For example, /dev/ttyS0
refers to the first serial port, while /dev/ttyUSB0
is for USB-connected serial devices. By writing data to these files, communication with devices through the serial port becomes possible. A simple diagram could look like this:
[PC] --- COM Port (COM1, COM2 on Windows)
|
----> [Linux] --- /dev/ttyS0 (Serial Port 1)
/dev/ttyUSB0 (USB-connected serial device)
Permission Error: “cannot open /dev/ttyS0”
When you try to run the installed software, an error may occur indicating that there is no permission:
cannot open /dev/ttyS0: Permission denied
Error Explanation: “cannot open /dev/ttyS0”
This error occurs when the system does not have the necessary “permissions” to access the serial port. In Linux, accessing important files or devices requires “administrator privileges.” Since /dev/ttyS0
is a serial port device, access is restricted for regular users.
To resolve this, you need to grant permissions that allow anyone to access the serial port. You can do this by running the following command:
sudo chmod 666 /dev/ttyS0
Why is sudo
necessary?
sudo
is a command used to execute commands with “administrator privileges.” In this case, administrator privileges are required to allow access to the serial port. The command chmod 666
grants “read and write permissions to all users” for the device file.
Once this is done, the error is resolved, and the connection is successful. Since this PC has three serial ports, let’s check their status. To be thorough, we will check for additional serial ports by incrementing the numbers.
Checking the Serial Port Status
To check the status of the serial ports, use the setserial
command. This command displays information about the serial ports. If setserial
is not installed, you can install it by running the following command:
sudo apt install setserial
After installation, run the following command to check the status of the serial ports:
setserial -g /dev/ttyS[0123]
How to Maintain Permissions After Reboot
When you reboot the system, the permissions for the serial ports will reset. To automatically apply the permissions after each reboot, you can add the following commands to a configuration file.
- Edit the
/etc/rc.local
file (administrator privileges are required).sudo nano /etc/rc.local
- Add the following commands at the end of the file:
chmod 666 /dev/ttyS0 chmod 666 /dev/ttyS1 chmod 666 /dev/ttyS2
- Save the file and exit.
With this setup, the permissions for the serial ports will persist even after a reboot.
Using Wine to Run Windows Software on Linux
Next, we will use Wine to run Windows software on Linux. In this example, we will install and verify the functionality of TeraTerm, a serial communication software for Windows.
What is Wine?
Wine is a useful tool that allows you to run Windows software on Linux. With Wine, you can install and use software that is normally only available on Windows. For more details about Wine, feel free to check out my blog where I explain Wine in detail.
Installing TeraTerm and Verifying Serial Ports
Once TeraTerm is installed using Wine, the serial ports (COM ports) will appear as “com1” or “com2,” just as they would on a Windows system. This is because Linux serial ports (such as /dev/ttyS0
) are mapped to Windows-style COM ports within the Wine environment.
Verifying Serial Port Configuration
Wine allows Linux serial ports to be recognized as COM ports. You can check the specific configuration by navigating to the following path within your home directory:
~/.wine/dosdevices
In this directory, you will see Linux serial ports (e.g., /dev/ttyS0
) mapped to Windows COM ports (e.g., com1
). For instance, /dev/ttyS0
is mapped to com1
, and you can use it just as you would on a typical Windows system.
Mapping of ttyS0 to com1
ttyS0
is mapped to com1
, and because there is no com0
, the port numbers are shifted by one.
Verifying Communication Using Wine and gtkterm
Finally, we verified that communication through the serial port is possible using TeraTerm, which was installed via Wine, and gtkterm
, which was installed on Linux. By combining software from both Linux and Windows environments, serial communication can be achieved.