简体   繁体   English

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

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

im working with two mpu6050 sensors, 2 arduino boards (1 arduino Mega and 1 Arduino Uno) and unity 3d. 我正在使用两个mpu6050传感器,2个arduino板(1个arduino Mega和1个Arduino Uno)和统一3d。 Each board has one mpu6050 connected and it is supposed to send the information recieved to unity. 每块板都连接了一个mpu6050,它应该将收到的信息发送到统一。 Im having troubles getting the data from unity (I have no problems with the information viewing it from the Arduino's IDE) Im working with two differents COM, 1 per board. 我有麻烦从统一获取数据(我从Arduino的IDE查看信息没有问题)我使用两个不同的COM,每个板1。

With my current unity code im having problems since it crashes when i try to debug it and when it doesen't it sends an exception which says something like: 使用我当前的统一代码即时出现问题,因为当我尝试调试它时它会崩溃,当它没有发送时会发送一个例外情况,例如:

Ports.io  access denied

I have the following arduino code (it is resumed to only the part that affects this question): 我有以下arduino代码(它只恢复到影响这个问题的部分):

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

Note: The MPU sends values from -90 to 90, so i use the map function to transform those values in a range from 0 to 360 注意:MPU从-90到90发送值,所以我使用map函数来转换0到360范围内的值

Here is the unity code (i have two differents code, but the only difference is where it says "COM4" the other one says "COM3"): 这是统一代码(我有两个不同的代码,但唯一的区别是它说“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);    
}
}

What im doing is using the value sent from arduino to unity to rotate a gameobject 我正在做的是使用从arduino发送到Unity的值来旋转游戏对象

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

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

相关问题 如何让两个物体在碰撞时粘在一起? (统一 3d ) C# - How can I make two objects stick together on collision? (unity 3d ) C# 如何在另一个脚本上运行一个方法? [统一 3D] C# - How can I run a method on one script from another? [UNITY 3D] C# 我如何禁用多个组件Unity 3D C# - How i can disable multiple components Unity 3d c# 如何使用C#在Unity 3D中的随机时间(相同位置)生成敌人? - How can I generate enemies in random time (same position) in Unity 3D using C#? 如何计算旋转单位3D C#的速度 - How can I calculate the speed of a rotation unity 3d c# 如何将信息从Kinect发送到Arduino? - How can I send information from a Kinect to an Arduino? Unity 3D如何使具有不同对象的触发器/碰撞发生不同的事情? C# - Unity 3D how to make different things happen for triggers/collisions with different objects? C# Unity 3D c#2个不同的循环要同时运行 - Unity 3D c# 2 different loops to be run at the same time 如何在Unity 5中使用C#按比例更改3D对象的比例和音频源的半径? - How can I change the scale of a 3D Object and the Audio Source's radius in a proportionally way in Unity 5 using C#? 如何使用ac#sript在3D统一游戏中生成硬币和不同的障碍物? - How to generate coins and different obstacles in a 3D unity game using a c# sript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM