Search by Tags

How to Use UART with Torizon Visual Studio Code Extension (.NET Core)

 

Article updated at 06 Jul 2020
Subscribe for this article updates

Introduction

In this article, we will show how you can use the UART interface, also known as serial, with .NET Core and the Visual Studio Code Extension for Torizon.

The goal is to create a simple application using .NET Core Console template showing the data returned from a serial UART GPS.

Prerequisites

Instructions

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.

Connect to a Serial GPS

For this example the Ublox Neo 6M, GY-NEO-6MV2, GPS module was used:


  • Ublox Neo 6M

    Ublox Neo 6M

Connect the UART of the GPS Module to an available UART on your board.

Board            GPS
VCC  <-------->  VCC
RX   <-------->  TX
TX   <-------->  RX
GND  <-------->  GND

For the purposes of example in this article, the Apalis iMX6Q and Ixora carrier board will be used. For connect GPS module we will use UART4 signals from Ixora header X27:


  • GPS Module connected to Ixora UART4

    GPS Module connected to Ixora UART4

Create a New Project

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


  • .NET Core Project

    .NET Core Project

Add UART Device to the Project

To add UART to your .NET Core Torizon project, first, open the Torizon Configuration View. To do that, press F1 in Visual Studio Code command bar and then type "Torizon: 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

Type the selected tty device. As an example, we will add /dev/apalis-tty4.

Warning: /dev/apalis-tty4 is UART4 in the case of the Apalis family. 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 Serial Access Permission

To have access to the serial devices we need to add the default torizon user to dialout group. Go to the Torizon Visual Studio Code Extension Configurations and add the buildcommands:

RUN usermod -a -G dialout torizon 

  • Add torizon user to dialout group

    Add torizon user to dialout group

Add Nuget Packages

To easily parse NMEA data from GPS module, we use the SharpGIS.NmeaParser. To add it to the project open a new terminal using the Visual Studio Code and enter the following command:

dotnet add package SharpGIS.NmeaParser

  • Dotnet add Nuget package

    Dotnet add Nuget package

Source Code

The example code used in this documentation comes from the sample https://github.com/toradex/torizon-samples/tree/master/gps/dotnet/console/

Set SerialPortDevice.PortName

Change the line 18 to:

            serialDev.PortName = "/dev/apalis-tty4";

Warning: Remember to modify the device path according to the serial device you are going to use.

With the correct device set you can now run and debug the application. See the .NET Core Development and Debugging on Torizon Using Visual Studio Code article for detailed instructions about debug tools.