1

I have A SerialPort created, and can check it is not null, and that SerialPort.IsOpen() returns true. But when I check the BytesToRead Property I get a NullReferenceException.

NullReferenceException: Object reference not set to an instance of an object
  System.IO.Ports.WinSerialStream.get_BytesToRead ()
  System.IO.Ports.SerialPort.get_BytesToRead ()
  (wrapper remoting-invoke-with-check) 
  System.IO.Ports.SerialPort:get_BytesToRead ()
  ...

I can step through with the debugger and see that immedietly before the BytesToRead call, the Serial Port is Instantiated and Open, but the debugger can't reach certain properties, including BytesToRead.

"System.IO.Ports.SerialPort"
    base: "System.IO.Ports.SerialPort"
    BaseStream: {System.IO.Ports.WinSerialStream}
    BaudRate: 9600
    BreakState: false
    BytesToRead: System.NullReferenceException: Object reference not set to an instance of an object
    BytesToWrite: System.NullReferenceException: Object reference not set to an instance of an object
    CDHolding: false
    CtsHolding: true
    DataBits: 8
    DiscardNull: System.NotImplementedException: The requested feature is not implemented.
    DsrHolding: false
    DtrEnable: false
    Encoding: {System.Text.ASCIIEncoding}
    Handshake: None
    IsOpen: true
    NewLine: "\r\n"
    Parity: None
    ParityReplace: System.NotImplementedException: The requested feature is not implemented.
    PortName: "COM3"
    ReadBufferSize: 4096
    ReadTimeout: 50
    ReceivedBytesThreshold: System.NotImplementedException: The requested feature is not implemented.
    RtsEnable: false
    StopBits: One
    WriteBufferSize: 2048
    WriteTimeout: -1

What could be going wrong?

Jethro
  • 3,029
  • 3
  • 27
  • 56
  • The Port in question, COM3, can be used with another tool to successfully communicate with the device i'm talking to. So I think I can rule out the USB cable, Device, and Port. I am running inside Unity 2017.4. – Jethro Apr 20 '18 at 15:58
  • I hoped to step into the Getter for BytesToRead, but my debugger won't go into it. The OS is Windows 10 (up to date). I believe the same code used to work (unchanged) on a Mac (only change was the different port string). But the code will not find any ports at all on the Mac now... this may be a separate issue – Jethro Apr 20 '18 at 16:02
  • Note - https://sourceforge.net/projects/serialportnet/ is 99% API compatible, open source, and reliable. I recommend migrating to this library. – Jethro Feb 11 '19 at 14:37

1 Answers1

2

It seems this has been a bug for Unity since 2016. They have "Postponed" working on it.

A Workaround is to use Experimental .NET 4.6 version, if you can migrate to that in your project without other problems.

https://issuetracker.unity3d.com/issues/serialport-bytestoread-returns-null-reference

Jethro
  • 3,029
  • 3
  • 27
  • 56
  • 1
    Sadly, that is not the only bug with the serialport in Unity. I suggest you don't use that at-all. Buy a plugin or make your own with C++. It's not hard if you have basic C++ background – Programmer Apr 20 '18 at 17:32