Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with Understanding MSComm & VB6

713 views
Skip to first unread message

Abdallah Magar

unread,
Oct 2, 2000, 3:00:00 AM10/2/00
to
A few years back I wrote some code that got information from modems using
the following code in OnComm event :

Open com port somewhere else in case you are wondering...

Private Sub MSComm1_OnComm()
MSComm1.InputLen = 0
If MSComm1.InBufferCount Then instring$ = MSComm1.Input
List1.AddItem instring$


etc....

This was in VB4 32bit Now I find (in vb6 ent W2K) it will not produce
anything from the com port, and have to use a lot of other subs to get it to
work. Can someone either give me a simple explanation or code that is not 3
or 4 sub calls long?

Regards

Ismael Yahya
mailto:i...@sakina.com


Carlos

unread,
Oct 2, 2000, 3:00:00 AM10/2/00
to
Try looking at the following,


' Initialise Comm Settings ( either in Form_Load or on the control
itself )

gvarSettingsCommPort = GetSetting("Datalogger", "Settings", "CommPort",
"2")
MSComm1.CommPort = gvarSettingsCommPort
MSComm1.Settings = "9600, N, 8, 1"


To start the comm port : ( input is stored in a byte array for my app ) (
MSComm1.RThreshold = 1 - starts the port receiving. This can be any value
other than 0 to start. It defines how many bytes are received before
triggering the oncomm event. Use with the .PortOpen property.

Private Sub CmdReceiveData_Click()
On Error GoTo CmdReceiveData_error
Screen.MousePointer = 11
If Not gboolCmdReceiveData_Toggle Then
Status = "Ready To Receive"
StatusBar1.Panels(1).Text = "Ready To Receive"
Erase gbyteInput_Data
MSComm1.RThreshold = 1
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True
CmdReceiveData.Caption = "Cancel"
gboolCmdReceiveData_Toggle = True
Else
Status = "Halted"
StatusBar1.Panels(1).Text = "Halted"
If MSComm1.PortOpen Then MSComm1.PortOpen = False
CmdReceiveData.Caption = "Receive Data"
gboolCmdReceiveData_Toggle = False
End If
Screen.MousePointer = 0
Exit Sub

CmdReceiveData_error:
If Err = 68 Then
MsgBox "Comm Port Not Available"
CmdReceiveData.Caption = "Receive Data"
StatusBar1.Panels(1).Text = "Comm Port Not Available"
gboolCmdReceiveData_Toggle = False
Screen.MousePointer = 0
Exit Sub
End If
MsgBox Error$
CmdReceiveData.Caption = "Receive Data"
Status = "Halted"
StatusBar1.Panels(1).Text = "Halted"
gboolCmdReceiveData_Toggle = False
Screen.MousePointer = 0
Exit Sub
End Sub


To process response use:

'*************************************************
'The OnComm event is used for trapping
'communications events and errors.
'*************************************************
'
Private Static Sub MSComm1_OnComm()
Dim EVMsg$
Dim ERMsg$

'--- Branch according to the CommEvent Prop..
Select Case MSComm1.CommEvent
'--- Event messages
Case MSCOMM_EV_RECEIVE
Call Get_Data
Case MSCOMM_EV_SEND

'Case MSCOMM_EV_CTS
' EVMsg$ = "Change in CTS Detected"
'Case MSCOMM_EV_DSR
' EVMsg$ = "Change in DSR Detected"
'Case MSCOMM_EV_CD
' EVMsg$ = "Change in CD Detected"
'Case MSCOMM_EV_RING
' EVMsg$ = "The Phone is Ringing"
'Case MSCOMM_EV_EOF
' EVMsg$ = "End of File Detected"

'--- Error messages
Case MSCOMM_ER_BREAK
EVMsg$ = "Break Detected. Please Check Comm Port Settings And
Retry"
'Case MSCOMM_ER_CTSTO
' ERMsg$ = "CTS Timeout"
'Case MSCOMM_ER_DSRTO
' ERMsg$ = "DSR Timeout"
'Case MSCOMM_ER_FRAME
' EVMsg$ = "Framing Error. Please Retry"
'Case MSCOMM_ER_OVERRUN
' ERMsg$ = "Overrun Error"
'Case MSCOMM_ER_CDTO
' ERMsg$ = "Carrier Detect Timeout"
Case MSCOMM_ER_RXOVER
ERMsg$ = "Receive Buffer Overflow"
'Case MSCOMM_ER_RXPARITY
' EVMsg$ = "Parity Error"
Case MSCOMM_ER_TXFULL
ERMsg$ = "Transmit Buffer Full"
Case Else
ERMsg$ = "Unknown error or event"
End Select

If Len(EVMsg$) Then
'--- Display event messages in label
Beep
MsgBox EVMsg$
EVMsg$ = ""

ElseIf Len(ERMsg$) Then
Beep
ERMsg$ = ""
End If

End Sub

"Abdallah Magar" <ma...@dmn.com.au> wrote in message
news:mp_B5.35$Ip....@nsw.nnrp.telstra.net...

Dick Grier

unread,
Oct 2, 2000, 3:00:00 AM10/2/00
to
Hi,

InputLen should be outside of the OnComm sub.

Does the Rthreshold property = 1?

You code should look something like this:

Private Sub MSComm1_OnComm
Static Buffer As String
Dim CRPosition As Integer
Buffer = Buffer & MSComm1.Input
CRPosition = InStr(Buffer, vbCR)
If CRPosition > 1 Then
List1.AddItem Left$(Buffer, CRPosition - 1)
Buffer = Mid$(Buffer, CRPosition + 1)
End If
End Sub

Replace vbCr with vbCRLF, if the strings that are being received are
delimited by both a carriage return AND a line feed.

For lots more information, and working code examples, you might be
interested in getting a copy of my book. See below.

--
Richard Grier (Microsoft Visual Basic MVP)
Hard & Software
12962 West Louisiana Avenue
Lakewood, CO 80228
303-986-2179 (voice)
303-986-3143 (fax)
Author of Visual Basic Programmer's Guide to Serial Communications, 2nd
Edition ISBN 1-890422-25-8 (355 pages).
For information look on my homepage at http://www.hardandsoftware.net.
Use the Books link to order. For faster service contact the publisher at
http://www.mabry.com/vbpgser.

0 new messages