Communications Between A PC And A Microcontroller.
In our last episode of Smiley's Workshop, we finished with our introduction to the Arduino Projects Kit and used all the parts to do some interesting things. This time, we will start to look a bit deeper into communicating between the Arduino and a PC and we'll begin a three-part series about serial communications between a PC and a microcontroller. In the first part, we will learn a bit about the virtual serial port used in the Arduino and introduce writing programs on the PC using the free Microsoft C# and Visual Basic Express .NET applications. We’ll use this to build Graphical User Interfaces (GUIs). Next time, we'll apply this knowledge to build a Simple Terminal program. In the third part, we'll build a GUI for an Arduino VoltMeter.
Why Imitate a PC Serial Port with USB?
In the old days, the PC serial port had a Windows© driver and hardware link based on the RS-232 electrical specification, and used a DB-9 connector and a UART (Universal Asynchronous Receiver Transmitter). It wasn’t exactly simple for a novice PC user to hook up a serial link since it required the user to select software interrupts and set hardware jumpers — something you and I as certifiable nerds like to do, but something that normal people hate and tend to mess up. These and other complications led to the development of the Plug and Play initiative (more commonly and correctly known as Plug and Pray). One part of all this was to replace the serial port with USB to help simplify things. This helped the user, but made the developer’s life much more complicated. Lots of perfectly good serial devices and a couple of decades of knowledge of how to do robust RS-232 style serial communications between PCs and external serial devices were made obsolete by USB.
I’ve read the USB specification, and I’m here to testify (brother, amen) that the old ComPort/UART/RS-232 was a piece of cake compared to USB. I worked with USB when it first came out and my brain is definitely worse for the wear. Fortunately, some really smart people created a transitional concept: to have an old fashioned RS-232 style serial port that runs over USB. The FTDI folks call this a Virtual Communications Port (VCP). It allows legacy applications to continue to use the old microcontroller code and the Windows COM port software with the USB part. This is all tidily bound up in a black box that the developer doesn’t have to open. These transitional devices give us the best of both worlds: the ease of using the serial (COM) port and the ubiquity of using USB. The developer has the option of adding RS-232 level converts to completely emulate the old way of doing things, or leaving the level converters off and outputting voltage levels directly compatible with a microcontroller’s UART. The latter is exactly what the Arduino does to allow serial communications with a PC.
The FTDI chip (FT232R) used on the Arduino is the same as that used on the BBUSB that was discussed in detail in the article “The Serial Port is Dead, Long Live the Serial Port’ by yours truly in the June ‘08 issue of Nuts & Volts. You can also get the book Virtual Serial Programming Cookbook (also by yours truly) and an associated projects kit from either Nuts & Volts or Smiley Micros. Much of the information in this article on using C# and VB for serial communications is derived from that book.
Why Communicate With a PC?
A microcontroller is used to control something. The something could be the ignition timing on your car, the water temperature in your washing machine, the obnoxious tune (that you think is cute) on your cell phone, etc., etc., etc. The word ‘ubiquitous’ seems almost invented to describe the current uses of microcontrollers; they really are everywhere.
Most often, the microcontroller knows how to do its job and doesn’t need any advice from you. If it does deign to accept suggestions, you usually give them via a button (snooze alarm) or touch pad (microwave oven). Some of us — mostly students, hobbyists, and developers — want to spend a lot of time in conversation with a microcontroller, however. This may be because we want to learn how it works; get it to follow complex commands that aren’t easy to give with buttons; or because we are designing a complex system and we need direct access to the micro’s brains while we are figuring out why things aren’t working like they are supposed to. If you are one of these folks, then you might see the benefit of being able to use the vast resources of a PC to talk to your microcontroller. We will look at some things to help you to do this.
Why Use C# and Visual Basic Express .NET?
One good reason for selecting either language in Express .NET is because both are free. (You like free, don’t you?) I decided to present the software in both C# and Visual Basic Express.NET because there are lots of folks who program in one language and think the other language is the vile realm of hell-bound heretics. However, the concepts are the same no matter what language is being used (religious wars aside). Examples are done in both languages with the C# example shown here, and the Visual Basic example included in the Workshop18.zip. Just skip over the language you don’t like.
You will find a treasure trove of free tools and learning materials at www.microsoft.com/express. If you are entirely new at programming GUIs (pronounced gooey) on Windows, then I’d suggest that you use C#, since it’s similar to C and is most likely the language you will choose for the other side of the cable — the microcontroller. If you are already a Basic fan or have used VB on a PC, then you will probably want to use the Visual Basic Express. Be prepared for a lot of culture shock, however.
The IDE shown in many of the illustrations here will be for C#; the Visual Basic IDE is virtually identical, so it shouldn’t be difficult to transpose the concepts.
As I stated before, the Microsoft learning resources are really great and free. For our purposes, you don’t need to view all the lessons, just the introductory materials, plus the parts on forms and common controls. Keep in mind the ultimate goal: We are learning to use tools that will allow us to build GUIs on a PC that we can use to communicate with microcontrollers connected to either a real serial port or a USB virtual serial port.
Once you’re comfortable with building simple forms with textboxes and buttons, you are ready to look at the Simple Terminal software source code. Pay careful attention as we go through the steps to create this program. Before we write it, let’s play with the finished version to get some insight into where we are going once we actually write the code.
The Simple Terminal is available in two forms: first is a publish version that allows you to install and run the program on your PC; the second version is the source code that loads in Visual Studio Express .NET.
The Workshop18.zip includes:
Unzip it and open the Simple Terminal Application directory; then click on the setup application. You will see the warning shown in Figure 2. Click Install if you are using Vista (cuss it out if it asks you if you are sure). It should open and look like Figure 1, except that the text boxes should be empty.
|FIGURE 1. A Simple Terminal.||FIGURE 2. Yet another bogus warning.|
Click on the ‘Settings’ menu item. In the Settings dialog, click the COM port connected to the Arduino as shown in Figure 3. In my case, the only port available is COM5. Your Arduino will probably be on a different port, so click on that one. Make sure the serial port you selected shows up in the Serial Port label. If you don’t click it, then the port defaults to COM1. The Arduino Command_Demo that we will be using has a baudrate set to 19200, which is the default for the Simple Terminal. You should now click ‘Okay.’
|FIGURE 3. Settings.|
Next, open the Arduino Command_Demo directory on your PC and then upload the Command_Demo program into the Arduino as discussed in earlier workshops (copy/paste/run). Press the reset button on the Arduino and you should see ‘Command_Demo rev. 0.02’ in the Receive text box. Finally, enter ‘cmd0!’ in the Send text box and you should see the response in the Receive box as shown in Figure 1.
Cool! Now, let’s follow a recipe and cook up a Serial Terminal GUI for ourselves, shall we?
The Main Form
|FIGURE 4. New Project.|
|FIGURE 5. Select Windows Application.|
|FIGURE 7. Properties.|
|FIGURE 8. Add MenuStrip.|
|FIGURE 9. menuStrip1.|
|FIGURE 10. Add RichTextBox.|
|FIGURE 11. RichTextBoxes.|
|FIGURE 12. BackColor.|
|FIGURE 13. Font.|
|FIGURE 14. GUI with makeup.|
|FIGURE 15. Add menu item.|
|FIGURE 16. Menu items.|
|FIGURE 17. Start debugging.|
Whoa, look at that! You didn’t write a word of software and yet you just created a GUI for a serial terminal! Click the little X in the upper right to close the debug form.
Add Functionality to the Menu Items
This is what we see in the C# Edit Window:
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
namespace WindowsApplication2<br />
public partial class Form1 : Form<br />
public Form1()<br />
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)<br />
MessageBox.Show(“Menu item ‘Settings’”);<br />
private void openPortToolStripMenuItem_Click(object sender, EventArgs e)<br />
MessageBox.Show(“Menu item ‘Open Port’”);<br />
private void clearToolStripMenuItem_Click(object sender, EventArgs e)<br />
MessageBox.Show(“Menu item ‘Clear’”);<br />
|FIGURE 18. Click the Settings menu item.|
private void clearToolStripMenuItem_Click(object sender, EventArgs e)<br />
richTextBox1.Text = “”;<br />
richTextBox2.Text = “”;<br />
In this workshop, we went through the Microsoft tutorials and learned just enough to be dangerous. We also built the GUI for a Simple Terminal. In the next workshop, we will build a dialog form (Settings) to get data from the user for selecting a serial port, and set up the UART. We will then learn to use the Serial Port Class, and finally we will build an Arduino voltmeter that displays the output on a PC. NV
You can find the source code and supplements for this article in Workshop18.zip in the downloads section of the Nuts & Volts website and Smiley Micros website.
The Arduino projects kit is a special hardware pack that will help you get hands on experience as you follow along with this series. It provides components for use with Workshops 9, 10, 11, and many future Workshops. Over time, we'll learn simple ways to use these components, and more importantly, use them to drill down into the deeper concepts of C programming, AVR microcontroller architecture, and embedded systems principles.