【问题标题】:Unity wont send anything to my arduinoUnity 不会向我的 arduino 发送任何内容
【发布时间】: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 板?你是从编辑器中尝试这个还是构建?
  • 我正在尝试编辑器

标签: c# unity3d arduino


【解决方案1】:

有 4 种可能的原因可能导致此错误。

1。 Arduino 程序是开放的。然后关闭它以再次从 Unity 运行您的程序。

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

3。您将脚本 (Sending) 附加到多个 GameObjects。确保Sending 脚本仅附加到一个游戏对象。为了验证这一点,创建一个简单的新项目并将Sending 脚本附加到一个GameObject

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

【讨论】:

  • @Sholyu 不错。您可以通过勾选复选标记来接受此答案。 i.stack.imgur.com/uqJeW.png
  • 完成!再次感谢!
  • @Sholyu 不客气。这是否解决了您在新问题中遇到的问题?
  • @Sholyu 一会儿试试看
  • 好吧,如果你需要我的 Unity Code 就告诉我,这不是同一个项目
猜你喜欢
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 2020-06-24
  • 1970-01-01
相关资源
最近更新 更多