SerialPort.Open throws exception but only if data is sent to com p

  • Thread starter Thread starter Henryk
  • Start date Start date
H

Henryk

I'm just exploring c# for the fun of it (being a long time MFC and WIN32
programmer).

I want to receive some binary data, which contains ascii values but is
wrapped by an STX (0x02) and an ETX (0x03) byte.

If I open the com port it just throws an IOException and I don't know why

The code looks like this:
this.rs232 = new SerialPort();

// setup the com port
this.rs232.BaudRate = 19200;
this.rs232.DataBits = 8;
this.rs232.Parity = Parity.None;
this.rs232.StopBits = StopBits.One;
this.rs232.PortName = "COM2";
this.rs232.ReadTimeout = 500; // 500 ms
this.rs232.WriteTimeout = 500; // 500 ms
this.rs232.Open()

If the transmitter is NOT connected the Open succeeds but as soon as I
connect the the transmitter to my com port the Open throws an exception.
Sometimes it succeeds but then the this.rs232.ReadByte failes with the same
exception.

What am I doing wrong? I'm trying this for quite a time. In WIN32 it would
be plain simple and I would be done with the whole program already ... :o(

Thank you for any hint!

Henryk
 
Here's the stack trace:

"System.IO.IOException" ... german description
Source="System"
StackTrace:
bei System.IO.Ports.InternalResources.WinIOError(Int32 errorCode,
String str)
bei System.IO.Ports.SerialStream.SetBufferSizes(Int32 readBufferSize,
Int32 writeBufferSize)
bei System.IO.Ports.SerialPort.Open()

Henryk
 
"System.IO.IOException"
Source="System"
StackTrace:
bei System.IO.Ports.InternalResources.WinIOError(Int32 errorCode,
String str)
bei System.IO.Ports.SerialStream.SetBufferSizes(Int32 readBufferSize,
Int32 writeBufferSize)
bei System.IO.Ports.SerialPort.Open()

Henryk
 
Ok, I got it...

Just to proof myself that it would be an easy task in WIN32 I changed some
of my existing programs and ... got the same error in WIN32 ReadFile.

Before I started my C# program I tested the data reception with
hyperterminal. Once hyperterminal crashed for whatever reason but I could
start it several more times without problem.

Because my test failed also with WIN32 ReadFile I just rebooted my pc and
both programs (C# and WIN32 test) work now.

Maybe someone of you has a good idea why this could happen?

Henryk
 
Sounds like a bug in the device driver for your hardware. Check the vendor
to see if there is an update.

--
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.
 
Back
Top