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--;
}
}
您确定您使用正确的COM端口吗? – Meeh
是的,COM3为统一,我的arduino也在COM3上 – Sholyu
这是在Windows,Mac或Linux上? – Programmer