【问题标题】:C# SerialPort EventHandler in UnityUnity 中的 C# SerialPort 事件处理程序
【发布时间】:2014-10-27 15:35:45
【问题描述】:

我正在为 Unity 编写 C# 代码。只需在 EventHandler 中读取 SerialPort 值。问题是没有调用处理程序。这是代码

using UnityEngine;
using System.Collections;
using System;
using System.IO.Ports;

public class MainScript : MonoBehaviour {

public SerialPort mySerialPort;
public static float speed=100;
GameObject cube ;
public GUIStyle style ;
// Use this for initialization
void Start () {

    cube = GameObject.FindGameObjectWithTag ("cube");

    if(mySerialPort.IsOpen)
        mySerialPort.Close();

    mySerialPort = new SerialPort("com5");
    mySerialPort.BaudRate = 9600;
    mySerialPort.Parity = Parity.None;
    mySerialPort.StopBits = StopBits.None;
    mySerialPort.DataBits = 8;
    mySerialPort.Handshake = Handshake.None;
    mySerialPort.DataReceived += new SerialDataReceivedEventHandler (DataReceivedHandler);

    if(mySerialPort.IsOpen == false)
        mySerialPort.Open();
}


void OnGUI(){
    GUI.Box (new Rect(100,100,100,100),"Speed : " + speed , style);
}
// Update is called once per frame
void Update () {
// speed = mySerialPort.ReadTo ("\r");
// update My View with the speed new value

    cube.transform.Rotate(Vector3.up * speed *Time.deltaTime);

}


public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    speed = float.Parse( sp.ReadTo ("\r") ) ;
    print ("Data Recieved : " + speed);
}
}

问题不在于串行端口,因为当我在更新功能中读取它以实现统一时,它会读取正确的值,但是更新 UI 时存在性能问题。

谢谢

【问题讨论】:

  • speed 是静态的,是这个问题吗?
  • 速度是静态的,因为它是在作为静态函数的事件处理程序内部调用的。你能告诉我你看到了什么吗??
  • + 函数完全没有被调用

标签: c# .net unity3d serial-port


【解决方案1】:

正如您所说,不会调用“DataReceived”事件的处理程序,这是 Unity 的一个已知问题(截至今天)。我的解决方法是使用单独的线程来读取端口(轮询):

private bool _looping;
private SerialPort _port;
public string _comPort; // e.g. "COM3"
private const int _baudRate = 921600;
private Thread portReadingThread;
private string strData;

private void OnEnable()
{
    _looping = true;
    portReadingThread = new Thread(ReadArduino);
    portReadingThread.Start();
}

private void OnDestroy()
{
    _looping = false;  // This is a necessary command to stop the thread.
                       // if you comment this line, Unity gets frozen when you stop the game in the editor.                           
    portReadingThread.Join();
    portReadingThread.Abort();
    _port.Close();
}

void ReadArduino()
{        
    // For any COM number larger than 9, you should add prefix of \\\\.\\ to it. 
    // For example for COM15, you should write it as "\\\\.\\COM15" instead of "COM15".
    _port = new SerialPort(_comPort, _baudRate);
    if (_port == null)
    {
        Debug.LogError("_port is null");
        return;
    }

    _port.Handshake = Handshake.None;
    _port.DtrEnable = true;
    //myPort.RtsEnable = true;
    _port.ReadTimeout = 500; // NOTE: Don't Reduce it or the communication might break!
    _port.WriteTimeout = 1000;
    _port.Parity = Parity.None;
    _port.StopBits = StopBits.One;
    _port.DataBits = 8;
    _port.NewLine = "\n";

    _port.Open();
    if (!_port.IsOpen)
        print("PORT HAS NOT BEEN OPEN!");
    // Send "START" command to the arduino.
    _port.WriteLine("START");
    // Start reading the data coming through the serial port.
    while (_looping)
    {
        strData = _port.ReadLine(); // blocking call.
        print(strData);
        Thread.Sleep(0);
    }
}

读取发生在以下行:

strData = _port.ReadLine(); // blocking call.

但它不会阻塞或冻结统一,因为它发生在单独的线程中。

顺便说一句,如果你想写一些东西到端口,只需写:

  public void WriteToArduino()
  {
        _port.WriteLine("YourMessageHere");
  }

您可以在游戏中的任何位置调用 WriteToArduino()。它不需要在任何特殊的线程或其他东西中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多