'SerialDataReceivedEventHandler' is not working.

Please help me.
‘SerialDataReceivedEventHandler’ is not working.

Tell me. If you know other way.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace SerialPortCtrl
{

public class ComPort
{
    SerialPort SP = new SerialPort();
    List<string> RevData = new List<string>();

    public ComPort()
    {
    }

    ~ComPort() 
    {
        SP.Close();            
    }

    private void SP_DataRecieved(Object sender, SerialDataReceivedEventArgs e)
    {
        RevData.Add(SP.ReadLine());
    }

    private void SP_ErrorRecieved(Object sender, SerialErrorReceivedEventArgs e)
    {
        RevData.Add("Error while receiving data. " + e.ToString()); 
    }
    
    public string GetData()
    {
        string RevStr = "";
        if ( RevData.Count == 0) return "";
        RevStr = RevData[0];
        RevData.RemoveAt(0);
        return RevStr;
    }

    public bool SendData(string Data)
    {
        if (SP.IsOpen)
        {
            SP.WriteLine(Data);
            return true;
        }
        else return false;
    }

    public bool Open(string PortName, int BaudRate = 9600, int DataBits = 8)
    {
        
        SP.PortName = PortName;
        SP.BaudRate = BaudRate;
        SP.DataBits = DataBits;
        SP.Parity = Parity.None;
        SP.StopBits = StopBits.One;
        SP.ReadTimeout = (int)500;
        SP.WriteTimeout = (int)500;
        SP.ReadBufferSize = 4;
        SP.WriteBufferSize = 4;

        SP.DtrEnable = true;
        SP.RtsEnable = true;

        SP.DataReceived += new SerialDataReceivedEventHandler(SP_DataRecieved);
        SP.ErrorReceived += new SerialErrorReceivedEventHandler(SP_ErrorRecieved);
        SP.Open();
        
        if (SP.IsOpen) return true; else return false; 
    }

    public void Close()
    {
        SP.Close();
    }
}

}

I’ve come across the same issue trying to get my arduino to communicate to unity through serial. I think the reason is due to monos implementation of SerialPort is not complete…

After some digging around I found SerialPort.ReceivedBytesThreshold property:

The number of bytes in the internal input buffer before a DataReceived event is fired. The default is 1.

Remarks: The DataReceived event is also raised if an Eof character is received, regardless of the number of bytes in the internal input buffer and the value of the ReceivedBytesThreshold property.

If you try and log your serial port’s ReceivedBytesThreshold property you get this error message:

NotImplementedException: The requested feature is not implemented.
System.IO.Ports.SerialPort.set_ReceivedBytesThreshold (Int32 value)
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:set_ReceivedBytesThreshold (int)
GyroSyncBehavior.Start () (at Assets/Scripts/GyroSyncBehavior.cs:23)

I’m guessing that the DataReceived event doesn’t fire under Unity’s .NET 2.0 mono version, but I’m a noob so I might be wrong.

It’s 2018, has this been fixed yet?

I still get that the call has not been implemented…
I wish someone in the Unity development team shows more love to the serial port calls.
We really need those to be complete.

110098-unitybug.jpg