x


'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();
     }
 }

}

more ▼

asked Nov 26, 2011 at 05:01 AM

eekdro gravatar image

eekdro
1 6 4 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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:

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. [1]: http://www.arduino.cc [2]: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.receivedbytesthreshold.aspx
more ▼

answered Jun 08, 2012 at 07:35 PM

boblol gravatar image

boblol
1

Did you manage to figure this out, I'm having the same problem as you guys right now.

Jul 21, 2012 at 05:44 PM Artifactx
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x67
x33

asked: Nov 26, 2011 at 05:01 AM

Seen: 2122 times

Last Updated: Jul 21, 2012 at 05:44 PM