【发布时间】:2016-05-26 12:50:46
【问题描述】:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class Sending : MonoBehaviour
{
public static SerialPort sp = new SerialPort("COM3", 9600);
void Start ()
{
OpenConnection();
}
public void OpenConnection()
{
if (sp != null)
{
if (sp.IsOpen)
{
sp.Close();
}
else
{
sp.Open();
}
}
}
void OnApplicationQuit()
{
sp.Close();
}
public static void Contact(float AngleFloat)
{
int AngleInt = (int)AngleFloat;
string AngleStr = AngleInt.ToString ();
Debug.Log(AngleStr);
sp.Write(AngleStr);
}
}
/////////////////////////////////////// /////////////////////
using UnityEngine;
using System.Collections;
public class rotate : MonoBehaviour {
private float baseAngle = 0.0f;
void OnMouseDown()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) *Mathf.Rad2Deg;
}
float OnMouseDrag()
{
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2(pos.y, pos.x) *Mathf.Rad2Deg - baseAngle;
transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
return ang;
}
void OnMouseUp()
{
float ang = OnMouseDrag();
Debug.Log(ang);
Sending.Contact(ang);
}
}
大家好,我有一个带有立方体的 Unity 场景,我可以拖动来旋转立方体,当我释放鼠标时,它会将他的新角度发送给我的控制一个小电机的 arduino。有我的两个脚本。
我的问题是,:我总是同样的错误:InvalidOperationException:指定端口未打开。
我对 arduino 的统一有点陌生,所以答案可能很明显。 (请原谅我的英语,法语)
谢谢
编辑:我正在添加我的 arduino 代码,也许问题就在这里
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 2;
int lf = 10;
int i = 0;
char myCol[20];
float angle = 5.625;
void setup()
{
Serial.begin (9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop()
{
Serial.readBytesUntil(lf, myCol,4);
int ValeurUnity = atoi(myCol);
int Tickinv = (int)((ValeurUnity*8)/angle);
int Tick;
if (Tickinv < 0)
{
Tick = abs(Tickinv);
}
if (Tickinv > 0)
{
Tick = (Tickinv * (-1));
}
if (Tickinv == 0)
{
Tick = 0;
}
if (i < Tick)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
Serial.println(Tick);
i++;
}
if (i > Tick)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
i--;
}
}
【问题讨论】:
-
您确定使用正确的 COM 端口吗?
-
是的,COM3 用于统一,我的 arduino 也在 COM3 上
-
这是在 Windows、Mac 还是 Linux 上?
-
您的代码没有问题。它对我有用。您使用的是什么 Windows 版本、Unity 版本和 Arduino 板?你是从编辑器中尝试这个还是构建?
-
我正在尝试编辑器