The lastest ebl-arduino release (r52) brings to Arduino's users an easy way to exchange comands and data between PC and Arduino using .Net, being also easy to port to another languages and transports (TCP and so on).
Versão em português desse artigo: Controlando o Arduino com .Net.
AdvancedSerial
A new class that implements a serial protocol to make easier the communication between PC applications and Arduino sketches.
It is an example of sketch that receive commands from PC to control an LCD display:
#include <AdvancedSerial.h> #include <LiquidCrystal.h> #define BACKLIGHT_ON_MESSAGE 0 #define BACKLIGHT_OFF_MESSAGE 1 #define TEXT_MESSAGE 2 //pins #define PIN_BACKLIGHT 7 //configure lcd LiquidCrystal lcd(13, 12, 11, 10, 9, 8); void setup() { //configure backlight pin pinMode(PIN_BACKLIGHT, OUTPUT); //configure LCD lcd.begin(16, 2); //begin serial port with a desirable speed Serial.begin(115200); //configure receiver callback AdvancedSerial.setReceiver(onMessage); } void loop() { //AdvancedSerial job AdvancedSerial.loop(); } void onMessage(AdvancedSerialMessage* message) { switch (message->id) { case BACKLIGHT_ON_MESSAGE: digitalWrite(PIN_BACKLIGHT, HIGH); break; case BACKLIGHT_OFF_MESSAGE: digitalWrite(PIN_BACKLIGHT, LOW); break; case TEXT_MESSAGE: lcd.clear(); for (int i=0; i<message->size; i++) { if (i==16) lcd.setCursor(0, 1); lcd.write((char)message->payload[i]); } break; } }
Client API - AdvancedSerialClient
AdvancedSerialClient is the communication entry-point for AdvancedSerial based sketches. It permits to ensure that the device is connect, send messages to it and receive asynchronous events.
This part of a C# code is responsible to send control messages to LCDWriter sketch from last example:
try { //create object AdvancedSerialClient ArduinoClient = new AdvancedSerialClient(); //connect to device ArduinoClient.Open(Arguments[PARAMETER_PORT].ToString(), 115200); //command for LED if (Arguments.ContainsKey(PARAMETER_BACKLIGHT)) { SerialProtocol.AdvancedSerialMessage BacklightMessage = new SerialProtocol.AdvancedSerialMessage(); if ((bool)Arguments[PARAMETER_BACKLIGHT]) BacklightMessage.ID = BACKLIGHT_ON_MESSAGE; else BacklightMessage.ID = BACKLIGHT_OFF_MESSAGE; ArduinoClient.Send(BacklightMessage); } //command for text if (Arguments.ContainsKey(PARAMETER_TEXT)) { SerialProtocol.AdvancedSerialMessage TextMessage = new SerialProtocol.AdvancedSerialMessage(); TextMessage.ID = TEXT_MESSAGE; TextMessage.Payload = new System.Text.ASCIIEncoding().GetBytes(Arguments[PARAMETER_TEXT].ToString()); TextMessage.Size = (byte)TextMessage.Payload.Length; ArduinoClient.Send(TextMessage); } } catch (Exception ex) { Console.Write("Error: " + ex.Message); }
The complete, and ready to use, source for this example is available in library source tree.
Running the example
It's needed to properly run this example:
- 1 Arduino;
- 1 Breadboard (don't forget the jumper wires);
- 1 Display 1602A (16x2 with backlight);
- 1 10k potentiometer.
The components must be assembled in breadboard as following:
Once you've uploaded the LCDWriter sketch from AdvancedSerial examples (IDE menu: File/Examples/AdvancedSerial/LCDWriter) to your Arduino board the LCDWrite.exe client API example (IDE directory: libraries\AdvancedSerial\clientapi\dotnet\Release) will be able to control the backlight and text shown in LCD display.
In closing, a short demonstration of this example running:
This project has its own repository with more information about the classes, usage instructions and download options. It could be accessed through the URL http://code.google.com/p/ebl-arduino/.
No comments:
Post a Comment