Serial Class Per Universal Windows Platform - Part Two

Introduction

In the previous article Serial Class Per Universal Windows Platform - Part One, we discussed everything about the use of Serial Communication class included in Windows.Devices namespace. In this second and final part, we will create a test project with Visual Micro, a plug-in that is installed in order to program the Arduino family cards directly from Visual Studio. At the end of the first article, we created the graphical interface and the code for the management of serial communication, with the following result.


final project created with Visual Studio 2015
Figure 1: The final project created with Visual Studio 2015.

Test project

We start with Visual Studio 2015, select New Project from the File menu, select from the installed templates section C ++, as shown in the figure, and we call it Test serial communication.

test project with Visual Micro
Figure 2: The test project with Visual Micro.

We confirm with OK, and we are led to the code, as shown in the figure.

TestComunicazioneSeriale.ino file with Visual Micro
Figure 3: The TestComunicazioneSeriale.ino file with Visual Micro.

We, at this point, are ready to write a short example of code for the Arduino boards. The advantage of using Visual Micro is the convenience of having intellisense that greatly simplifies code writing, the suggestion of the methods, and / or properties at the time of typing a particular object. Another step to debug our code, unlike classic Arduino IDE where neither of these things are helpful, is  to highlight one thing: The debugger for Visual Micro is not included by default, but must be purchased separately. However, you have a trial where you can try it before buying if deems appropriate. Now, we write the code that does the following:
  1. LED off with one command called 1.
  2. Powering LEDs with command called 2.
  3. Pulse 1 second LED with command called 3.
  4. Pulse 2 seconds LED by command called 4.

It's really quite simple what we will do, and a deliberate choice. Now, we write the C ++ code below and comment on all phases.

  1. /*  
  2. Name: TestComunicazioneSeriale.ino  
  3. Created: 07/08/2016 14:43:27  
  4. Author: Carmelo La Monica  
  5. */  
  6. char incomingByte = '0'// for incoming serial data   
  7. int ledPin = 13;  
  8. // the setup function runs once when you press reset or power the board   
  9. void setup() {  
  10.         Serial.begin(9600); // opens serial port, sets data rate to 9600 bps   
  11.         pinMode(ledPin, OUTPUT);  
  12.     }  
  13.     // the loop function runs over and over again until power down or reset   
  14. void loop() {  
  15.     // send data only when you receive data:   
  16.     if (Serial.available() > 0) {  
  17.         // read the incoming byte:   
  18.         incomingByte = Serial.read();  
  19.     }  
  20.     if (incomingByte == '1') {  
  21.         Serial.println("LedOff");  
  22.         digitalWrite(ledPin, LOW); // sets the LED off   
  23.         incomingByte = '0';  
  24.     }  
  25.     if (incomingByte == '2') {  
  26.         Serial.println("LedOn");  
  27.         digitalWrite(ledPin, HIGH); // sets the LED on   
  28.         incomingByte = '0';  
  29.     }  
  30.     if (incomingByte == '3') {  
  31.         Serial.println("LedOn");  
  32.         digitalWrite(ledPin, HIGH); // sets the LED on   
  33.         delay(1000); // waits for a second   
  34.         Serial.println("LedOff");  
  35.         digitalWrite(ledPin, LOW); // sets the LED off   
  36.         delay(1000);  
  37.     }  
  38.     if (incomingByte == '4') {  
  39.         digitalWrite(ledPin, HIGH); // sets the LED on   
  40.         Serial.println("LedOn");  
  41.         delay(2000); // waits for a second   
  42.         digitalWrite(ledPin, LOW); // sets the LED off   
  43.         Serial.println("LedOff");  
  44.         delay(2000);  
  45.     }  
  46. }  
Now, analyze the newly-written code. Here, we defined two variables:
  1. char incomingByte = '0'// for incoming serial data   
  2. int ledPin = 13;  
The first one is char where we will check the values that can be from 1 to 4 as previously written. The second variable is int ledPin where we will connect the LED, to be precise on pin 13 of the Arduino Uno board.
  1. void setup()   
  2. {  
  3.     Serial.begin(9600); // opens serial port, sets data rate to 9600 bps   
  4.     pinMode(ledPin, OUTPUT);  
  5. }  
