MSComm control, a mysterious behavior

T

thomasc1020

Hello,

I have a small problem playing with MSComm control in VB.NET 2003.
Below is the code I am working on.

Private Sub MSComm1_OnComm(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles
MSComm1.OnComm
Do
Buffer = Buffer & MSComm1.Input
Loop Until InStr(Buffer, Chr(3))

Correct = InStr(Buffer, Hi")

If (Correct) Then
Dim Form2 As New Form
Form2.MdiParent = Me.MdiParent
Form2.Show()
End If
End Sub


What I want to do is this:
Once the process goes into the 'MSComm1_OnComm' block, it should
1> read the MSComm input and
2> check if it contains "Hi".
3> If it does, display Form2.
4> If it does not contain "Hi", wait for next input and repeat 1>-3>

Below is the cord I am working on.
For some reason I don't understand, it repeats the 'If ~ End If'
clause 24 times, displaying 24 new forms, before it exits the
'MSComm1_OnComm' block. I have no idea why it reapeats the 'If ~ End
If' clause 24 times.

Should I 'reset' the OnComm event in some way in order to exit the
OnComm procedure?
Or Can it be related to the syntax using Parent/Child Form?

I've been struggling with this problem for about 10 days already.
Please help.
 
T

TBass

[snip]
Do
Buffer = Buffer & MSComm1.Input
Loop Until InStr(Buffer, Chr(3))
[/snip]

Ouch. Don't loop on Input. MsComm doesn't return one character at a
time. When you call the Input function, you get everthing it received
when it fired the OnComm callback -- that doesn't mean you got your
whole communication. You need to store what you got, check if it
contains the string you have, then either do what it is you want or
let OnComm finish and wait for the next call.

So...

Private myBuffer as String

Private Sub MSComm1_OnComm(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles
MSComm1.OnComm

'* APPEND THE LATEST RX TO THE BUFFER
myBuffer = Buffer + MSComm1.Input

'* DID WE GET THE WHOLE COMMUNICATION?
if ( InStr( myBuffer, "HI" ) ) then
'* YES. DO SOMETHING
MsgBox( "DO SOMETHING")

'* CLEAR OUT THE BUFFER FOR NEXT TIME
myBuffer

end if

End Sub


Check out Serial Port Complete by Jan Axelson. It's centered on vb6,
not .NET (unless it's been updated in the last 3 years), but it
contains everything you need to know about MSCOMM (a very nice
library).

Rock on,
T
 
D

Dick Grier

Hi,

You should NOT use a loop in the OnComm event. OnComm is generated each
time RThreshold (or more) characters are received. Thus, you can append new
data to a Static or form-level Private variable and parse that variable. A
loop will cause headaches and isn't useful.

I have LOTs of examples in my book, with suggestions of when to and not to
use loops to receive data -- loops can be used, usefully, but not in OnComm.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top