|| 3.32 out of 5
How Do I Use Serial Communication in Visual Basic (Specifically v6.0)?
I want to control a serial instrument using Visual Basic. How can I do this?
First, it is strongly suggested that anyone wanting to control a serial instrument consider using NI-VISA (available with Measurement Studio). If this is not an option, it is still possible to interface to a serial port in Visual Basic (the comments here are using Visual Basic 6.0). Serial communication in Visual Basic is done by using the MSComm component. Full documentation and examples with the MSComm component can be found in the Microsoft Developer Network. Here are some common issues which may occur when using Visual Basic for serial instrument control.
- Q: How do I add an MSComm component to my program?
A: In Visual Basic 6.0: right click the toolbox and select "Components". Check "Microsoft Comm Control 6.0" and click "OK". Place a MSComm control (it looks like a phone) on your form (it will be invisible at runtime). You can name the control if you like, the default name is MSComm1.
- Q: What properties of the MSComm component should I use?
A: The MSComm control is used by modifying and reading the object properties. Some of the useful ones are:
|This property can be used to set or return the comm port being used.|
|This property contains a string which specifies the communications settings of the port. The string is in the form "baud,parity,data bits,stop bits". For example the string "9600,N,8,1" corresponds to 9600 baud, no parity, 8 data bits, and 1 stop bit.|
|This property sets the handshaking mode for communication, a list of available constants should come up when it is used.|
|This property sets the number of characters that are read every time the input property is accessed. Setting this property to 0 will read tell MSComm to read all available data.|
|This can be used to set or retrieve the status of the port. Use this property to open and close serial sessions.|
|Accessing this property reads |
InputLen bytes from the buffer and then clears them from the buffer.
|Setting this property sends a command to the serial port.|
|This property returns the number of bytes currently in the buffer.|
- Q: How do I send carriage returns and line feeds in Visual Basic?
A: Visual Basic has a string constant called
vbLf for line feed, a string constant called
vbCr for carriage return, and a
vbCrLf constant, which sends both. You can concatenate strings using the
- Q: Why is the serial port sending data to my instrument, but not receiving responses?
A: Make sure that you are sending the proper termination character (usually
vbCr) and that you have the proper handshaking mode enabled.
- Q: Why am I receiving some data from the instrument, but not as much as I should be?
A: You may need to give the instrument time to respond. If you know how many bytes you are expecting, you can use a while loop and the
InBufferCount property to wait until the buffer has the expected amount of data. Alternately, you can use variables of the
Date data type with the
Now value and the
DateDiff function to wait for a specified period of time.
- Q: How do I parse responses in VB?
A: The best way to parse responses is to read them one character at a time by setting
InputLen = 1 and check for separators as you go. You can add each byte to a buffer if it is not a separator. If placed in a while loop this should eliminate any timing issues as well as parsing the data.
External Link: Microsoft Developer Network
Developer Zone Tutorial: Visual Basic 6.0 and .NET in Instrument Control
Report Date: 09/18/2003
Last Updated: 11/14/2012
Document ID: 31HBCB1P