Linux Serial Debugging

This post provides instructions for changing the group ownership and permissions of a serial port in Linux, as well as methods for sending and receiving data through the serial port. It also explains how to display serial port data in hexadecimal format using various tools and provides additional resources for reading serial port data in C++.

type
status
date
slug
summary
tags
category
icon
password

Serial port permission check

Group ownership change

To change the group ownership of the serial port /dev/ttyTHS2 from root to dialout, you can use the chgrp command in Linux. This command changes the group ownership of a file or a device to the specified group. You'll typically need to execute this command with superuser privileges, as it involves modifying device file properties.
Here's how you can do it:
  1. Open your terminal.
  1. Execute the following command:
    1. This command changes the group ownership of /dev/ttyTHS2 to dialout.
  1. Optionally, you can also change the permissions to allow the group to write to the device, as follows:
    1. This command grants read and write permissions to the group (dialout).
  1. Verify the changes by listing the device's details:
    1. The output should now show dialout as the group owner and should have the appropriate read/write permissions (crw-rw----).

Important Notes

  • Changing group ownership and permissions of device files should be done with caution, as it can affect system security and stability.
  • Ensure that the dialout group exists on your system. This is the standard group used for serial port access on most Linux distributions, but it may vary.
  • After changing the group ownership, make sure your user account is a member of the dialout group. You can add your user to the group with sudo usermod -a -G dialout $USER if you haven't already done so. Log out and log back in for this change to take effect.
  • These changes might be reset on system reboot, especially if your system uses udev rules to manage device files. If the changes do not persist after a reboot, you may need to create or modify udev rules for permanent changes.

Access permission change

To change the permissions of the serial port /dev/ttyTHS2 from -rw-r--r-- to crw-rw----, you'll be modifying the file permissions to allow both read and write access for the owner and the group, but no access for others. This is a common requirement for serial devices, where both read and write permissions are necessary for effective communication.
Here’s how you can change the permissions:
  1. Open your terminal.
  1. Execute the chmod command to change the permissions:
    1. In this command:
      • 6 (in 660) stands for read (4) + write (2) permissions for the owner.
      • The second 6 is for read + write permissions for the group.
      • 0 means no permissions for others.
  1. Verify the changes:
    1. After running the command, you can verify that the permissions have been updated correctly by listing the details of the device:
      The output should now show the permissions as crw-rw----.

Explanation of crw-rw----

  • c: This indicates that the file is a character device file, which is typical for serial ports and other similar hardware interfaces.
  • rw-: Read and write permissions for the file's owner.
  • rw-: Read and write permissions for the file's group.
  • --: No permissions for others.

Important Notes

  • Changing device permissions can have security implications. Ensure that this change aligns with your system's security policies.
  • These permissions might be reset upon system reboot, especially if your system uses udev rules to manage device files. To make permanent changes, you may need to create or modify udev rules.
  • Be cautious when granting write permissions to device files, and ensure that only trusted users have access to the group that owns the device.

Loopback transmit and receive test

To print data to a serial port in Linux, you can use various command-line tools. One of the most common tools for this purpose is echo combined with a redirection to the serial device. Another powerful tool is screen, which can be used for both writing to and reading from serial ports interactively. Here's how you can use these tools:

Using echo with Redirection

  1. Identify the Serial Port: First, you need to identify the serial port device. It’s usually named /dev/ttyS0, /dev/ttyS1, etc., for native serial ports, or /dev/ttyUSB0, /dev/ttyUSB1, etc., for USB-to-serial converters.
  1. Set Serial Port Settings: Before sending data, you might need to configure the serial port settings like baud rate, parity, and stop bits. You can use the stty command for this. For example, to set the baud rate to 9600:
    1. Send Data: To send data, use echo with output redirection:
      1. This command will send the string "Your data here" to /dev/ttyS0.

    Using screen for Interactive Communication

    screen can be used to interact with the serial port in a more interactive way.
    1. Install screen (if it's not already installed):
      1. Start a screen Session:
        1. This command will open a screen session to /dev/ttyS0 with a baud rate of 9600. You can type in the terminal to send data, and incoming data will be displayed.
      1. To Exit screen:
          • Press Ctrl-A, then Ctrl-\\. Confirm with y to quit.

      Notes

      • Permissions: Accessing serial ports usually requires certain permissions. You might need to run the commands as root (using sudo) or add your user to a group that has permission to access serial devices (like dialout on some systems).
      • Device Availability: Ensure that the device is available and not being used by another process.
      • Safety: Be cautious when sending data to serial ports, as incorrect settings or data can sometimes interfere with connected hardware.
      These methods are suitable for basic serial communication. For more advanced needs, consider using a scripting or programming language with a serial communication library.

      Display serial port data

      To display serial port data in hexadecimal format in Linux, you can use various tools and methods depending on the level of complexity and control you need. Below are some common approaches:

      1. Using xxd

      The xxd command creates a hex dump of a given file or standard input. You can pipe the output of the serial port into xxd:
      Or, for continuous monitoring:
      Here, stdbuf -oL ensures line-buffered output, allowing for real-time monitoring. Replace /dev/ttyS0 with your actual serial port.

      2. Using hexdump

      hexdump is another tool that you can use to display data in hexadecimal format:
      The -C option displays the output in a canonical hex+ASCII format.

      3. Custom Script or Program

      For more control, such as formatting or handling data, you can write a custom script or program. Here's a simple example in Python using pyserial:
      First, install pyserial if you haven't:
      Then, use the following script to read and display data in hex:
      This script reads available data from the serial port and prints it in hex format.

      4. Using screen or minicom with Logging

      If you are using screen or minicom, you can enable logging to a file and then view the file in hex format using xxd or hexdump.
      For screen, start a session with logging:
      For minicom, enable logging through the settings (minicom -s), and then start a session:
      The logged file can be viewed using:

      Notes

      • Replace /dev/ttyS0 and 9600 with the correct serial port and baud rate for your setup.
      • Make sure you have the necessary permissions to access the serial port. This might involve using sudo or adding your user to the dialout group.
      • Adjust configurations like baud rate and parity bits as per your device's requirements.
      • For real-time applications, buffering behavior of commands like cat should be considered. The stdbuf utility can be used to adjust buffering.

      Connect NX board to 1 gen flight controller

      FC Gen1: COM3 → NX COM
      FC Gen1: COM3: Sony Camera Protocol

      Appendix I

      Sample to read serial port data in C++ using poll
      Sample to read serial port data in C++ using interrupt
      Difference between polling and interrupt method for serial communication
      Loading...