[英]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.