简体   繁体   English

Java GUI和Arduino-从串行中提取信息

[英]Java GUI and Arduino - Extract Information from Serial

I am currently working in an Arduino project. 我目前在Arduino项目中工作。 In which I need to read 5 analog sensors. 我需要在其中读取5个模拟传感器。

The arduino code for each sensor its really simple. 每个传感器的arduino代码非常简单。 But it is the following: 但这是以下内容:

void setup() 
 {
  Serial.begin(9600);
 }

void loop() 
 {
 int sensor1 = analogRead(A0);
 float volt1 = sensor1 * (5.0 / 1023-0);
 Serial.print("The voltage on the sensor  1 is:   ");
 Serial.println(volt1);
 }

Using the Java documentation found on the Arduino Website http://playground.arduino.cc/Interfacing/Java I was able to set up the Rx and Tx libraries. 使用Arduino网站http://playground.arduino.cc/Interface/Java上的Java文档,我能够设置Rx和Tx库。 And I was also able to get data from the serial port in the Java Console. 而且我还能够从Java控制台的串行端口获取数据。 What I need to do now is to extract that information from the console and display it on a GUI. 我现在需要做的是从控制台中提取该信息并将其显示在GUI上。

In the Java console I have: 在Java控制台中,我有:

The voltage on the sensor 1 is: X 传感器1上的电压为:X
The voltage on the sensor 2 is: Y The voltage on the sensor 3 is: Z 传感器2上的电压为:Y传感器3上的电压为:Z
The voltage on the sensor 4 is: W 传感器4上的电压为:W
The voltage on the sensor 5 is: A 传感器5上的电压为:A

And I need to put the values of X, Y, Z, W and A each one on a different JTextField 我需要将X,Y,Z,W和A的值分别放在不同的JTextField上

Thanks for the help 谢谢您的帮助

Looking at your sample output data: 查看示例输出数据:

The voltage on the sensor 1 is: X
The voltage on the sensor 2 is: Y
The voltage on the sensor 3 is: Z
The voltage on the sensor 4 is: W
The voltage on the sensor 5 is: A

It sounds like you simply need to get the data after the : character and remove the whitespace around the value so you can parse it as an floating point value. 听起来您只需要获取:字符后的数据并删除该值周围的空格,即可将其解析为浮点值。 This should be simple using the String class: 使用String类应该很简单:

  1. get the last ":" character using indexOf() 使用indexOf()获得最后的“:”字符
  2. chop the last part of the string using substring() 使用substring()截取字符串的最后一部分
  3. clear whitespace using trim() 使用trim()清除空白

Here's a basic example: 这是一个基本示例:

String stringFromSerial = "The voltage on the sensor 1 is: X";
String valueString = stringFromSerial.substring(stringFromSerial.lastIndexOf(":")+1).trim(); 
System.out.println(valueString);

If it does what you want, you could encapsulate this in a function: 如果它满足您的要求,则可以将其封装在一个函数中:

float extractFloat(String serialMessage){
  float result = Float.NaN;
  try{
    result = Float.parseFloat(serialMessage.substring(serialMessage.indexOf(":")+1).trim());
  }catch(Exception e){
    System.err.println("Oh-oh! Something went wrong trying to convert " + serialMessage);
    e.printStackTrace();
  }
  return result;
}

I'm not sure if it's worth sending so many characters though. 我不确定是否值得发送这么多字符。 You may want to consider sending the values separated by comma, as in a csv row. 您可能要考虑发送以逗号分隔的值,例如在csv行中。

Something along these lines: 遵循以下原则:

Serial.print(volt1);
Serial.print(",");
Serial.print(volt2);
Serial.print(",");
Serial.print(volt3);
Serial.print(",");
Serial.print(volt4);
Serial.print(",");
Serial.println(volt4);

The nice thing about this is that in newer versions of Arduino( version 1.6.6 or above ) you should be able to use Tools > Serial Plotter . 这样做的好处是,在较新版本的Arduino(版本1.6.6或更高版本)中,您应该可以使用Tools> Serial Plotter

Additionally, if you're not stuck to raw java and swing components, you might want to check out Processing and it's Serial library 此外,如果您不拘泥于原始的Java和swing组件,则可能要签出Processing及其序列库

I am not quite clear on specifics of your question, but I don't have enough points to comment, so I will answer at least part of it. 对于您的问题,我不太清楚,但是我没有足够的意见要发表,所以我将至少回答一部分。

new JTextField(Float.toString(volt1));

Will create a JTextField with the value of the voltage. 将使用电压值创建一个JTextField。 You can assign the created field to whatever variable you wish. 您可以将创建的字段分配给所需的任何变量。

I am not sure why you would want to extract the value from the console when you have the value right there in a variable. 我不确定在变量中有值时为什么要从控制台中提取值。

Does that help? 有帮助吗?

If not, could you be more specific in your requirements and why you want to get the value from the console? 如果不是,您能否更具体地说明您的要求,以及为什么要从控制台获取价值?

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

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