Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic

Serial Port Configuration Dialog

Rate me:
Please Sign up or sign in to vote.
3.32/5 (11 votes)
28 Oct 2008CPOL3 min read 105.3K   5.1K   36   11
Generic configuration dialog for a serial port
Screenshot - frmComm.png

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

VB.NET
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

  • VB.NET
    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.

  • VB.NET
    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.

  • VB.NET
    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

VB.NET
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

  • VB.NET
    ByRef comm As MSComm

    MSComm will be configured from the INI file. If the comm is already open, it will be closed.

  • VB.NET
    ByRef strIniFile As String

    Optional name of the INI file containing configuration information. GUI is pre-loaded with the settings from the INI file.

  • VB.NET
    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

<stockticker>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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer Prolitech
United States United States
doing business as Prolitech
Redwood City, CA

blog (mostly technical)
http://prolifictec.com/blog/index.html

Comments and Discussions

 
Questionhow to communicate MItsubishi PLC with C# through 232 port.... Pin
Frq Sdq26-Aug-15 19:36
Frq Sdq26-Aug-15 19:36 
GeneralNeed frmAbout.frx file ... Pin
aaaaazzerty8-Sep-08 23:08
aaaaazzerty8-Sep-08 23:08 
GeneralRe: Need frmAbout.frx file ... Pin
Nick Alexeev28-Oct-08 19:57
professionalNick Alexeev28-Oct-08 19:57 
Generalmicrocontroller AduC + C# Pin
Juan Silva4-Nov-07 10:32
Juan Silva4-Nov-07 10:32 
AnswerRe: microcontroller AduC + C# Pin
Nick Alexeev4-Nov-07 23:14
professionalNick Alexeev4-Nov-07 23:14 
QuestionHow can read from HyperTerminal? Pin
ulduz6-Oct-07 1:20
ulduz6-Oct-07 1:20 
Questionwhat is this method for ? Pin
besthms31-Jul-07 3:43
professionalbesthms31-Jul-07 3:43 
Generali try that method and dosent work Pin
besthms31-Jul-07 3:13
professionalbesthms31-Jul-07 3:13 
sir , i need a function that return boolean value if i have data to recive ,, to use it in reciving .. im using Vb.net 2005 ,, it dosent work help me please ,,
(if true recive ,, if false no data.)Cry | :((
thnx 4 ur concern ,, thnx alot


2b or not 2b

GeneralRe: i try that method and dosent work Pin
YAIR-I22-Sep-07 9:45
YAIR-I22-Sep-07 9:45 
QuestionHelp please in Serial Port ? Pin
besthms27-Jul-07 6:47
professionalbesthms27-Jul-07 6:47 
AnswerRe: Help please in Serial Port ? Pin
Nick Alexeev27-Jul-07 7:02
professionalNick Alexeev27-Jul-07 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.