[TUTORIAL]How to use UART on LattePanda - NFC reader example

userHead tyjjr 2017-02-20 17:41:45 5356 Views1 Replies
LattePanda onboard Arduino Leonardo compatible micro controller(ATmega32U4) has a hardware UART connector(pin0 (RX) and pin1 (TX)), you can easily use them to receive, transmit data and transport it to LattePanda serial port, it can be accessed by any development platform which provide serial API.

This tutorial will show you how to use the UART connector in LattePanda, for a clearer demonstrate, i will use a NFC Module to demonstrate the entire process of using UART.

This tutorial will also show you how to use Serial API in C# .NET, you can use it to communication with any Microcontrollers which support serial function.

Hardware prepare
1. LattePanda
2. NFC Module and NFC card
3. DuPont cable

Please refer to the image below to connect the NFC Module and LattePanda FullSizeRender.jpg FullSizeRender.jpg (57.73 KiB) Viewed 4498 times Arduino passthrough
1. Arduino Leonardo has two way of hardware Seral port, Serial and Serial1, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 (RX) and 1(TX), use the Serial1 class, in this example, we will use Serial1 to connect with NFC Module.
2. before you start to reading this section, make sure your lattepanda have installed Arduino IDE.
3. the only thing you need to do is receive data from one UART and send it to the other. Copy the code below to your Arduino IDE, make sure you have chose the right Serial Prot and Board(Arduino Leonardo) on Tools, and upload it to the arduino.

Code: Select all

void setup() { Serial.begin(115200); // open serial with PC Serial1.begin(115200);//open serial1 with device } void loop() { while(Serial.available()) { Serial1.write(Serial.read()); } while(Serial1.available()) { Serial.write(Serial1.read()); } } C# code and demo app
This scetion will show you how to use NFC Module to read a NFC card via Serial API of .NET. It will be very easy if you use it on another development platform. You can download the Demo app for test or following the guide below to build your own project. UartPassthrough.zip (19.54 KiB) Downloaded 205 times 1. Open Visual Studio and build a new Windows Forms Application.
2. Double click Form1.cs to open the design window and add two buttons and a label on it. Demo.png Demo.png (13.38 KiB) Viewed 4498 times 3. Open the code of Form1.cs and change it to these code below.

Code: Select all

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //add framework for Serial and Delay api using System.IO.Ports; using System.Threading; namespace UartPassthrough { public partial class Form1 : Form { public SerialPort _serialPort; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Open Serial Port, you can fine the serialPortName of your arduino in device manager. serialOpen("COM7", 115200, 8000); } //Create a function for open serial port. public bool serialOpen(string serialPortName, Int32 baudRate,int delay) { _serialPort = new SerialPort(serialPortName, baudRate); _serialPort.DataBits = 8; _serialPort.Parity = Parity.None; _serialPort.StopBits = StopBits.One; try{ _serialPort.DtrEnable = true; _serialPort.Open(); Thread.Sleep(delay); return true; } catch { return false; } } //Create a function for send data and wait for reply. public int[] sendCommand(byte[] command, int timeForDelay) { try { _serialPort.Write(command, 0, command.Count()); Thread.Sleep(timeForDelay); int[] returnData = new int[_serialPort.BytesToRead]; for(int i = 0;i< returnData.Count();i++) { returnData[i] = _serialPort.ReadByte(); } return returnData; } catch { return new int[0]; } } //Send data to serial to wake up the NFC Module private void button1_Click(object sender, EventArgs e) { byte[] message ={0x55,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,0x03,0xfd,0xd4,0x14,0x01,0x17,0x00}; int[] returnData = sendCommand(message, 100); label1.Text = "result: "; for (int i = 0; i < returnData.Count(); i++) { label1.Text += returnData[i].ToString() + ", "; } } //Send data to read NFC card tag. private void button2_Click(object sender, EventArgs e) { byte[] message = { 0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x01, 0x00, 0xE1, 0x00 }; int[] returnData = sendCommand(message, 100); label1.Text = "Tag: "; for (int i = 0; i < returnData.Count(); i++) { label1.Text += returnData[i].ToString() + ", "; } } } } 4. Click Start to run the project, click Wake Up to init the NFC module, you will see the result in Label1. Put a NFC card in the NFC module and clike Read Tag, the tag of the card will show in Label1. Demo.png Demo.png (13.38 KiB) Viewed 4498 times Resources: UartPassthrough.zip (19.54 KiB) Downloaded 205 times