Serial Networking Devices  «Prev  Next»

Lesson 3Administering serial ports
ObjectiveConfigure and use Serial Ports

Administering Serial Ports

Administering, configuring, and using serial ports in Red Hat Enterprise Linux (RHEL) 9.2 involves several steps. Here are the general steps you need to follow:
  1. Identifying the Serial Port: The first step is identifying the serial port you wish to configure. Serial ports in Linux are represented as files in the /dev directory. They're typically named /dev/ttyS0, /dev/ttyS1, etc., with ttyS0 usually being the COM1 in DOS/Windows terms.
    To list available serial ports, you can use the command:
    # dmesg | grep tty
    
  2. Configuring the Serial Port: Once you've identified the serial port, you can configure it using the setserial command. This command allows you to set and report the configuration information associated with a serial port. For instance, you can use setserial to define the port's baud rate, parity, data bits, and stop bits.
    An example usage is as follows:
    # setserial /dev/ttyS0 baud_base 9600 spd_normal
    

    In this example, /dev/ttyS0 is the serial port, the baud_base is set to 9600 (standard baud rate), and spd_normal sets the port speed to the speed of the baud_base. The setserial command must be executed with root privileges, and changes made with setserial are not persistent across reboots. To make them persistent, you'll have to add the appropriate setserial command to a startup script, such as
    /etc/rc.d/rc.local.
    
  3. Using the Serial Port:
    To use the serial port, you can use a terminal emulator such as minicom, screen, or picocom.
    For example, to use minicom with /dev/ttyS0, you would use the command:
    # minicom -D /dev/ttyS0
    

    To use screen with /dev/ttyS0 at 9600 baud, you would use the command:
    # screen /dev/ttyS0 9600
    
  4. Checking Serial Port Communication:
    To check if data is being sent or received on a particular serial port, you can use the cat command to monitor the port:
    # cat /dev/ttyS0
    

    In another terminal, you can send data to the port:
    # echo "Test message" > /dev/ttyS0
    

    You should see the "Test message" output in the terminal where you ran the cat command. Remember that working with serial ports directly can affect your system. Always ensure that you have the appropriate system privileges and that you're certain about the changes you're making.
  5. Troubleshooting: If the serial port is not functioning as expected, ensure the port is not being used by another process. Use the fuser command to check for processes using the serial port:
    # fuser /dev/ttyS0
    

    These are the basics of administering, configuring, and using serial ports in RHEL 9.2. Depending on your specific use case and hardware, you may need to use additional commands and options. Always refer to the man pages (man setserial, man minicom, man screen, etc.) and the official Red Hat documentation for more detailed information.

Serial ports provide the interface between serial devices and the computer's I/O subsystem. On the Intel x86 architecture, there are usually two built-in serial ports. These normally correspond to /dev/ttyS0 and /dev/ttyS1, which are COM1 and COM2 under DOS and Windows. During system boot, Linux initializes these and two other ports: /dev/ttyS2 and /dev/ttyS3. The setserial command provides a method for configuring and examining the characteristics for a vast range of serial devices, including modems, mice, and serial terminals. The setserial command enables you to set data speed, the I/O port address, flow control, and other variables for each initialized serial port. It also enables you to define additional serial ports and to query the settings of existing ports.
For example, setserial /dev/ttyS0 produces something similar to the following output:

Set Serial Administration
/dev/ttyS0, UART: 16550A, Port: 0x03F8, IRQ: 4

The following serial port configuration:
  • Device: /dev/ttyS0
  • UART Type: 16550A
  • Port Address: 0x03F8
  • IRQ: 4

  1. Device being queried or set
  2. Universal Asynchronous Receiver Transmitter converts signal voltage to computer voltage and converts asynchronous input to synchronous output.
  3. The logical address for the serial port
  4. The interrupt channel given exclusively to this serial port


Serial Ports: No serial ports are assigned by default. You can assign any of the serial ports ($_com1 to $_com4) to a device such as a modem (/dev/modem), a mouse (/dev/mouse), or a terminal line (/dev/tty0).

Red Hat Reference

Using setserial Command

Use the setserial command to query the characteristics of a serial device. As you have seen, the command is easy to use.
You simply enter setserial followed by the device name.
How do I check and configure serial ports under Linux for various purposes such as modem, connecting null modems or connect a dumb terminal?
Linux offers various tools. Linux uses ttySx for a serial port device name. For example, COM1 (DOS/Windows name) is ttyS0, COM2 is ttyS1 and so on.
Task: Display Detected System's Serial Support
Simple run dmesg command
$ dmesg | grep tty

Output:
[   37.531286] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[   37.531841] 00:0b: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[   37.532138] 0000:04:00.3: ttyS1 at I/O 0x1020 (irq = 18) is a 16550A


Use the setserial command in Linux

The `setserial` command in Red Hat Linux (or any Linux distribution) is used to configure and query serial port settings. Here's a quick guide on using `setserial` effectively:
Installing the `setserial` Command
1. Check if `setserial` is already installed:
   which setserial
   
If it's not installed, you can add it using `yum` or `dnf`:
   sudo dnf install setserial
   
Syntax The basic syntax for `setserial` is:
setserial [options] 

Common Use Cases
  1. 1. Query Serial Port Settings

    To check the current settings of a serial port (e.g., /dev/ttyS0):

    setserial /dev/ttyS0
        
  2. 2. Set Serial Port Parameters

    You can set parameters like IRQ, UART type, and baud base. For example:

    sudo setserial /dev/ttyS0 irq 4 uart 16550A port 0x3F8
        
    • irq 4: Sets the IRQ number to 4.
    • uart 16550A: Specifies the UART type.
    • port 0x3F8: Configures the I/O port address.
  3. 3. Use Auto-Detection

    To automatically detect and configure the serial port:

    sudo setserial /dev/ttyS0 auto_irq autoconfig
        
  4. 4. Save Settings Permanently

    setserial changes are temporary and will be reset after a reboot. To make them permanent:


    1. 1. Add the setserial command to a startup script (e.g., /etc/rc.d/rc.local):

      echo "setserial /dev/ttyS0 irq 4 port 0x3F8 uart 16550A" | sudo tee -a /etc/rc.d/rc.local
          
    2. 2. Ensure the script is executable:

      sudo chmod +x /etc/rc.d/rc.local
          

Tips
  • Ensure you have the correct permissions to modify serial port settings. Use sudo if needed.
  • Incorrect settings can make the serial port unusable. Always verify your hardware's specifications.
  • To debug, you can use dmesg to check kernel messages:
  dmesg | grep ttyS
  
This should help you use the `setserial` command effectively on Red Hat Linux 9.2.
In the next lesson, the roles serial modems play in the Linux OS will be defined.

SEMrush Software 3 SEMrush Banner 3