473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
May 2 '06 #1
5 2233
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.

May 2 '06 #2
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
May 2 '06 #3
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.AsynchronousOperation s
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.AsynchronousOperation s
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

May 2 '06 #4
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.
May 2 '06 #5
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.Tim eout.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
May 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: seankub | last post by:
I am trying to read data from com1 and I am using the new serialport control. I downloaded the script from microsoft that does a readline from the comm port, but I keep getting the "operation timed...
6
by: DraguVaso | last post by:
Hi, I'm experimenting with the Serial Port in VB.NET 2005. Although it isn't that easy to get any feedback from my COM-port as I thought it would be... How can I read all the Data that the...
4
by: Ben Kim | last post by:
Hello all, Does anyone have an example on how to implement the new SerialPort Class in VB.NET? I have been able to create the class, send (.WriteLine) to the port and Read from the port but...
2
by: Jay | last post by:
Extracted from the C# example in http://msdn2.microsoft.com/en-us/library/s14dyf47.aspx... public static void Main() { string name; string message; StringComparer stringComparer =...
3
by: Mugunth | last post by:
Hi, I've a strange problem with serial port class of .NET Framework 2.0 I use com0com (Null Modem COM Port emulator) to emulate a virtual port. This application creates virtual com port pairs...
0
by: nmsreddi | last post by:
Hi friends I am working on serialport in c# ,i am using C#2005 i have successfully done the serial communication with GSM modem and able to send and receive data , the main problem ,the serial...
10
by: sklett | last post by:
I'm trying to send some printer commands (ZPLII) to an attached USB printer using the SerialPort component. I didn't get very far at all. In fact I haven't gotten anywhere. The first problem I...
0
by: cronusf | last post by:
I set up two virtual COM ports 3 and 4 using com0com. I tried to test it with the following program. However, the DataReceived event handlers never get called. Can anyone with SerialPort class...
9
by: jensa | last post by:
Hi, I am currently writing a simple form application in C# that uses the SerialPort class (C# 3.5). But I have put my head in the wall several times to solve a simple problem. I have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.