It’s been a while since my last post and now I think I have something to share, related to LOB Metro Apps and Serial Port Communication. Winodws 8.1 provides a really good platform for companies to build nice, touch friendly user experience. For some companies, especially for industrial, it is important for platform to support different type of devices. Even now it is a bit difficult to find a PC which still has Serial Port, many third party devices still use a Serial Port as a main point of communication. Windows 8.1 added back support for Serial Port communication, but you need to have a proper Serial to USB converter which is compatible with USB CDC standard protocol, what is really hard to find in real life. If you are lucky and you have found a way how to get that converter, this post is not for you and you better look at the Microsoft sample here However for all other developers, who are looking for kind of easy way to include integration of Serial Port in their Windows 8 app, this post will be useful.
As I mentioned in my previous posts, I have been working on some very interesting project, related to Kiosk software built for Windows 8. One of the coolest feature, in my opinion, was a RFID reader integration. Most of the RFID readers support only Serial Port communication and I have tried couple different ways how to connect that sort of devices to my Metro app. You can easily find a Serial to Bluetooth converter on the market and Windows 8 device api does it job really well, when you want to have a connection to Bluetooth device. However, device api will always prompt for access from user, what wasn’t good in my case.
In Windows 8.1 Update 1, the Microsoft introduced a new component called – Broked Windows Runtime Component (the white paper could be found here – here) The idea behind, in simple words that Broked component allows inter process communication between win32 app and your metro app, using a kind of proxy. So, in this post I will go through a simple scenario, when we handle a Serial Port communication inside Broked Component and pass data to your metro app. The good thing also about broked component is that if you app is in the sleep mode, it still can receive a data and you can activate your app based on what you received from Broked component. So let’s start.
Setting up the solution
To be able to create a new project in VS 2013, you need to get the last update for VS. Create a new Windows Store blank app called “SerialPortSampleApp”:
Now, right click on solution and add new Project, called it “SerialPortSampleAppBroked”
Rename the class to SP, so your solution will look like this:
New, add a new item project to solution. It is going to be our proxy project.
Once all done, come to Proxy project and add reference to SerialPortSampleAppBroked project:
After, come to SerialPortSampleApp and add reference to your Proxy Project
Build the solution. The last step before we start modifying the code is to change Proxy project properties to allow VS to register new dll on build. Right click on Proxy project, go to properties and change “Register Output” to YES.
Close VS and run it again with Administrator rights. Now we are ready to start.
So far we have created 3 projects – one is to keep our Serial Port logic, second one is our Metro App, which will provide a UI, and the last one is a Proxy project which will play a role of Bridge between two projects. As you can see, we change the properties of Proxy project to register a new dll when we build a solution. The metro app has a reference to Proxy project and it will expect a dll to be registered in the system. The last part is we need to change the Metro App manifest file, to let the app know where to find an implementation of our SerialPort library.
So come to your App and right click on manifest file and select – view code:
In my case the path to Broked Project dll is “C:\Users\nicksav\Documents\visual studio 2013\Projects\SerialPortSampleApp\SerialPortSampleAppBroked\bin\Debug”
Save manifest and build the solution again. Now are ready to ad Serial Port logic into Broked component.
Right click on your Broked project and select “Unload Project”
Next, edit csproj file. We are going to add reference to System.IO library.
Add System.IO reference:
Save the file and reload the project.
Now come to SP.cs file and add following code:
public sealed class SP
private SerialPort _serialPort;
public event EventHandler<string> PeriodicEvent;
string portName = "";
string ports = SerialPort.GetPortNames();
if (ports.Count() > 0)
int portInt = 0;
foreach (var port in ports)
var portNumber = port.Substring(port.Length - 1);
if (portInt < Int32.Parse(portNumber))
portInt = Int32.Parse(portNumber);
if (portInt != 0)
portName = "COM" + portInt;
_serialPort = new SerialPort(
_serialPort.ReadTimeout = 200;
if (_serialPort != null && _serialPort.IsOpen)
_serialPort.DataReceived += _serialPort_DataReceived;
private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
char GPSChars = new char;
int Cnt = 0;
while (Cnt < GPSChars.Length)
Cnt += _serialPort.Read(GPSChars, Cnt, GPSChars.Length - Cnt);
StringBuilder SB = new StringBuilder(Cnt);
for (int I = 0; I < Cnt; I++)
This is just a standard code to use when communicating to Serial Port. You change the settings, based on your Device. The most impotent part here is “PeriodicEvent”. This event is triggered when the data comes from Serial Port. Build the solution and open “MainPage.xaml.cs”
Add the following code:
public sealed partial class MainPage : Page
SerialPortSampleAppBroked.SP client = new SerialPortSampleAppBroked.SP();
client.PeriodicEvent += client_PeriodicEvent;
void client_PeriodicEvent(object sender, string e)
var dataReceived = e;
That’s all. Now you are able to get data from Serial Port. Keep in mind that Microsoft Store wont allow such apps, it is only for side loading.
The source could be found here: