- Home /
'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();
}
}
}
Answer by boblol · Jun 08, 2012 at 07:35 PM
I've come across the same issue trying to get my [arduino][1] 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][2] 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:
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. [1]: http://www.arduino.cc [2]: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.receivedbytesthreshold.aspxNotImplementedException: The requested feature is not implemented.
Did you manage to figure this out, I'm having the same problem as you guys right now.
Answer by Cheeeese · Jan 26, 2018 at 07:02 PM
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.
Your answer
Follow this Question
Related Questions
Can't access to Time.time in callback method AsyncCallback 1 Answer
How to do a "custom" callback ? (like OnMouseEnter()) 3 Answers
Action delegate doesn't show in inspector. 1 Answer
A way to detect when the Editor Application is focused 7 Answers
Admob, Can't call a function from HandleUserEarnedReward() for some reason. 0 Answers