A GUI application for simple serial communication terminal will be created in the following steps, using .NET framework in Visual Studio environment. Visual C++ is selected as a programming language.
(1) Download and install Visual Studio from Microsoft site. (I’m using Express Edition 2012)
(2) Open up Visual Studio, then click File > New Project.
(1) Download and install Visual Studio from Microsoft site. (I’m using Express Edition 2012)
(2) Open up Visual Studio, then click File > New Project.
(3) Click Installed > Templates > Visual C++ > CLR > CLR Empty Project. Type the Solution Name “SerialCOMMTerminal”, Click OK.
(4) An empty project is created, it still has no form, we need to add it manually. Pull down "Project" menu and select "Add New Item", select "UI" folder on the left and then select "Windows Form" on the right. Name the form "Form1.h" and click Add button.
(Note: The form name should be different than our application namespace, as I've experienced error when building it with same name, Visual Studio will complain that the namespace is "ambiguous" and the building failed)
(5) Pull down "Project" menu and select "Properties". Open up "Configuration Properties" and then "Linker" and then "System".
Change the "SubSystem" property to "Windows / SUBSYSTEM:WINDOWS)".
Open up again "Configuration Properties" and then "Linker" and then "Advanced". Set the "Entry Point" property to "main".
(6) Add all the fields and buttons to Form1.h[Design] by expanding “Toolbox” at the left side dock, just drag and drop until the form looks like this:
When we add any component to the form design, Visual Studio will automatically modify the source code in “Form1.h”, adding the reference to the component that we just added in the “InitializeComponent” function. In the form there are: 10 labels, 2 textboxes, 4 buttons, 6 comboboxes, 2 richtextboxes, and 1 serial port component.
Add the collection of item to the combobox, and edit the text property so that it has similar string with the initial default selected item. (For example, if we want combobox cbBaudRate has “9600” selected from the start of application, then edit the cbBaudRate Text property to “9600”)
(8) Put the code for Event handler method (all the button need this when triggered by click). In the form design, click the button (for example btnRescan) then under Properties window, choose the lightning bolt symbol, then double click the empty space to the right of Click Event. This will bring the “Form1.h” Tab, and a new method/function is created automatically, then we edit the code inside it.
(9) How we make the application work is simple. When the application first start, it will scan for the available COM port connected to our computer by using the method from SerialPort class “GetPortNames()”. All the available COM port then copied to combobox cbCOMPort, so the user can select which port to connect. Then we set all the setting (baudrate, data bits, parity, stop bit, etc) as we need, then click button “Connect”. The application will copy all the selected settings from the comboboxes to serialPort1 object, then open the port. After that if we want to send some string to the serial port we just type the string in the richtextbox under the label “Write:” then click “Send”, the application will copy all the string and call SerialPort method “Write()”. Any byte received by serialPort1 will trigger an event, the event handler method will call SerialPort method “ReadExisting()” and put all the received string to richtextbox under the “Read:” label. When we want to end the connection just click “Disconnect” button and SerialPort method “Close()” will be called.
(10) Edit the code for “SerialCOMMTerminal.cpp” (under “Solution Explorer”, expand “Source Files” right click SerialCOMMTerminal.cpp then select View Code):
(12) If the build success, to debug the application click Debug > Start Debugging.
Testing
To test the serial communication terminal I use FT232R, a USB to serial converter chip from FTDI. The FT232R board is connected in loopback fashion (TX --> RX).
Once connected to PC USB port, Windows device manager will detect the USB Serial Port (COM7 in my case). The serial communication setting for the port must be set in device manager, right click on the port then choose properties. The port setting used for this simple test is shown below:
Write some string for example “hello world” then click the Send button. The string “hello world” will be received in the Rx buffer and displayed in the rich text box.
Next the serial communication terminal is tested to communicate with real device. It will be used to communicate with a microcontroller. The connection from FT232R to MCU (PIC24F) is similar like diagram below:
This time, handshaking/flow control CTS/RTS is used. For full duplex communication (DTE-DTE), CTS and RTS pin is connected just like shown below:
Change the port setting in device manager to:
PIC24F running UART. The test work like this: From the terminal we first send the total number of byte to inform the MCU, then we type and send the string. PIC24F MCU will receive the byte as many as had been informed on the first transmit, then it will convert the string to upper case or vice versa, then send it back to the serial port. The terminal receiving it will display the string in the rich textbox. For example we first send “011” (mean 11 byte), then we type “hello world” and send it. PIC24F will receive it and convert it to upper then send it back as “HELLO WORLD”.