通过蓝牙的Unity串行端口通信没有得到实时更新



我写了一个从串行端口读取数据的小脚本。然后在windows蓝牙上,我创建了一个传入端口(com8(。我使用一个名为蓝牙终端的应用程序从安卓系统连接到了蓝牙。问题是当我在Unity上运行脚本时,它没有实时更新数据。例如,我发送了1、2、3、4、5等字符串……一个接一个地,它只更新了1、3、四、六等字符串。但另一方面,当我使用Arduino串行检查com端口时,它正在实时更新,没有任何问题。下面是我的代码

using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;
public class Movement : MonoBehaviour
{
private float speed = 5.0f;
public Text outText;

SerialPort sp=new SerialPort("COM8",9600);
// Start is called before the first frame update
void Start()
{
sp.Open();
sp.ReadTimeout =1;

}
// Update is called once per frame
void Update()
{
if(sp.IsOpen){

try{

print(sp.ReadLine().ToString());
outText.text=sp.ReadLine().ToString();
}catch(System.Exception){

}
}

}
}
  1. 将资产商店中的"Ardity"添加到Unity项目中。(免费((https://assetstore.unity.com/packages/tools/integration/ardity-arduino-unity-communication-made-easy-123819)
  2. 将SerialController.cs添加到您的游戏对象并打开它
  3. 添加调试。日志(消息(;在Update((内部
  4. 运行游戏并从蓝牙设备发送信号
  5. 检查日志值

最新更新