Introduction
Quite a few times in my hardware projects, I had to send some calibration data between a microcontroller (Microchip PIC) and a PC through a serial port (RS-232). The software on the PC side is written in VB6 with an MSComm
control. The latter has to be configured with parameters: port number, baud rate, number of data bits, number of stop bits. Configuration of an MSComm
is a fairly common task (it used to be even more common in the past). So, at first I thought that there should be a dialog for that available on the web. Unfortunately, such a dialog was nowhere to be found, and I wrote one myself. Hopefully, it turned out generic and reusable enough.
Features
- Automatic detection of serial ports
- The most recent port settings are stored in the INI file
- Interface supports configuration of multiple ports in the same program
Dialog
Configuration is done in the frmComm
dialog.
ShowConfig
Declaration
Public Function ShowConfig( _
ByRef comm As MSComm, _
Optional ByRef strIniFile As String, _
Optional ByRef strSection As String) _
As VbMsgBoxResult
Purpose
Set-up the configuration dialog and show it to the user. cboPort
is copulated with a list of available COM ports.
Parameters
-
ByRef comm As MSComm
MSComm
which will be configured in the dialog. If the comm
is already open, the GUI is pre-loaded with its configuration and is comm closed.
-
Optional ByRef strIniFile As String
Optional name of the INI file containing configuration information. GUI is pre-loaded with the settings from the INI file.
-
Optional ByRef strSection As String
Optional name of the section in the INI file containing the configuration information. If the section name is not supplied, default name serial_port
is used.
Return Value
vbOK
when configuration was successful
vbCancel
when user decided to cancel configuration
QuietConfig
Declaration
Public Function QuietConfig( _
ByRef comm As MSComm, _
ByRef strIniFile As String, _
ByRef strSection As String) _
As VbMsgBoxResult
Purpose
Configure the target MSComm
with parameters from the local file without showing the configuration dialog.
Pre-conditions
The supplied INI file contains the configuration information.
Post-conditions
The target MSComm
is configured but not opened.
Parameters
-
ByRef comm As MSComm
MSComm
will be configured from the INI file. If the comm
is already open, it will be closed.
-
ByRef strIniFile As String
Optional name of the INI file containing configuration information. GUI is pre-loaded with the settings from the INI file.
-
ByRef strSection As String
Optional name of the section in the INI file containing the configuration information. If the section name is not supplied, default name serial_port
is used.
Return Value
vbOK
when configuration was successful
vbAbort
if the INI file was erroneous or non-existent
INI file
Loading and Saving
The dialog is populated with the parameters from the INI file, if the name of the file is supplied.
Structure
Multiple sections for multiple ports within the same INI file, or individual INI files for multiple ports. The name of the INI file section can be chosen by the user, the default name is serial_port
.
KEY: CommPort
- port number as in MSComm.CommPort
KEY: Settings
- port settings same format as in MSComm.Settings
Closing Remarks
Please let me know if you find any bugs or have any suggestions!
References
[1] Serial port configuration dialog in HyperTerminal
[2] Root MSDN article for MSComm
control
http://msdn2.microsoft.com/en-us/library/aa259393.aspx
Revision History
Rev# |
Notes |
Date |
Author(s) |
0.1 |
Initial draft |
June 4, 2007 |
Nick Alexeev |
0.2 |
Added missing frmComm.frx to the project files |
June 18, 2007 |
Nick Alexeev |
0.3 |
Updated the article |
July 23, 2007 |
Nick Alexeev |