help with MSComm and serial device parser requested
I have a laboratory device that I am trying to write an interface to.
The device is an echo-back serial device with a command parser that acts very squirrely. If a recognized legal character is sent to the device, it will echo it back. If the character is not legal, it will send some other character (various error sequences).
the commands that are required are between 3 and 6 characters long. When the parser sees a proper and complete command, it resets itself for the next command. If you need to reset the parser, you can send a "Q" between 1 and 3 times. The parser also resets after sending an error.
The best way to detect an error is to watch the echo, and if it is not the same character, then you need to go back and send each character of the command again from the beginning.
I am just having no luck with this. I can communicated through a direct com1 connection in hyperterminal, and I can see the results when a command is rejected by the parser, but I can make it work in VB with the MSComm control.
Thank you
Chris Nogy
[1100 byte] By [
kaz1] at [2007-11-17 16:34:38]

# 1 Re: help with MSComm and serial device parser requested
Hi Chris,
Unfortunately, most VB programmers never do anything with the serial port, so you're stuck with me and maybe a few others.
There is actually a lot of good information on the MSComm control in the VB help files, but it takes a lot of reading and testing code.
You can use the following to send a single character, or several characters:
MSComm1.Output = MyChar
Before you send, the port must be open
MSComm1.PortOpen = True
When you are all finihsed, you should close the port:
MSComm1.PortOpen = False
If you have to change any of the serial port properties, the port myst be closed. You can check for this by:
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Now, reading from the port. I have always used a Do Loop with the DoEvents command so it does not tie up system resources:
Do
DoEvents
If MSComm1.PortOpen = True Then
Temp$ = FrmMain.MSComm1.Input
Temp1$ = Temp1$ & Temp$
Loop Until Temp$ = "N" Or EndReceive = True
MSComm1.PortOpen = False
In the above example, the letter N is used to indicate the end of the data. You will probably use something else. Maybe your equipment sends an EOF or you know that a certain number of characters are returned. I also had a cancel button which would set EndReceive to true so the user can stop the loop.
I set MSComm's InputLen =1, and used HandShaking=2.
You could also look at using the OnComm event, probably comEvReceive, to detect when data is received.
I hope this helps.
# 2 Re: help with MSComm and serial device parser requested
I use the MSComm control in a few of my projects and while
it seems simple a few of the properties present problems
in various forms one is Rthreshold. This property controls
the CommEvent (comEvReceive). It can't be 0 and if you set it to
low and are using comInputModeText then it might not be raised
if set to a high value ie 256 then you will get 256 CommEvents
but you might lose data also. My PIC projects return strings
like this for example (1 space,space,data value(decimal)crlf)
where 1 is channel #,data CRLF and i look for LF in my vb code
something along these lines
dim EOL as integer
dim InBuffer as string
inBuffer = inBuffer & MSComm1.Input
EOL = InStr(InBuffer,Chr$(10)
"Take out Data.
If EOL > 0 Then
Rdata = Mid$(InBuffer , EOL-2) " the EOL-2 Eliminates the CRLF unpritable characters
I have a better ActiveX Control that is easy to use and presents
none of the problems mscomm1 has and it also has a port monitor
where you can see th TD Pin output as well as RD Input in Dec and Hex plus it monitors all handshaking signals DTS... etc. and
its is free the only draw back is you can't use it in Apps that you
sale.If you send me your email I will email you a copy of exe
My email cdenton_38@hotmail.com
PS A great book for this purpose is Automating Science and Engineering Laboratories with Visual Basic Authors Mark F. Russo
and Martin M. Echols ( A Wiley Interscience Book)
# 3 Re: help with MSComm and serial device parser requested
Sorry to disagree with you ron, but using a doEvents and Do loop is totally the wrong approach for the Comm control. I did this when I first started out with the comm control and didn't understand it. I found that most times this method worked fine but occassionally it blew up big time.
The only sure way is to make use of the on_comm event that automatically get's fired as soon as any data comes back through the Com Port. You also have more control, becaue it gives a status back and so you can handle any errors accordingly.
You can always close the port in this event when you know you have all the data. use something like:
Private Sub MSComm_OnComm ()
Select Case MSComm1.CommEvent
' Handle each event or error by placing
' code below each case statement
' Errors
Case comEventBreak ' A Break was received.
Case comEventFrame ' Framing Error
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of
' chars.
Case comEvSend ' There are SThreshold number of
' characters in the transmit
' buffer.
Case comEvEof ' An EOF charater was found in
' the input stream
End Select
End Sub
place your code in the comEvReceive.
Also ensure that you have Threshold set to 1 otherwise you may endup with data in the buffer not coming through.
It's also very important that you have baud, parity etc set correctly.
Regards
Bill
