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:指定的端口不是Open。
我对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--;
}
}
答案 0 :(得分:0)
可能有4种可能导致此错误。
<强> 1 即可。 Arduino计划是开放的。关闭它然后再从Unity运行你的程序。
<强> 2 即可。另一个应用程序是使用该端口。请重新启动计算机以确保没有其他应用程序正在使用该端口。重新启动后,请不要打开Arduino应用程序。只需打开Unity并尝试再次运行程序。
第3 即可。您已将脚本(Sending
)附加到多个 GameObjects 。确保Sending
脚本仅附加到一个GameObject。要验证这一点,请创建一个简单的新项目,并将Sending
脚本仅附加到一个 GameObject 。
<强> 4 即可。您连接到另一个端口。确保您连接的端口是Arduino端口。请查看下面的图片以供参考。