In the setup () method which executes only once, we initialize the serial communication with a baud rate of 9600 bits / ledPin. If we set the variable as an output (OUTPUT) with the pinMode instruction, so as to give a voltage value when the code will set to 1 by lighting the LED in this way.
  1. // the loop function runs over and over again until power down or reset   
  2. void loop()   
  3. {  
  4.     // send data only when you receive data:   
  5.     if (Serial.available() > 0) {  
  6.         // read the incoming byte:   
  7.         incomingByte = Serial.read();  
  8.     }  
  9.     if (incomingByte == '1') {  
  10.         Serial.println("LedOff");  
  11.         digitalWrite(ledPin, LOW); // sets the LED off   
  12.         incomingByte = '0';  
  13.     }  
  14.     if (incomingByte == '2') {  
  15.         Serial.println("LedOn");  
  16.         digitalWrite(ledPin, HIGH); // sets the LED on   
  17.         incomingByte = '0';  
  18.     }  
  19.     if (incomingByte == '3') {  
  20.         Serial.println("LedOn");  
  21.         digitalWrite(ledPin, HIGH); // sets the LED on   
  22.         delay(1000); // waits for a second   
  23.         Serial.println("LedOff");  
  24.         digitalWrite(ledPin, LOW); // sets the LED off   
  25.         delay(1000);  
  26.     }  
  27.     if (incomingByte == '4') {  
  28.         digitalWrite(ledPin, HIGH); // sets the LED on   
  29.         Serial.println("LedOn");  
  30.         delay(2000); // waits for a second   
  31.         digitalWrite(ledPin, LOW); // sets the LED off   
  32.         Serial.println("LedOff");  
  33.         delay(2000);  
  34.     }  
  35. }  
In the loop () method, we use a series of if statements to compare the values of the variable incomingByte. As you can see, there are four constructs for everyone with a condition 1, 2, 3, or 4. Returning for a moment to the project created in the first part, we have assigned each button interface a value which will be sent to the serial port after clicking on them. For precision: 
  1. "Shut Led" button will write '1' value on the serial port.
  2. Button "Turn on LED" will write '2' value on the serial port.
  3. Button "Pulse 1000 ms" write '3' value on the serial port.
  4. Button "Pulse 2000 ms" will write '4' value on the serial port.

The value sent by the interface on the Raspberry Pi2 and Windows 10 IoT will be read in the Arduino loop () method which will begin the comparison of if-tests. If one of the four satisfies the condition, the code will be executed inside. Suppose, you do click on the button "Pulse 1000 ms", the value '3' will be sent to the serial port and it will be fulfilled on the Arduino C ++ code.

  1. if (incomingByte == '3')  
  2. {  
  3.     Serial.println("LedOn");  
  4.     digitalWrite(ledPin, HIGH); // sets the LED on   
  5.     delay(1000); // waits for a second   
  6.     Serial.println("LedOff");  
  7.     digitalWrite(ledPin, LOW); // sets the LED off   
  8.     delay(1000);  
  9. }  
At this point, incomingByte will be '3' ( for which we will have a flash of LED for a second). This is written in delay(1000) which means a one-second pause. Everything will continue as long as we will not give another command to the interface in Windows 10. If we were to click on the "Turn Off LED" button, the first condition of the Arduino C ++ code will be executed. As the incomingByte will have the value '1', thus it will turn off the LED.

Test the project

Below is a video that shows everything we read and discussed in these two articles. A video is sometimes more than a thousand lines of text and / or words. Find the video at this link.

Conclusions

In this second part, we completed and demonstrated a simple example how they can communicate via a serial Raspberry Pi2 card with Windows IoT core and an Arduino Uno. We saw the project management with Visual Studio and a Type Universal application in the first part, concluding then with the second part of creating an example with Visual Micro, and in a practical way through videos seen the final operation of the project. Now it's up to you and good coding. 


Similar Articles