layout | title | permalink |
---|---|---|
code |
Writing to UART |
/UART.htm |
Learn how to use HardwareSerial to read and write to the UART port.
- HardwareSerial defines an object called Serial1.
- This reads and writes to COM2 on the Windows Image which is linked to the UART port on the Galileo board.
- This reads and writes to COM2 on the Windows Image which is linked to the UART port on the Galileo board.
- While this sample doesn't use a serial event, if you want to use serialEvent1, you will need to:
- Create a method for
serialEvent1()
that returns void and has no parameters in your main.cpp. This method will be called when data is available on the serial1 port at the end of the loop. - Right click on the Project in the Solution Explorer, then select Properties.
- Under Configuration Properties -> C/C++ -> Preprocessor, add SERIAL_EVENT1; to Preprocessor Definitions.
- Create a method for
- DB9 Female to 3.5mm Cable{:target="_blank"}
- USB to Serial Cable{:target="_blank"}
- Shut down Galileo and remove power
- Remove microSD card and plug it in to a PC (Windows will automatically assign a drive letter, in our case it was "k")
- Open an Administrative command prompt on your development machine:
- bcdedit /store k:\efi\microsoft\boot\bcd /enum
- Verify you got bcd contents
- bcdedit /store k:\efi\microsoft\boot\bcd /set {default} debug No
- bcdedit /store k:\efi\microsoft\boot\bcd /set {default} testsigning OFF
- bcdedit /store k:\efi\microsoft\boot\bcd /enum
- Verify debug and testsigning are now “No”
- Safe dismount of microSD from PC by ejecting in Windows Explorer.
- Put microSD in Galileo and powered up
- Create a new project from the template.
- Plug the two serial ends of the cables together.
- Plug the 3.5mm end of the cable into the UART jack on the Galileo board.
- Plug the USB end of the cable into your computer's USB port.
- Open up Device Manager on your development machine and find out which COM port is being used by the adapter.
- Open a terminal program like Tera Term{:target="_blank"}
- Set the program to monitor the serial connection from the COM port you found in Step 5.
- Make sure your options are as shown below (with the Port set to the COM port you found in Step 5):
- If you are using Tera Term, you can get to the menu shown above by clicking on Setup -> Serial port..
- Replace the existing code in main.cpp with the following code:
{% highlight C++ %} #include "stdafx.h" #include "arduino.h"
int _tmain(int argc, _TCHAR* argv[]) { return RunArduinoSketch(); }
void setup() { Serial1.begin(CBR_9600, Serial.SERIAL_8N1); }
char output = 'a'; // The character being written
void loop() { // Handles the writing if (Serial1.write((uint8_t)output) != 1) { Log(L"Serial1.write failed\n"); } else { Log(L"%c being sent\n", output); }
// Loops the character from a to z
if (output == 'z')
{
output = 'a';
}
else
{
output++;
}
Sleep(1000);
} {% endhighlight %}
« Return to Samples{:role="button"}{:class="btn btn-default"}