Ports.SerialPort freezes
-
Hi I'm using the SerialPort class (.net 2.0) for rs232 communications. Unfortunately, an it freezes up my program when I try to set a parameter for it after it's been opened. First I initialize the port:
SerialPort port = new SerialPort(comname, 19200);
port.Open();
port.ReceivedBytesThreshold = 63;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);Then I do some communications stuff. So far so good. The freeze occurs when I try to use the port for some other process.
if (port.IsOpen)
{
port.DiscardInBuffer(); // will freeze here
port.DiscardOutBuffer(); // here
port.Close(); // here
}port.ReceivedBytesThreshold = 1; // or here
port.Open();Any of the commented line freeze up my program as if in a blocking state, and the code doesn't throw an exception of any kind, it just freezes each and any thread currently running. Does anyone have any clues what's happening here?
-
Hi I'm using the SerialPort class (.net 2.0) for rs232 communications. Unfortunately, an it freezes up my program when I try to set a parameter for it after it's been opened. First I initialize the port:
SerialPort port = new SerialPort(comname, 19200);
port.Open();
port.ReceivedBytesThreshold = 63;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);Then I do some communications stuff. So far so good. The freeze occurs when I try to use the port for some other process.
if (port.IsOpen)
{
port.DiscardInBuffer(); // will freeze here
port.DiscardOutBuffer(); // here
port.Close(); // here
}port.ReceivedBytesThreshold = 1; // or here
port.Open();Any of the commented line freeze up my program as if in a blocking state, and the code doesn't throw an exception of any kind, it just freezes each and any thread currently running. Does anyone have any clues what's happening here?
-
Ok, I already found the problem, for some strange reason the port needs to be reconstructed:
port = new SerialPort(port.PortName, port.Baudrate);
Weird...
Not Wierd. You'll actually find that this happens throughout the Framework. Try looking at the Font class for another example of immutable, or partially immutable, objects.
Dave Kreskowiak Microsoft MVP - Visual Basic