2016-05-26 71 views
0
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); 
} 
} 

///////////////// //////////////////////////////////团结不会发送任何东西给我的arduino

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--; 
    } 
} 
+0

您确定您使用正确的COM端口吗? – Meeh

+0

是的,COM3为统一,我的arduino也在COM3上 – Sholyu

+0

这是在Windows,Mac或Linux上? – Programmer

回答

0

有4件可能的事情可能会导致此错误。

。 Arduino计划是开放的。关闭它然后再次从Unity运行你的程序。

。另一个应用程序正在使用端口。请重新启动计算机以确保没有其他应用程序正在使用该端口。重新启动后,请勿打开Arduino应用程序。只需打开Unity并尝试再次运行程序。

。您将脚本(Sending)附加到多个GameObjects。确保Sending脚本仅附加到一个GameObject。要验证这一点,请创建一个简单的新项目,并仅将Sending脚本附加到一个GameObject

。您连接到不同的端口。确保您连接的端口是Arduino端口。看下面的图片以供参考。

enter image description here

+0

解决方案1工作!非常感谢! – Sholyu

+0

@Sholyu好的。您可以通过勾选复选标记来接受此答案。 http://i.stack.imgur.com/uqJeW.png – Programmer

+0

完成!再次感谢! – Sholyu