Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Wait for serial port

Reply
 
Old 10-30-2009   #1 (permalink)
kpg


 
 

Wait for serial port

Hi all,

What is the 'best' way to wait for data from a serial port.

I'm using ,net 2.0 system.io.port, and I have a working class that listenes
to the serial port's data received event.

I want to implement a WaitForResponse method that will send a command to
the com port and wait for the completed reply (a CR terminates a reply).

I don't want to sit there and poll it in a loop, and sleeping seems
inefficient to me (how long to sleep?). Concerning the sleep, I'm assuming
here that the com port is running on a different thread so if I sleep my
thread it will continue - is that correct?

Here are the highlights of the serial port class:

_RXBuffer as string

Public Sub New()
AddHandler comPort.DataReceived, AddressOf myDataReceived
End Sub

Public Sub WriteData(ByVal buffer As String)
comPort.Write(buffer)
End Sub

Private Sub comPort_DataReceived(ByVal sender As Object, _
ByVal e As SerialDataReceivedEventArgs)
Dim buffer As String = comPort.ReadExisting()
_RXBuffer &= buffer
End Sub


I want to add:

Public Sub WaitForResponse(ByVal buffer As String)
comPort.Write(buffer)
'wait here until there is some data to inspect
'then test if data has a CR
'is so exit sub
End Sub


This code must have been written a million times by now. Either my google
skills are impaired or it's a closely guarded secret :0


Thanks,
kpg

My System SpecsSystem Spec
Old 10-30-2009   #2 (permalink)
kpg


 
 

Re: Wait for serial port

could it be this simple????


Public Sub WaitForResponse(ByVal buffer As String)
comPort.WriteLine(buffer)
_RXBuffer = comPort.ReadLine()
End Sub

My System SpecsSystem Spec
Old 10-30-2009   #3 (permalink)
DickGrier


 
 

Re: Wait for serial port

Probably it is not that simple.

The answer is, it depends. (IMO)

If this is a command/response protocol -- that is, you send a command and
wait for a response, then a loop is exactly what you want. This loop could
be in a background thread, but more frequently (and efficiently) you simply
wait in a loop for the response, often including a call to
Thread.Sleep(somenumberofmS) in the loop (an occasionally,
Application.DoEvents, though usually isn't needed). This allows you to time
out of the loop should you not receive the expected response within a
reasonable time. This same looping operation could be in a thread, with a
callback. This sort of this is appropriate for complex responses that may
take an extended period of time to be received. More commonly, a
command/response pair would take a second or so, and the extra expense, in
both performance and code, of creating the thread isn't warranted.

If the response is asynchronous -- that is, not generated by a specific
command, then the most common design pattern is to use the DataReceived
event.

In most cases, you don't want to use the ReadLine method, which only works
with strings, terminated specifically by the newline characters that are
defined. It is a blocking call, and these (IMO) should be avoided.

Dick

--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.

My System SpecsSystem Spec
Old 4 Weeks Ago   #4 (permalink)
kpg


 
 

Re: Wait for serial port

Thanks for the reply.

It is a simple command/response model, I expect responses back in less
than one second.

As far as the ReadLine I've learned that the Serial port class does
accept a user defined end of line character, and a read timeout can be
set, so on the surface it sounds like it could be used without the
danger of falling in a black hole.

As far as it being a blocking call, that's what I'm trying to implement
with my WaitForResponse. But, yes, I tend to agree perhaps a loop would
be best, Why give up control when there is no need.

From my VB6 days I've use DoEvents a lot and Sleep almost never. Evey
time I sleep in a VB6 program the thing I'm waiting also sleeps. Same
thread I suppose. Is there a dnager of losing com port data while my
thread is sleeping?


kpg

"DickGrier" <dick_grierNOSPAM@newsgroup> wrote in
news:eEvRgNcWKHA.4816@newsgroup:
Quote:

> Probably it is not that simple.
>
> The answer is, it depends. (IMO)
>
> If this is a command/response protocol -- that is, you send a command
> and wait for a response, then a loop is exactly what you want. This
> loop could be in a background thread, but more frequently (and
> efficiently) you simply wait in a loop for the response, often
> including a call to Thread.Sleep(somenumberofmS) in the loop (an
> occasionally, Application.DoEvents, though usually isn't needed).
> This allows you to time out of the loop should you not receive the
> expected response within a reasonable time. This same looping
> operation could be in a thread, with a callback. This sort of this is
> appropriate for complex responses that may take an extended period of
> time to be received. More commonly, a command/response pair would
> take a second or so, and the extra expense, in both performance and
> code, of creating the thread isn't warranted.
>
> If the response is asynchronous -- that is, not generated by a
> specific command, then the most common design pattern is to use the
> DataReceived event.
>
> In most cases, you don't want to use the ReadLine method, which only
> works with strings, terminated specifically by the newline characters
> that are defined. It is a blocking call, and these (IMO) should be
> avoided.
>
> Dick
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Where's my serial port? Vista performance & maintenance
usb-to-serial port cleanup Vista hardware & devices
Serial Port Vista General
How Do I Determine The Serial Port In Use? Vista hardware & devices
USB to 9-pin serial port Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46