繁体   English   中英

如何从两个不同的arduino板发送信息到统一3d c#

[英]How can i send information from two different arduino boards to unity 3d c#

我正在使用两个mpu6050传感器,2个arduino板(1个arduino Mega和1个Arduino Uno)和统一3d。 每块板都连接了一个mpu6050,它应该将收到的信息发送到统一。 我有麻烦从统一获取数据(我从Arduino的IDE查看信息没有问题)我使用两个不同的COM,每个板1。

使用我当前的统一代码即时出现问题,因为当我尝试调试它时它会崩溃,当它没有发送时会发送一个例外情况,例如:

Ports.io  access denied

我有以下arduino代码(它只恢复到影响这个问题的部分):

void setup() {
         Serial.begin(9600);
}
void loop() {
         Serial.println(map((ypr[2] * 180/M_PI),-90,90,0,360));
         delay(20);
}

注意:MPU从-90到90发送值,所以我使用map函数来转换0到360范围内的值

这是统一代码(我有两个不同的代码,但唯一的区别是它说“COM4”,另一个说“COM3”):

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

public class Mover_Con_Arduino : MonoBehaviour
{
   SerialPort sp = new SerialPort("COM4", 9600);

void Start()
{
}

void Update()
{
    if (!sp.IsOpen)
    {
            sp.Open();
    }

    float rot = float.Parse (sp.ReadLine ());
    transform.localEulerAngles = new Vector3 (rot, 0, 0);
    print (rot);    
}
}

我正在做的是使用从arduino发送到Unity的值来旋转游戏对象

我最好的猜测是sp.Open()无法打开COM端口并在sp.ReadLine()上抛出异常;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM