简体   繁体   English

使用处理读取Arduino int数组

[英]Reading Arduino int array with Processing

I have an arduino-processing communication problem. 我有arduino处理通讯问题。 I have arduino code that gives me X-and/Y coordinates from a touchScreen and it works well, no problem with that, I got my X- and Y coordinates. 我有arduino代码,可以通过触摸屏为我提供X和/ Y坐标,并且运行良好,没问题,我得到了X和Y坐标。 BUT I need to visualize that, I am trying to write code in Processing 3.0, so that I will be able to see on my computer, where the touchFolie was being touched. 但是我需要想像一下,我正在尝试在Processing 3.0中编写代码,以便能够在我的计算机上看到touchFolie被触摸的位置。 So I need to send the xy from arduino to processing so that I will be able to draw. 因此,我需要将XY从arduino发送到处理过程,以便能够进行绘制。 Does anyone know how can I send an integer array[X,Y] from arduino to processing ?? 有谁知道如何从arduino发送整数数组[X,Y]进行处理?

It will be helpful to have a play with the Serial library in Processing and the SerialRead example (in Processing > File > Examples > Libraries > serial > SimpleRead ) 在Processing和SerialRead示例中使用Serial库很有帮助 (在Processing> File> Examples> Libraries> serial> SimpleRead中

Let's say you're starting from scratch. 假设您是从头开始。 To send data from Arduino you need to open a Serial connection. 要从Arduino发送数据,您需要打开一个串行连接。 At this stage the only important details is the baud rate: how fast is the data flowing. 在此阶段,唯一重要的细节是波特率:数据流的速度。

Here's a minimal Arduino data out example: 这是一个最小的Arduino数据输出示例:

void setup() {
  //initialize the Serial library with baud rate 115200
  Serial.begin(115200);
}

void loop() {
  //write data to the serial port
  Serial.println("test");
  delay(1000);
}

If you open Serial Monitor in the Arduino software and set the baud rate to 115200 you should see test printed once a second. 如果您在Arduino软件中打开串行监视器并将波特率设置为115200,则应该每秒看到一次测试打印。

To read the same data in Processing, aside from specifying the baud rate, you must also specify the serial port (what you have selected in Tools > Port and is also listed at the bottom right of your current Arduino sketch): 要在处理中读取相同的数据,除了指定波特率之外,还必须指定串行端口(在“工具”>“端口”中选择的端口,并在当前Arduino草图的右下方列出):

import processing.serial.*; 

void setup() { 
  //update the serial port index based on your setup
  println(Serial.list());
  Serial arduino = new Serial(this, Serial.list()[0], 115200); 
  arduino.bufferUntil('\n'); 
} 

void draw() { 
} 

void serialEvent(Serial p) { 
  //read the string from serial
  String rawString = p.readString();
  println(rawString);
}

Notice that we tell Processing to buffer until it reaches a '\\n' character so we don't have to worry about waiting for every single character and append it manually to a String. 请注意,我们告诉Processing进行缓冲,直到到达'\\n'字符为止,因此我们不必担心等待每个字符并将其手动附加到String。 Instead using bufferUntil() , serialEvent() and readString() most of the work is done for us. 取而代之的是使用bufferUntil()serialEvent()readString()为我们完成了大部分工作。

Now that you can send a String from Arduino and read it in Processing, you can do some String manipulation, like splitting a string into multiple using a separator via the split() function: 现在您可以从Arduino发送一个String并在Processing中读取它,您可以执行一些String操作,例如通过split()函数使用分隔符将一个字符串拆分成多个字符串:

String xyValues = "12,13";
printArray(xyValues.split(","));

The last part would be converting the split values from String to int : 最后一部分是将分割值从String转换为int

String xyValues = "12,13";
String[] xy = xyValues.split(","); 
printArray(xy);
int x = int(xy[0]);
int y = int(xy[1]);
println("integer values: " + x + " , " + y);

So, in theory, you should be able to do something like this on Arduino: 因此,从理论上讲,您应该能够在Arduino上执行以下操作:

int x,y;

void setup() {
  //initialize serial 
  Serial.begin(115200);
}

void loop() {
  //simulating the x,y values from the touch screen, 
  //be sure to replace with the actual readings from 
  //NOTE! If the screen returns values above 255, scale them to be from 0 to 255
  x = map(analogRead(A0),0,1024,0,255);
  y = map(analogRead(A1),0,1024,0,255);
  //write the data to serial
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  Serial.print("\n");
}

then on the Arduino side: 然后在Arduino方面:

import processing.serial.*; 

float x,y;

void setup() { 
  size(400,400);
  //update the serial port index based on your setup
  Serial arduino = new Serial(this, Serial.list()[0], 115200); 
  arduino.bufferUntil('\n'); 
} 

void draw() { 
  background(0); 
  ellipse(x,y,10,10);
} 

void serialEvent(Serial p) { 
  //read the string from serial
  String rawString = p.readString();
  //trim any unwanted empty spaces
  rawString = rawString.trim();
  try{
    //split the string into an array of 2 value (e.g. "0,127" will become ["0","127"]
    String[] values = rawString.split(",");
    //convert strings to int
    int serialX = int(values[0]);
    int serialY = int(values[1]);
    //map serial values to sketch coordinates if needed
    x = map(serialX,0,255,0,width);
    y = map(serialY,0,255,0,height);
  }catch(Exception e){
    println("Error parsing string from Serial:");
    e.printStackTrace();
  }
}

Note The Arduino code above will probably not solve your problem as it is, you need to integrate your touch sensor code, but hopefully it provides some hints on how you can tackle this. 注意上面的Arduino代码可能无法照原样解决您的问题,您需要集成触摸传感器代码,但希望它能提供一些有关如何解决此问题的提示。

Sending a String, then parsing it is one approach, but not the most efficient. 发送字符串然后解析它是一种方法,但不是最有效的方法。 If you have your x,y values in a 0-255 range, you could send just 2 bytes (each coordinate as a single char ) instead of up to 8 bytes, but for now it may be much easier to play with Strings rather than jump into bytes straight away. 如果您的x,y值在0-255范围内,则可以仅发送2个字节(每个坐标作为一个char ),而不是最多发送8个字节,但是就目前而言,使用String而不是String可以轻松得多立即跳入字节。

This tutorial will help you. 教程将为您提供帮助。 5 minutes and you are able to connect each others! 5分钟,您就可以彼此连接!

EDIT: 编辑:

First of all look the first part of the tutorial ("From Arduino..." "...To processing"). 首先,看一下教程的第一部分(“从Arduino ...”“ ...到处理”)。 In arduino you have just to send your coordinates in your Serial. 在arduino中,您只需在序列号中发送坐标即可。

Serial.println(coordX);
Serial.println(coordY);

In processing you receive this coordinates as Text but you are able to convert it in Float with parseFloat() function. 在处理中,您将以文本形式接收此坐标,但是可以使用parseFloat()函数在Float中对其进行转换。

This is the code you need in Processing to receive your coordinates and store them in variables. 这是处理中需要的代码,以接收您的坐标并将其存储在变量中。

    import processing.serial.*;
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port

    float x = 0;
    float y = 0;
    boolean first = true;
    setup() {
       String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
       myPort = new Serial(this, portName, 9600); 
    }
    void draw() {
      if ( myPort.available() > 0) {  // If data is available,
         val = myPort.readStringUntil('\n');         // read it and store it in val
         if (first) {
            x = parseFloat(val);
            first = false;
            println("x= "+val); //print it out in the console
            } 
         else {
             y = parseFloat(val);
             first = true;
             println("y= "+val); //print it out in the console
             }
      }

I hope this will help you solve your problem. 希望这可以帮助您解决问题。

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

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