Overview
I've started to learn C# to make mobile apps with Xamarin forms for controlling some of my electronics projects. To familiarize myself with the syntax and idiosyncrasies of C# I decided to make a basic serial monitor desktop app using Windows Forms. I basically modelled it of the serial monitor included in the Arduino ide, and tried to add small improvements where possible.
Code
Main steps
- Layout the GUI
- Establish serial reading
- Establish serial writing
- Improve UX
GUI Layout
In the past I've made small GUI programs with python and tkinter, so using the Windows Forms design editor was a welcome change. It was very easy to get a basic interface up and running without having to delve too deeply into the code. It was very much a case of dragging and dropping the relevant parts onto the window in the designer and attaching callback function to the relevant events.
Serial communication
The C# serial port library is pretty simple and easy to use. All it needs is a COM port to connect to and a valid baud rate as can be seen in the example below.
Here is a simplified example of the serial reading and writing functions. The full functions can be found in the project linked at the bottom of the page.
Sending and receiving was tested with an Arduino running a program that echoes any message it receives back to the terminal.
Improvements
The textbox, and button for message sending were disabled and greyed out when the serial port is not connected. The list of connected devices is also refreshed each time the COM port combo box is clicked. I also added an implementation for keeping track of the previously send messages, which can be scrolled through with the up and down keys. This was done simply by pushing them to a stack when they are sent, and then pushing them to another stack when the up key is pressed to enable scrolling up and down through them.
Full repository : https://github.com/zacrogers/SerialTerminal