Home Parallel Port Serial Port Serial Info Parallel Info USB .NET Webboard I/O Today API Project Directory

 Visual Basic(VB) Programming in RS-232,Serial Port,Com Port Interfacing for HardwareS systems.

Thai Page
Visual Basic programming access I/O Serial Port

    We will used VB control "MSComm" that you should be referrent the control by Custom control .

This programming guide assumes the user has a basic knowlege of Visual Basic programming. The teaching method used is to show a basic example of a VB6.0 program which communicates with an modem or PC to PC together by sending and receiving ASCII data, and then disect the program to understand its operation.
PC's serial data acquisition interfaces require the sending and receiving of ASCII data to operate. To communicate with the serial port using Visual Basic, the MsComm control must be utilized to allow serial data transfer via a serial port ( Com1-Com4). MSComm is a custom control shipped with VB5.0 and VB6.0 and must be loaded using the Tools menu.
Select menu Project-->Companents
Follow to select in check box name is MSComm after it will display a telephone icon on control box dialog that drag and drop it to your form of Project Please see thus picture below.

1. Step one Select at top menu bar of Visuabasic thus as picture below


2. Step two Select Control name is "Microsoft Comm Control 6" thus picture below.

3. Step three drag MS Comm control from Toolbox to paste on Form thus as picture below.

Programming for interfacing Serial Port has two method .

1. interrupt communication.
Interrupt process is check device signal if the devices has already in sending code to comm port of computer.
we are programming in CommEvent and OnCommEvent that these event will be active when coming signal from device .

2. Polling Communication
In PC system ,the polling has some transfering between terminal and CPU in case data is byte type that keyborad send coming.
The polling will be through checking signal from device such as keyboard polling as user press button.
For CPU polling to check signal,we call " Wet Poll" that will be loss time 90% , We can avoid the cycle time lose with using polling technique that call is "Round Robin"
About in VB programming ,we use Timer control for checking data from Serial port .


      MSComm control has Event which use one only in Oncomm Event that communication is interrupt .
For programming in Serail Port communication normally using comm event are comEvReceive,comEvSend if it is communication in MODEM then using many one in check signal ,please refer help or MSDN.

MSComm Elemant .

Setting Communication of Serial Port

- ComPort mean is Port interfacing RS-232 (Com1,Com2)
- Setting mean is Baud,Parity,Data(number of bits),Stop Ex. "1200,n,8,1 "
- HandShaking mean is we can define to 4 type 1.comNone 2.comXonXoff 3. comRTS 4.comTRSXonXoff

Using Buffer for receive and sent data

-InBuffersize mean is define buffer for receive data.
-OutBuffersize mean is define buffer for send data.
- Rthreshold mean is define to occur in Event-driven for send data
- Sthreshold mean is define to occur in Event-driven for receive data
- Inputlen mean is number of data to read in a buffer receive data
- EOFEnable mean is End-Of-File(EOF)

About Hardware

- ParityReplace
mean is character value instead of occur Parity Error
- NullDiscard mean is define in receive or not "NULL CHARACTER"
- RTSEnablemean is define signal RTS (Request To Send)
- DTSEnablemean is define signal DTR(Data Terminal Ready)

Define MSComm Control properties to connect port

1. Property CommPort
Select comm port that we are using it thus as source code below
In this using is Com1

2. Property Settingsmean is setting value for receive /send data that will know baud rate of device which connect ,how much thus as detail below
MSComm1.Settings="Baud(rate for receive/send data),Parity(N,Number of bits,bits Stop"

3.Property InputLenmean is define input buffer size when data incoming

4. Property PortOpen mean is open port to communucate Open =True ,Close =False

5. Property Rthresholdmean is do activate occur with Event-driven when has data in receive buffer (comm port) that active the CommEvent in procedure OnComm Event
MSComm1.Rthreshold =1

We can using the MSComm properties allow the setting of communication parameters including port selection and port enabling functions. or we will be write to your code
From detail above, we will programming in VB procedure at Sub Form_Load() or create new Sub in case call in the future

Private Sub Form_Load()
       MSComm1.Rthreshold =1
End Sub

How to communicate with Serial Port
from progrmming above is define start value to comm port and open port to communicate with RS-232 So, we can get data and send data with Property Output and Input
       Output =send data to serial port
       Input =receive data from port but this will being command to write at Event Property OnComm that has in Sub MSComm_OnComm that read data from serial port or RS-232

If we want print data to port while we are typing data to keyborad ,we use event the KeyPress of Control TextBox

Sub txtRXTX_KeyPress(KeyAscii As Integer)
End Sub

For Prooerty Input goto Event OnComm

