Using Visual C# Express 2008 with the FTDI virtual COM port

Microsoft Visual C# Express 2008 offers an easy to use method to design PC based applications, it provides a multitude of buttons, timers, textboxes etc which are all available to be easily integrated into the target program. The aim of this tutorial was to show the absolute basics of what is required to talk to the COM port, from setting the port up to declaring an array buffer and then on to finally communicating with the target. More advanced error handling is not including for the purpose of keeping simplicity.

The Visual Express 2008 compilers from microsoft are available free of charge here

The first section of code is automatically added by Visual C# Express 2008, it is the include files for the compiler.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

Next comes the project name USB_READ_WRITE,  form1 which refers to the dialog box I drew my combo box "Ports", textbox "Messagebox1" and button in "Go". There is also a serial port handle "USB_RS485".

namespace USB_READ_WRITE
    public partial class Form1 : Form
        static SerialPort USB_RS485;
        byte port_open = 0;
        public Form1()        
         USB_RS485 = new SerialPort();

Listing the COM ports available is the first real task the program deals with, it does this with the SerialPort.GetPortNames() function and simply grabs each string (i.e. COM3) and loops round placing them in the combo box. Then I print "Select Port" in the combo box, this is only visible at the start and once a port is selected it's name will appear. 

            foreach (string s in SerialPort.GetPortNames())
         Ports.Text = "Select Port"

The Ports_SelectedIndexChanged routine below first of all checks to see if a previous port has been opened by the program and if so closes it. It then moves on to building a new group of settings for the COMx specified in the combo box. Finally it checks to see whether the COM port was successfully opened and if so set the flag.

        private void Ports_SelectedIndexChanged(object sender, EventArgs e)
         if (port_open != 0) 
             port_open = 0;
         USB_RS485.PortName = Ports.Text;                         //Ports.Text from combo box i.e. COM3
         USB_RS485.Parity = 0;
         USB_RS485.BaudRate = 38400;
         USB_RS485.StopBits = StopBits.Two;
         USB_RS485.DataBits = 8;
         USB_RS485.ReadTimeout = 500;
         USB_RS485.WriteTimeout = 500;
         if (USB_RS485.IsOpen == true) port_open = 1; 

Next comes the response to a click of the "Go" button. Firstly the program looks to see whether a COM port is open, if it finds there is not then it just returns. Upon finding that opening a COM port has been successfully opened the program loads a buffer array with data and uses the Write() function to send the data to the port defined by the USB_RS485 handle. We then wait for the expected data length to be received and use the Read() function to collect the data, again using the USB_RS485 handle we defined. Finally the returned data is then displayed in a Text box called "Messagebox1".

     private void Go_Click(object sender, EventArgs e)
        byte[] buff = new byte[6];

        if (port_open == 0) return;
            //USB_RS485.BreakState = true;
                             //if break is required use these two lines
            //USB_RS485.BreakState = false;
         buff[0] = 0;   
         buff[1] = 1;  
            buff[2] = 2;
         buff[3] = 3;
         buff[4] = 4;
         buff[5] = 5;
         USB_RS485.Write(buff, 0, 6);                                 //write to COMx (array, start, length)
         while (USB_RS485.BytesToRead < 4);                   //wait for expected read data length, i.e 4
         USB_RS485.Read(buff, 0, 4);                                 //read from COMx (array, start, length)
         Messagebox1.Text = string.Format(" Data returned = {0},{1},{2},{3}", buff[0], buff[1], buff[2], buff[3]);