Sunteți pe pagina 1din 2

BCL Team Blog : SerialPort and DataReceived Event [Ryan Byington] http://blogs.msdn.com/bclteam/archive/2006/05/15/596656.

aspx

Welcome to MSDN Blogs Sign in | Join | Help

BCL Team Blog


Base types, Collections, Diagnostics, IO, RegEx...

SerialPort and DataReceived Event [Ryan Byington]


I have seen a few customers complain that their DataReceived event handler was never getting called and I thought I would
share their problems here so you can learn from their mistakes. The problems revolve around the port not being open. This
is sometimes as obvious as not calling SeiralPort.Close if you expect your DataReceived handler to be called but
sometimes it is not as obvious as the GC can Close the SerialPort.
One customer had something like the following code:
shared Dim WithEvents mySerialPort as SerialPort

public shared sub SendText(text as string)


mySerialPort = new SerialPort("COM1")
mySerialPort.Open()
mySerialPort.WriteLine(text)
mySerialPort.Close()
end sub

public shared sub MySerialPortDataReceviedHandler( _


sender as Object, _
e As SerialDataReceivedEventArgs) _
Handles mySerialPort.DataReceived
'Read here...
end sub
On the surface this may seem like everything should work. However the DataReceived event handler will not get called if
the port has been closed. Also, the handler will only get called for data received since the port has been opened and the
MySerialPortDataReceviedHandler is setup to handle the DataReceived event. So in this example the customer’s
DataReceived handler will only get called for data received while code is executing between the Open call and the Close
call in the SendText method. This is a pretty small window so it is unlikely that the customer would ever see their handler
get called. The fix here is to not open and close the port for every write but open the port when the app is starting up and
close the port when it is shutting down.

Another customer had the following code:


public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM1");

mySerialPort.DataReceived += MySerialPortDataReceviedHandler;
mySerialPort.Open();

Console.WriteLine("Press any key to continue...");


Console.ReadKey();
}

private static void MySerialPortDataReceviedHandler(


object sender,
SerialDataReceivedEventArgs e)
{
//Read here...
}

The problem here is a lot more subtle, the customer opened the port setup MySerialPortDataReceviedHandler to handle
the DataReceived event and makes not calls to SerialPort.Close(). However the GC can actually close the port after Open
has been called since there are no more references to mySerialPort after this call. An object is eligible to be GC’d as soon

1 of 2 3/12/2010 12:38 PM
BCL Team Blog : SerialPort and DataReceived Event [Ryan Byington] http://blogs.msdn.com/bclteam/archive/2006/05/15/596656.aspx

as there are no more references to the object. You can read more about this at http://blogs.msdn.com/cbrumme/archive
/2003/04/19/51365.aspx. The fix is to make a call to SerialPort.Close() at the very end of the Main method. This keeps the
mySerialPort alive until the end of this method.

Published Monday, May 15, 2006 8:00 AM by BCLTeam


Filed under: System.IO

Comments

Monday, May 15, 2006 12:50 PM by snaveen


# re: SerialPort and DataReceived Event [Ryan Byington]

This is true (C# Code )if you have /DEBUG set to false in c# compiler or if you don't use VS.NET.
If you have try the same with VS.NET the object would not be collected.
Tuesday, October 10, 2006 4:40 PM by BCLTeam's WebLog
# Top 5 SerialPort Tips [Kim Hamilton]

The SerialPort class requires some “warming up” time for our users coming from VB6 or other
non-.NET
New Comments to this post are disabled

© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement

2 of 2 3/12/2010 12:38 PM

S-ar putea să vă placă și