.NET Gadgeteer Serial Port Communication

The .NET Gadgeteer system allows you to create small standalone devices that run the so-called .NET Micro Framework. In an unusual scenario, I wanted to be able to send messages from a laptop, which is connected to a Gadgeteer device via a USB cable, to the Gadgeteer device. (Normally, a WiFi connection would be used). I used a USB serial module on Gadgeteer to receive messages. I wrote a simple WinForm application to send messages from my laptop. I used a small OLED display on Gadgeteer to display the received message.


First I got a USB Serial Port module from GHI Electronics. Before connecting the serial module to Gadgeteer, I connected the serial module directly to my laptop to get the appropriate driver, so I could eventually build and deploy the Gadgeteer program to the Gadgeteer device. On a Windows 7 laptop, in Device Manager, I got an unknown “Other Devices”. It took me a while to find the right driver on the Internet from somebody/something called FTDI. When I finally installed the correct driver, the serial port module showed up as “COM4” in Device Manager, in the Ports (COM & LPT) section. On a Windows 8 laptop, the driver was quickly found automatically.

With the USB serial module driver installed on my laptop, I connected the module to socket #9. To display the messages sent from the laptop, I connected an OledDisplay module from Seeed to socket #6.

I created a new Visual Studio project using the Gadgeteer template. (This assumes I had all the necessary Gadgeteer software installed, which isn’t a trivial task). The key code in ProgramStarted is:

. . .
uint lineStart = 1; // class-scope
. . .

void ProgramStarted()
  Debug.Print("Program Started");
  usbSerial.SerialLine.DataReceived +=

Notice the class-scope “lineStart” variable. The event handler, which does something on Gadgeteer when data is received from the connected laptop is:

void SerialLine_DataReceived(GT.Interfaces.Serial sender,
  System.IO.Ports.SerialData data)
  // called when connected PC sends bytes
  Font smallFont =
  GT.Color red = GT.Color.Red;
  oledDisplay.SimpleGraphics.DisplayText("", smallFont,
    red, 1, lineStart);

  int numBytesToRead = usbSerial.SerialLine.BytesToRead;
  byte[] inputBuffer = new byte[numBytesToRead];
  usbSerial.SerialLine.Read(inputBuffer, 0, numBytesToRead);
  string s = new string 

  if (lineStart > 120)
    lineStart = 1;

  oledDisplay.SimpleGraphics.DisplayText(s, smallFont,
    red, 1, lineStart);
  lineStart += 9;

There is a lot going on here. Basically, I grab the bytes received from the laptop, convert to a string, and display on the Oled device. To be honest, I don’t understand a lot of what’s going on — I was happy just to get it working.

I built and deployed the Gadgeteer program, with all the usual glitches.

To send messages, I created a new Visual Studio WinForm application. I added a TextBox control for users to type in a message, and a Button control to trigger sending. The key code is:

. . . 
using System.IO.Ports;
. . .
private void button1_Click(object sender, EventArgs e)
  string s = textBox1.Text.Trim();
  System.IO.Ports.SerialPort sp = new
    System.IO.Ports.SerialPort("COM4", 9600,
    System.IO.Ports.Parity.None, 8,

I built and ran the WinForm application and successfully sent messages from the WinForm to the Gadgeteer device as shown in the image above.

This entry was posted in Machine Learning, Software Test Automation. Bookmark the permalink.