2016-05-26 25 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