System.IO.Ports.SerialPort Read Issue

S

Scott McNair

I have an external RFID/keypad device that uses a serial port for
communication. Whenever I attempt to read input from the keypad, I run
into the following problem.

Press 1: Nothing appears
Press 2: One appears
Press 3: Two appears
[etc]

I've adjusted the ReceivedBytesThreshold to various things between 0 and
20; 1 works the best but it's still giving me this issue. I also changed
the ReadBufferSize around from its original value of 4096 to numbers as
high as 65536, but with no luck.

Does anybody have any additional suggestions?

Oh, it should be noted for the sake of posterity that when I used the RFID
portion to read an 8-digit code off my RFID card, it correctly reads and
reports the 8-digit code.

Regards,
Scott
 
D

Dick Grier

Show your code. There are a couple of things that might be happening. Is
the data binary or ASCII text?

--
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.
 
S

Scott McNair

Show your code. There are a couple of things that might be happening.
Is the data binary or ASCII text?

Well, I solved one problem (I had a couple of lines of code reversed)...
glad you asked me to post the code, or I would have missed it!

This does however lead to a second problem that I'd neglected to
mention, and that is that sometimes it doesn't pick up a keypress... I
have to hit it 3 or 4 times sometimes in order for the keypress to
register.

I think I know the cause of that as well... if I wait a second or so
between keystrikes, it works every time. If I try to hurry up when
punching the buttons, it doesn't. So obviously there's some sort of
time buffer that it's running against. I'm positive it's not the fault
of the controller, since there's a version of the code that uses MSCOMM
in VB6 that works fine with this controller.

Anyway, here's my code. To answer your question, the data being
returned is ASCII:

Dim SerialPort As New System.IO.Ports.SerialPort

Public Sub OpenReader()
If SerialPort.IsOpen Then
SerialPort.Close()
End If

With SerialPort
.ReceivedBytesThreshold = 1
.PortName = "COM1"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Open()
End With
End Function

....and here's the portion that actually performs the read:

sstr = Microsoft.VisualBasic.Left(InBuffer, 1)

If UCase$(sstr) = "A" Then
'This is the equivalent of "*" key - clear buffer
KeyPadInputBuffer = ""
ElseIf UCase$(sstr) = "B" Then
'This is the equivalent of the "#" key - process buffer
RaiseEvent HIDKeypad(KeyPadInputBuffer)
KeyPadInputBuffer = ""
Else
'This is a 0-9 keypad entry - append to string
KeyPadInputBuffer &= sstr
End If
 
D

Dick Grier

You'll have to add some debugging code (Debug.Print or Debug.PrintLine) to
see "what's up." All you show is the decoding, not the receive data code.
Are you using ReadExisting or Read? Whatever, print it to the Immediate
window, so that you can view data as received.

The SerialPort object DataReceived event isn't much different than the
OnComm event (as generated by receive data). However, depending on what you
are doing in your HIDKeypad event code, there may be an issue cause by
cross-thread calls. Do you attemp to display data in this event? You
cannot do so directly, but need to use a Delegate.

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.
 
S

Scott McNair

You'll have to add some debugging code (Debug.Print or
Debug.PrintLine) to see "what's up." All you show is the decoding,
not the receive data code. Are you using ReadExisting or Read?
Whatever, print it to the Immediate window, so that you can view data
as received.

I'm using ReadExisting. I put in a Debug.Print, and noticed that sometimes
it's throwing in an extra vbCR or vbLF. I had been trapping by checking
for the buffer's length and only taking the leftmost character, but the
data could be coming like "LF1CRLF" (line-feed before the character).
Before I had been taking the left-most character, but I told it to just
strip out all CR and LF entries, and it seems to be working fine now.
The SerialPort object DataReceived event isn't much different than the
OnComm event (as generated by receive data). However, depending on
what you are doing in your HIDKeypad event code, there may be an issue
cause by cross-thread calls. Do you attemp to display data in this
event? You cannot do so directly, but need to use a Delegate.

In my troubleshooting app I'm writing the results to a label, but I'm
Invoking the results into the label so no worries there. In the actual
application I'm comparing the results to an XML file.

Thanks for your help,
Scott
 

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