469,641 Members | 1,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

SerialPort in C# Handle error

When I try to use a System.IO.Ports.SerialPort object to read from a serial port, half of the time I have no problems. However, after I successfully read from the serial port, then close the connection, either when re-opening it (in the same instance or a new instance of my program) I get either an IOException "The Handle is Invalid" exception or an ArgumentException "The portName is invalid.

Now, I get the port name by doing this:

Expand|Select|Wrap|Line Numbers
  1. String[] ports = SerialPort.GetPortNames();
So I know it is a valid port and the portName shouldn't be invalid.

As for the invalid handle, I open() and close() my streams before and after I read them. This is my SerialPort init section:

Expand|Select|Wrap|Line Numbers
  1. sin = new SerialPort();
  3. sin.PortName = comboBox1.SelectedItem.ToString();
  4. sin.BaudRate = 19200;
  5. sin.Parity = Parity.Odd;
  6. sin.DataBits = 8;
  7. sin.StopBits = StopBits.One;
  8. sin.Handshake = Handshake.None;
  9. sin.ReadTimeout = 500;
which is all set from the specs of the device I am trying to read data from. comboBox1 is where I put my ports[] array for the user to choose which port. It is defaulted on the first entry (and I only have one installed so its always the first port)

When I don't receive the error, the program runs fine, but when I press the Close Connection button or when I try to re-start the connection I get one of the two errors; more often than not it's the invalid handle error.

Expand|Select|Wrap|Line Numbers
  1. The given port name is invalid.  It may be a valid port, but not a serial port.
  2. Parameter name: portName
ArgumentException Stack
Expand|Select|Wrap|Line Numbers
  1.    at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
  2.    at System.IO.Ports.SerialPort.Open()
  3.    at SpO2_Test.Form1.getData() in C:\data\SpO2\SpO2_Test\SpO2_Test\Form1.cs:line 83
Expand|Select|Wrap|Line Numbers
  1. The handle is invalid.
IOException Stack Trace
Expand|Select|Wrap|Line Numbers
  1.    at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
  2.    at System.IO.Ports.SerialStream.get_BytesToRead()
  3.    at System.IO.Ports.SerialPort.get_BytesToRead()
  4.    at SpO2_Test.Form1.getData() in C:\data\SpO2\SpO2_Test\SpO2_Test\Form1.cs:line 95

It is an external USB serial port which I am half-temped to blame. Though, I would expect it to close the stream correctly as well so I really have no clue. When I do get one of these errors, I disable the USB device, remove it, then plug it in again to fix the problem; half of the time, that works. If it doesn't, I do it again until it does.

Really, I would appreciate any help in this matter. Thanks.
Oct 17 '06 #1
1 12361
Well, we installed a PCI Serial/Parallel card and it works fine, now. Though, the Parity that the device says it has must be switched in my program. (docs say odd parity and .net works only when even set.)

Note to self: Don't trust USB Serial Ports...
Oct 18 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Sezhman | last post: by
7 posts views Thread by hansiman | last post: by
reply views Thread by Jay | last post: by
3 posts views Thread by bhumithakker | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.