Private Sub MSComm1_OnComm()
Dim StrData As Variant
'define variable type as it is variant
End Sub

first time i did programming receive data with Property Input if we get direct data from this ,we will get refuse data ,so we may be using a filter function name is DataShow as write thus as below

DataShow function

Public Static Sub DataShow(TextShow As Control, Data As String)
Const SpeedBaud = 16000
Dim LngSize As Long, X
LngSize = Len(TextShow.Text)
If TermSize > SpeedBaud Then
       TextShow.Text = Mid$(TextShow.Text, 4097)
       LngSize = Len(TextShow.Text)
End If
TextShow.SelStart = SpeedBaud
       X = InStr(Data, Chr$(8))
       If X Then
              If X = 1 Then
              TextShow.SelStart = SpeedBaud - 1
              TextShow.SelLength = 1
              Data = Mid$(Data, X + 1)
       Data = Left$(Data, X - 2) & Mid$(Data, X + 1)
       End If
       End If
Loop While X
       X = InStr(Data, Chr$(10))
       If X Then
       Data = Left$(Data, X - 1) & Mid$(Data, X + 1)
       End If
       Loop While X
       X = 1
       X = InStr(X, Data, Chr$(13))
       If X Then
       Data = Left$(Data, X) & Chr$(10) & Mid$(Data, X + 1)
       X = X + 1
       End If
Loop While X
TextShow.SelText = Data
TextShow.SelStart = Len(TextShow.Text)
End Sub

Copy code in ListBox below.

Call function DataShow :

Call DataShow Text1,(StrConv((StrData),vbUnicode))

      that has character show onTextBox ,about programming in communicate to devices that is receive/sent with Acsii by Serial RS-232 such as load cell ,temperature module we can setting value merely Com Port with its device protocol .

Using Even in OnComm() procedure

this link is table of MS Comm control we get Event in the table to programming in OnComm() procedure with Select case condition as thus code below

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
       Case comEvReceive
       Dim Buffer As Variant
              Buffer = MSComm1.Input
              ShowData txtRXTX, (StrConv((Buffer), vbUnicode))
       Case comEvSend :
'here put your condition that you want
       Case comEvCTS   
       Case comEvDSR
       Case comEvCD
       Case comEvRing
       Case comEvEOF
       Case comBreak
       Case comCDTO
       Case comCTSTO
       Case comDCB
       Case comDSRTO
       Case comFrame
       Case comOverrun
       Case comRxOver
       Case comRxParity
       Case comTxFull
End Select
End Sub

 I have get exemple program which is control step motor with command Ascii communicate to Micro controller
  A99 ' accelerate setting
' speed setting
' step setting
'direction setting
' go ,it is start motor command
We are assume use a button in my porgram thus as below

    Private Sub Command1_Click()
          MSComm1.Rthreshold =1
          If MSComm1.PortOpen Then
              MSComm1.Output ="A99 V3 D5000 H- G " 
' character lastest will be press space bar 1 time before put symbo "
          End If
   End Sub

     and this exemple program about MODEM communicate with serial port.

        Private Sub Form_Load()
           Dim strValue As String
' define Buffer value from Modem
           MSComm1.CommPort = 1
' select Com1
           MSComm1.Settings = "9600,n,8,1"
' setting parameter in communicate with com port
            MSComm1.InputLen = 0
'define size of Input buffer (comming with Modem)
           MSComm1.PortOpen = True
' command open port
           MSComm1.Output = "ATV1Q0" & Chr&(13)
'send AT command to Modem
             strValue = strValue & MSComm1.Input   
' get input data of Com Port
              Loop Until InStr(strValue, "OK" & vbCrLf)
              MSComm1.PortOpen = False 'Close port
         End Sub

    Final Notes

    - Port selection and enable functions can be programmed in the "load" and "unload" subroutines in the "form" object or they may be controlled by radio buttons or pull down menus.

    - When recieving data from the serial port, be sure to wait for the correct number of characters to be recieved in the serial buffer. Check your serial port programming manual for the correct number of characters to be recieved and add one for the carriage return. The command sends both a carriage return and line feed thus two must be added to the number of characters expected.

    - In many cases it may be desired to send a string incorporating a comand and some variable. For example, the "GO" command outputs to port A, the integer value position. If the position is a variable 1500, a string to set the port to the value of this variable would look like;

MSComm1.Output = " GO 1500" + Chr(13)
The Chr(13) is a carriage return.

You may be many question how to communicate with microcontroller ,So, we can use concept on above to apply in your project ,process for communicate is like my exemple programming but you will be know protocol of those device.

   Serail Port Project Page

   VB Communication

  VB interface Stepping Driver IM483IE

Copyright (c) 2000-2008 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com