SerialPort是打开的,但在BytesToRead

时间:2018-04-20 15:56:21

标签: c# unity3d serial-port unet

我创建了一个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

可能出现什么问题?

1 个答案:

答案 0 :(得分:2)

自2016年以来,这似乎是Unity的一个错误。 他们已经推迟了#34;正在努力。

解决方法是使用Experimental .NET 4.6版本,如果您可以在没有其他问题的情况下迁移到项目中。

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