简体   繁体   English

处理 - > Arduino - >通过端口进行Unity通信

[英]Processing --> Arduino --> Unity communication via ports

What I try to do: Processing 3 is receiving a sort of QR code via my webcam --> it reads the value and sends it to my Arduino, Arduino successfully receives it and can now do stuff with it. 我尝试做的事情:处理3通过我的网络摄像头接收一种QR码 - >它读取值并将其发送到我的Arduino,Arduino成功接收它,现在可以用它做任务。 Now I want to add another communication channel, Unity. 现在我想添加另一个通信渠道Unity。 I want the Arduino to send the value from the Processing to Unity. 我希望Arduino将值从Processing发送到Unity。 It is easy to communicate between Arduino and Unity, but I need Processing for the webcam value. 在Arduino和Unity之间进行通信很容易,但我需要处理网络摄像头的价值。

The problem: Both Processing 3 and Unity make use of the same port (COM4, 9600). 问题: Processing 3和Unity都使用相同的端口(COM4,​​9600)。 This will cause an IO exception in Unity saying "Access Denied" followed by errors of the Serial port isn't open. 这将导致Unity中的IO异常,说“访问被拒绝”,然后串口错误未打开。

Processing 3 code 处理3个代码

...
 //Open port
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.write(1);
...

Arduino code Arduino代码

void setup() {
  // put your setup code here, to run once:
   ...
   Serial.begin(9600); // Start serial communication at 9600 bps
   ...
}

void loop() {
  if (Serial.available()) { // If data is available to read,
    val = Serial.read(); // read it and store it in val
  }
  //val contains now the data that was sent from Processing 3
  //Send this data to Unity:
  Serial.flush();
  Serial.println(val);
}

Unity code Unity代码

...
SerialPort stream = new SerialPort ("COM4", 9600); //We obviously can't open this port since its already in use by Processing 3. How to fix this?
...
// Use this for initialization
    void Start () {
        stream.Open(); //Throws IO exception: Access Denied error
    }
    // Update is called once per frame
    void Update () {
        string value = stream.ReadLine();
        val = int.Parse(value);
        if (val == 1) {
            //Links van arduino
            goLeft();
        }else if (val == 2) {
            //Rechts van arduino
            goRight();
        }

    }

We obviously can't open the port in Unity since its already in use by Processing 3. How to fix this? 我们显然无法打开Unity中的端口,因为它已经被Processing 3使用了。如何解决这个问题? Communication stream: 通讯流:

Processing 3 --> Arduino --> Unity

Eventually Unity needs to know whether you have to go Left or Right based on the QR code input on the webcam. 最终,Unity需要知道您是否必须根据网络摄像头上输入的QR码向左或向右移动。

You can't use the same serial port in two concurrent applications (why do you want to use an Arduino at all?) A solution is to establish alink between applications. 您不能在两个并发应用程序中使用相同的串行端口(为什么要使用Arduino?)解决方案是在应用程序之间建立连接。 A network connection, using a 127.0.0.1 loopback connection, is a tried and tested way of creating that link. 使用127.0.0.1环回连接的网络连接是一种经过试验和测试的创建该链接的方法。

As far as protocols go, you have endless options, my personal preference is to use OSC - both processing (via OSCP5) and Unity (various plugins, including my own I should really make public at some point) have decent support for messaging, but you can use many other types of links (ie. websockets) 就协议而言,你有无穷无尽的选择,我个人的偏好是使用OSC - 处理(通过OSCP5)和Unity(各种插件,包括我自己我应该在某个时候真正公开)对消息传递有相当的支持,但是你可以使用许多其他类型的链接(即websockets)

For the people who would like to know how to solve this problem. 对于想知道如何解决这个问题的人。 You can't. 你不能。 We have to think in another way. 我们必须以另一种方式思考。 Options were: 选项包括:

  1. Close port while done processing and then open port in Unity (was not practical since we need a constant stream of instructions) 完成处理后关闭端口,然后在Unity中打开端口(因为我们需要一个恒定的指令流,所以不实用)
  2. Leave out the Arduino and communicate between Processing and Unity (was not practical either since we need the Arduino in this case for blinkers) 省略Arduino并在Processing和Unity之间进行通信(因为我们在这种情况下需要Arduino用于闪烁灯,因此不实用)
  3. Find another communication form between the Arduino/Processing and Unity without any ports. 在没有任何端口的情况下,在Arduino / Processing和Unity之间找到另一种通信形式。 (Thanks to @kaj) (感谢@kaj)

I've implemented the last option 我已经实现了最后一个选项

  • Output Processing results to a "data.txt" file 输出处理结果为“data.txt”文件
  • Unity opens this file in C# with ReadAccess FileStream and reads the output and uses this Unity使用ReadAccess FileStream在C#中打开此文件,并读取输出并使用它

In this way we can still use the Arduino and Unity directly reads output from the Processing without a middleman. 通过这种方式,我们仍然可以使用Arduino和Unity直接读取Processing的输出而无需中间人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM