Mbed .NET RPC Library
.
This is a revised version of the Mbed .NET RPC library over Serial Interface that fixes the bugs inherent in the previous version as published here: mbed .NET RPC library. This new version runs on Windows machine with .NET framework 4.0 or higher. The latest version of Visual Studio Community Edition could be obtained from here.
To run the C# code, you need to have the latest version of Mbed RPC running on your Mbed device and connected to your PC via the USB port. You will need to know the virtual com port number that is assigned to your Mbed before running this code. The demo code and GUI below shows how to toggle and blink Mbed LEDs using the Mbed .NET RPC library.
Serial RPC Demo
The complete demo project was built with Visual Studio 2015, and it consists of two parts (see picture below):
- the .NET RPC Library (Mbed.RPC.Library) - which provides the interface to interact with Mbed pins and objects
- the Mbed RPC project (Mbed. RPC.Serial) - which makes use of the .NET RPC Library to blink LEDs.
The RPC Serial Project is already set as the default startup project which will call the .NET library to invoke the RPC calls that talk to the Mbed. The main C# code is as shown below.
using org.mbed.RPC; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace Mbed.RPC.Serial { public partial class SerialRPCForm : Form { DigitalOut _led1, _led2, _led3, _led4; // DigitalOut RPC Object int led1Status, led2Status, led3Status, led4Status, commStatus; // led status variables string selectedPort; SerialRPC _serialRPC; // embed rpc handle public SerialRPCForm() { InitializeComponent(); //get list of active ports on the computer string[] ports = SerialPort.GetPortNames(); serialComboBox.Items.AddRange(ports); } private void SerialRPCForm_Load(object sender, EventArgs e) { //disable controls until com port is connected groupBox1.Enabled = false; startButton.Enabled = false; //initialize status variables commStatus = 0; led1Status = 0; led2Status = 0; led3Status = 0; led4Status = 0; statusLabel.Text = "Not connected!"; } private void serialComboBox_SelectedIndexChanged(object sender, EventArgs e) { selectedPort = serialComboBox.SelectedItem.ToString(); try { //Create an mbed object for communication over USB (serial) _serialRPC = new SerialRPC(selectedPort, 9600); //Create new Digital Outputs on the mbed _led1 = new DigitalOut(_serialRPC, new MbedPin("LED1")); _led2 = new DigitalOut(_serialRPC, new MbedPin("LED2")); _led3 = new DigitalOut(_serialRPC, new MbedPin("LED3")); _led4 = new DigitalOut(_serialRPC, new MbedPin("LED4")); //enable controls after com port is connected groupBox1.Enabled = true; startButton.Enabled = true; //MessageBox.Show(selectedPort +" connected to Mbed", "Mbed Connected!", MessageBoxButtons.OK, MessageBoxIcon.Information); commStatus = 1; serialComboBox.Enabled = false; statusLabel.Text = "Mbed connected to " + selectedPort; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); commStatus = 0; if (_led1 != null) _led1.delete(); if (_led2 != null) _led2.delete(); if (_led3 != null) _led3.delete(); if (_led4 != null) _led4.delete(); if (_serialRPC != null) _serialRPC.delete(); //disable controls if mbed is disconnected groupBox1.Enabled = false; startButton.Enabled = false; statusLabel.Text = "Not connected!"; } } //fire an event if when checkbox changes private void led1_chkBox_CheckedChanged(object sender, EventArgs e) { if (led1_chkBox.Checked) { led1Status = 1; string _response = _led1.write(1); //Led on } else { led1Status = 0; string _response = _led1.write(0); //Led off } } //fire an event if when checkbox changes private void led2_chkBox_CheckedChanged(object sender, EventArgs e) { if (led2_chkBox.Checked) { led2Status = 1; string _response = _led2.write(1); //Led on } else { led2Status = 0; string _response = _led2.write(0); //Led off } } //fire an event if when checkbox changes private void led3_chkBox_CheckedChanged(object sender, EventArgs e) { if (led3_chkBox.Checked) { led3Status = 1; string _response = _led3.write(1); //Led on } else { led3Status = 0; string _response = _led3.write(0); //Led off } } //fire an event if when checkbox changes private void led4_chkBox_CheckedChanged(object sender, EventArgs e) { if (led4_chkBox.Checked) { led4Status = 1; string _response = _led4.write(1); //Led on } else { led4Status = 0; string _response = _led4.write(0); //Led off } } //LED blinky event private void button1_Click(object sender, EventArgs e) { statusLabel.Text = "Busy! - RPC action in progress..."; groupBox1.Enabled = false; startButton.Enabled = false; startButton.Enabled = false; stopButton.Enabled = false; BlinkLed(6); stopButton.Enabled = true; startButton.Enabled = true; groupBox1.Enabled = true; startButton.Enabled = true; statusLabel.Text = "Mbed connected to " + selectedPort; } private void stopButton_Click(object sender, EventArgs e) { try { //delete objects before exit if (_led1 != null) _led1.delete(); if (_led2 != null) _led2.delete(); if (_led3 != null) _led3.delete(); if (_led4 != null) _led4.delete(); if (_serialRPC != null) _serialRPC.delete(); Debug.Print("Complete"); } catch (NullReferenceException ex) { Debug.Print("No Reference: " + ex.Message); } this.Close(); } //custom functions/methods private void BlinkLed(int n) { try { for (int i = 0; i < n; i++) { led1Status = 1 - led1Status; //flip between 0 and 1 _led1.write(led1Status); led2Status = 1 - led2Status; _led2.write(led2Status); led3Status = 1 - led3Status; _led3.write(led3Status); led4Status = 1 - led4Status; _led4.write(led4Status); Thread.Sleep(250); } } catch (NullReferenceException ex) { Debug.Print("No Reference: " + ex.Message); statusLabel.Text = ex.Message; } } } }
Running the code above with the .NET and Mbed RPC libraries in place will produce the following GUI display that can be used to toggle LED1, LED2, LED3 and LED4 on Mbed.
Information
The source code and executable files can be downloaded from the links below.
Below is the code that needs to be running on Mbed for .NET RPC to work:
#include "mbed.h" #include "SerialRPCInterface.h" using namespace mbed; //Create the interface on the USB Serial Port SerialRPCInterface RPC(USBTX, USBRX); int main() { while(1) { } }
Import programRPC_DOTNET
demo code for mbed iterface
Troubleshooting!
The Mbed needs to be connected to the PC before running the code in order for the Mbed com port to be listed dropdown list. It is required that no terminal window should be connected to the Mbed com port.
References
Thank you!
Please log in to post comments.