SerialPort.Open error

I’ve developed a C#.NET application that communicates with an Arduino using the SerialPort.Open method and that works great. However, I recently wanted to use the same code in Unity and tried both in 3.5 and 4 and both versions throw the following exception (.NET part of stack trace included)

System.IO.IOException: The port `COM10’ does not exist.
at System.IO.Ports.WinSerialStream.ReportIOError (System.String optional_arg) [0x00000] in :0
at System.IO.Ports.WinSerialStream…ctor (System.String port_name, Int32 baud_rate, Int32 data_bits, Parity parity, StopBits sb, Boolean dtr_enable, Boolean rts_enable, Handshake hs, Int32 read_timeout, Int32 write_timeout, Int32 read_buffer_size, Int32 write_buffer_size) [0x00000] in :0
at (wrapper remoting-invoke-with-check) System.IO.Ports.WinSerialStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
at System.IO.Ports.SerialPort.Open () [0x00000] in :0
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()

The port obviously exists as I can open up my C# application and communicate with the Arduino fine, but Unity always says the port does not exist.

To eliminate any error on my part, after using the string “COM10”, I tried using the SerialPort.GetPortNames() method and use the string directly form that in .Open, but it was to no avail.

I exit my application before trying it out in Unity. No matter how many times I close my application and how many times I try Unity, the situation is the same.

Me too I have the same problem,
Thanks in advance for help

Problem Solved!
Make sure that the name of the serial port is less than 10 (COM10 is not accepted):
if not change its name in System - Hadware configuration :
see picture attached


If you have the same problem, make sure that you use .Net 2.0 in unity3D - palyer settings (not .NET 2.0Subset)

3 Likes

Dude! Thanks a lot!!! You just saved my life!

amibest thank you! I’ve spent a few hours trying to figure out why it works on the books but not in my code – I second zweihander111!!!

@amibest : Thanks dude…thanks a lot!!!

awesome dude … but y the com port name should not be more than 10 ?

This discussion explains how to connect to ports greater than 10

1 Like

This is not working for me . Did u give it a Try ?

1 Like