1

我创建了一个 SerialPort,可以检查它不为空,并且SerialPort.IsOpen()返回 true。但是当我检查BytesToRead属性时,我得到了 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 ()
  ...

我可以逐步使用调试器并在调用 BytesToRead 之前立即看到,串行端口已实例化并打开,但调试器无法访问某些属性,包括 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

可能出了什么问题?

4

1 回答 1

2

自 2016 年以来,这似乎一直是 Unity 的一个错误。他们已经“推迟”了它的工作。

一种解决方法是使用 Experimental .NET 4.6 版本,如果您可以在项目中迁移到该版本而不会出现其他问题。

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

于 2018-04-20T16:23:00.780 回答