Search by Tags

How to Use UART with Torizon Visual Studio Code Extension (Python)

 

Article updated at 17 Feb 2021
Compare with Revision




Select the version of your OS from the tabs below. If you don't know the version you are using, run the command cat /etc/os-release or cat /etc/issue on the board.



Remember that you can always refer to the Torizon Documentation, there you can find a lot of relevant articles that might help you in the application development.

Torizon 5.0.0

Introduction

In this article, we will show how you can manipulate UART using Visual Studio Code Extension for Torizon.

This article complies to the Typographic Conventions for Torizon Documentation

Prerequisites

Instructions

In the following sections, we will demonstrate how to use the VS Code Extension for Torizon.

Define the UART Device

In Linux systems, including TorizonCore, UART devices can be accessed through /dev/tty*. Toradex adds a layer of abstraction with symlinks, so the UART names are standardized for a given SoM family. Take a look to UART (Linux) article to understand the different /dev/tty* names across the Toradex's Module families.

Once you identify the exact name of your UART device, you can add it to your application.

Create a New Project

On VS Code, create a new Torizon Python Application. See the Python Development and Debugging on TorizonCore Using Visual Studio Code article for detailed instructions.


  • Python project

    Python project

Add UART Device to the Application

To add UART to your project, first, open the Torizon Configuration View. To do that, press F1 in Visual Studio Code command bar and then type "Torizon/Python: Focus on Configurations View"


  • Show the Configurations View

    Show the Configurations View

On the device item, press the '+' button on the right.


  • Add New Device to The Project

    Add New Device to The Project

On the same command box, type the selected tty device. As an example, we will add /dev/tty0.

Warning: /dev/tty0 is a ficticious example. Find your UART name on the previous section Define the UART Device.


  • Adding tty device

    Adding tty device

Check if the device was added to your project.


  • Project's Devices

    Project's Devices

Add Torizon User to dialout Group

By default UART devices are accessible only for users that belong to the dialout group, the torizon user is not part of that group in the base container configuration, but you can fix this by setting the "buildcommands" custom property to:

 RUN usermod -a -G dialout torizon

Examples

The serial port can be used with several programming languages and libraries, same as with any Linux distribution. This article focuses on Python.

Documented use cases are provided in the following subsections.

Connect to a Serial GPS

The example application used in this article comes from the example Python: Connect to a Serial GPS. Please read the following sections from it:

Add the UART Device to Project and Torizon User to dialout Group

Use the instructions from the beginning of this article to add the corresponding /dev/tty* to your project. On the Apalis iMX6, UART2 is accessible through /dev/ttymxc1. Remember to also add the user to the dialout group.

Prepare the Project's Files

This example is very simple. From your new empty project template, you only need to replace the content of 2 files: requirements.txt and main.py:

Run the Example

Press F5 to build and run the example.

Sample Output
Time = 07:53:31
Latitude = 3340.18707, N
Longitude = 07259.43225, E
.
.

Example Explained: The Project's Dockerfile

Read the Example Explained: The Project's Dockerfile.

Torizon 4.0.0

Introduction

In this article, we will show how you can manipulate UART using Visual Studio Code Extension for Torizon.

This article complies to the Typographic Conventions for Torizon Documentation

Prerequisites

Instructions

In the following sections, we will demonstrate how to use the VS Code Extension for Torizon.

Define the UART Device

In Linux systems, including TorizonCore, UART devices can be accessed through /dev/tty*. Depending on the UART hardware implementation, those devices can have different names. Take a look to UART (Linux) article to understand the different /dev/tty* names across the Toradex's Modules.

Once you identify the exact name for your UART device, you can add it to your application.

Create a New Project

On VS Code, create a new Torizon Python Application. See the Python Development and Debugging on TorizonCore Using Visual Studio Code article for detailed instructions.


  • Python project

    Python project

Add UART Device to the Application

To add UART to your project, first, open the Torizon Configuration View. To do that, press F1 in Visual Studio Code command bar and then type "Torizon/Python: Focus on Configurations View"


  • Show the Configurations View

    Show the Configurations View

On the device item, press the '+' button on the right.


  • Add New Device to The Project

    Add New Device to The Project

On the same command box, type the selected tty device. As an example, we will add /dev/tty0.

Warning: /dev/tty0 is a ficticious example. Find your UART name on the previous section Define the UART Device.


  • Adding tty device

    Adding tty device

Check if the device was added to your project.


  • Project's Devices

    Project's Devices

Add Torizon User to dialout Group

By default UART devices are accessible only for users that belong to the dialout group, the torizon user is not part of that group in the base container configuration, but you can fix this by setting the "buildcommands" custom property to:

 RUN usermod -a -G dialout torizon

Examples

The serial port can be used with several programming languages and libraries, same as with any Linux distribution. This article focuses on Python.

Documented use cases are provided in the following subsections.

Connect to a Serial GPS

The example application used in this article comes from the example Python: Connect to a Serial GPS. Please read the following sections from it:

Add the UART Device to Project and Torizon User to dialout Group

Use the instructions from the beginning of this article to add the corresponding /dev/tty* to your project. On the Apalis iMX6, UART2 is accessible through /dev/ttymxc1. Remember to also add the user to the dialout group.

Prepare the Project's Files

This example is very simple. From your new empty project template, you only need to replace the content of 2 files: requirements.txt and main.py:

Run the Example

Press F5 to build and run the example.

Sample Output
Time = 07:53:31
Latitude = 3340.18707, N
Longitude = 07259.43225, E
.
.

Example Explained: The Project's Dockerfile

Read the Example Explained: The Project's Dockerfile.