SerialPort class: @#%&! who wrote the feature list?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

back in 1978 (!) the VAX/VMS serial line driver offered everything a
developer needs to develop protocols of all kinds:
- read x bytes
- read to end of line
- read to special character
- read to one in a set of special characters
- and of course synchronous and asynchronous using events and including
timeouts.
And even before RSX-11 could do this out of the box.

The new serialport class is a joke - no documentation, no features, no
virtual COMx!

For example:
how does the DataReceived event know whether to fire at the end of a line
(LF received) or at any byte received ?

Mr. Dave Cutler: please kick the .NET feature them and show them what you
have done 40 years ago. There's nothing new to invent - just re-engineer!

angrycoder
 
herbert wrote:

Perhaps I am missing something, but:
- read x bytes
SerialPort.Read

- read to end of line
SerialPort.ReadLine

- read to special character
SerialPort.ReadTo

- read to one in a set of special characters

Don't know
- and of course synchronous and asynchronous using events and including
timeouts.

Since you can access the data in the serial port using a stream via the
BaseStream property, perhaps there is an asynchronous method using
streams.
- how does the DataReceived event know whether to fire at the end of a line
- (LF received) or at any byte received ?

The DataReceived event, when raised, passes in an instance of
SerialDataReceivedEventArgs which exposes an EventType property which
is an instance of the SerialData enum. This enum has two values:
"Chars" or "Eof". Is that not what you're looking for? The only thing
from your list that wasn't there was the "read to one in a set of
special characters".

Are you having a specific problem with the SerialPort class?

Hope this gives you some ideas.
 
Chris,

thanks a lot. I'll try.

And yes, I am missing samples and/or HowTo articles in the online docu.

I tried the asynch pattern through
myCom1.BeginRead
which is not accepted by VS.2005.
Shouldn't every method support the begin/end asynch .NET pattern ?

herbert
 
I believe that *any* method can be called asynchronously using code
similar to the following which I took from MSDN2:

Imports System
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class AsyncDemo
' The method to be executed asynchronously.
Public Function TestMethod(ByVal callDuration As Integer, _
<Out> ByRef threadId As Integer) As String
Console.WriteLine("Test method begins.")
Thread.Sleep(callDuration)
threadId = Thread.CurrentThread.ManagedThreadId()
return String.Format("My call time was {0}.",
callDuration.ToString())
End Function
End Class

' The delegate must have the same signature as the method
' it will call asynchronously.
Public Delegate Function AsyncMethodCaller(ByVal callDuration As
Integer, _
<Out> ByRef threadId As Integer) As String
End Namespace


Imports System
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class AsyncMain
Shared Sub Main()
' The asynchronous method puts the thread id here.
Dim threadId As Integer

' Create an instance of the test class.
Dim ad As New AsyncDemo()

' Create the delegate.
Dim caller As New AsyncMethodCaller(AddressOf
ad.TestMethod)

' Initiate the asynchronous call.
Dim result As IAsyncResult = caller.BeginInvoke(3000, _
threadId, Nothing, Nothing)

Thread.Sleep(0)
Console.WriteLine("Main thread {0} does some work.", _
Thread.CurrentThread.ManagedThreadId)

' Call EndInvoke to Wait for the asynchronous call to
complete,
' and to retrieve the results.
Dim returnValue As String = caller.EndInvoke(threadId,
result)

Console.WriteLine("The call executed on thread {0}, with
return value ""{1}"".", _
threadId, returnValue)
End Sub
End Class

End Namespace
 
Hi,
Shouldn't every method support the begin/end asynch .NET pattern ?
<<

No. It is a serial class, not something else. It uses a worker thread for
reading serial data, so adding a delegate method (another thread) is
superflous, IMO.

You can write a wrapper (simply, IMO) to do the things that you find to be
missing. BTW, you can use the NewLine property to define a special charater
to be used for the ReadLine and WriteLine methods, if you want. However, if
you want to mix a special charcter or characters... You can do what I
recommend anyway. Simply write your own parsing code that is called from as
a result of data received in the OnComm event.

I have example code in my book that covers 99.9% of every simple serial
requirement. If there is something specific that you want to do, post a
message to the newsgroup. If I can provide an answer with a reasonable
amount of effor, I will try to do so.

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.
 
Thanks gentlemen,

what you tell me is
1) Microsoft is unable to complete documentation and samples six months
after product shipment (that should be fairly easier accomplished than
shipping code bugfix)
2) cash-loaded microsoft is unwilling to pay some MVPs to write HOWTO articles
3) instead I have to buy a book (which my company doesn't like at all)
and now for the funny part, following the advice on Mr. Grier's web site
4) Amazon.com tells me that this book cannot be shipped to Austria
5) and Spyware Doctor flags Mr. Griers web site as dangerous.

And all I wanted to know is why the following event handler only returns two
bytes echoed from the modem("AT" instead of "ATE1" and "OK"):


com1 = New SerialPort("COM3") 'COM3 = internal modem in Laptop
com1.BaudRate = 9600
com1.DataBits = 8
com1.Parity = Parity.Even
com1.StopBits = StopBits.One
com1.ReadBufferSize = 16

AddHandler com1.DataReceived, AddressOf DataReceivedHandler

Try
com1.Open() 'open the port
com1.WriteLine("ATE1") 'modem should reply "OK" 0x4F 0x4B
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
'wait forever!
Finally
com1.Close()
End Try
Console.WriteLine("Hit ENTER to exit.")
Console.ReadLine()
End Sub

Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs)
If e.EventType = SerialData.Chars Then 'byte received
Console.WriteLine(vbCrLf)
Console.WriteLine("byte rcvd.")

Dim rcvdByte As Integer = com1.ReadByte()
Console.WriteLine("Received (String): " & rcvdByte.ToString)
Console.WriteLine("Received (Hex): " & String.Format("{0:X2}",
rcvdByte))
End If

End Sub

thanks herbert
 
Back
Top