C# Windows IoT Core Serial Communications with an ESP8266

Falafel Software Bloggers
Falafel Software
Published in
4 min readDec 28, 2016

Windows IoT Core is great for connecting single board computers such a Pi2 to an ESP8266, via a serial link. The hardware for such a link can be found in my previous post here. The details for a basic serial communication C# framework are detailed in this post.

If you have an ESP8266, there are numerous ways of communicating with them such as LuaLoader. However, I needed to communicate with my ESP8266 through a Pi2 running Windows IoT Core. This is part of a larger automated test project for an upcoming consumer project we’re developing. In this post, I’ll cover the basic framework for detecting, initializing, listening, and writing to a com port.

Because our program needs to run on Windows IoT Core, our project type is a Universal Windows app. To enable serial communications in your UW app you need to manually edit the Package.appxmanifest file by right clicking on the file in the Solution Explorer and selecting View Code.

Fig 1

Fig 1

Edit the Capabilities tag to include the following DeviceCapability tag. Yes, you have to do this manually at the moment.

<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>

In this project we’re going to put all of the communication capability inside a viewmodel class called ESPViewModel. We have some private members to handle the com port, which are explained below.

Fig 2

Fig 2

Fig 2–1: SerialDevice member is used connect to the serial hardware.
Fig 2–2: DataReader member is used to handle the reading of data from the serial hardware.
Fig 2–3: CancellationTokenSource member is used to help with canceling serial reads when a timeout limit is reached.
Fig 2–4: The split variable is used in the parsing of data being read so we can throw away older data. The port variable is used to identify the specific serial port name we want to open. We are using the “USB to TTL Serial Cable — Debug / Console Cable for Raspberry Pi” cable from Adafruit in this project. The value we need to load into the port variable to select this cable is “TTL232R-3V3”. Other serial hardware will have their own values. The read buffer length is set to 1024 bytes.

Now we need to detect and connect to the serial we are interested in.

Fig 3

Fig 3

Fig 3–1: GetDeviceSelector is used to get an Advanced Query Syntax (AQS) string for selecting available serial ports.
Fig 3–2: FindAllAsync is used to select available serial ports using the AQS string. We can now select the particular port we are trying to connect to. The object returned contains all of the device information for the serial port.
Fig 3–3: FromIdAsync actually connects to the hardware and returns an object where we can configure/read/write to our serial port.
Fig 3–4: Now we configure our serial port to talk with our ESP8266. We can also configure our app to start listening for serial data.

Next we set up the listener.

Fig 4

Fig 4

Fig 4–1: We create a new DataReader passing in the InputStream of our serial port.
Fig 4–2: Now we enter into a continuous loop that calls our ReadAsync function that listens for data until the app ends.

Fig 5

Fig 5

Fig 5–1: Check to see if a cancellation was requested or a timeout has occurred.
Fig 5–2: Create and execute the data reading task.
Fig 5–3: If bytes are read, then read them.

Writing data to our serial port is simple.

Fig 6

Fig 6

Fig 6–1: Create a new DataWriter object using the serial’s OutputStream.
Fig 6–2: We write the data we want to send to the com port by calling the DataWriter.WriteString method.
Fig 6–3: To flush the data out to the com port we call the DataWriter.StoreAsync function. The number of bytes written is returned.
Using this basic framework you can build a complete Windows IoT Core app that can allow you to reliably communicate to your ESP8266. You can download the basic app here. You’ll notice a few more bits of code that will be covered in a follow up post that deals with communications within a state machine. That’s useful with an automated test project.

--

--