将字符串从 Java 发送到 Arduino(简单示例)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25502517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send String from Java to Arduino (simple example)
提问by takluiper
It is solved. I put a
Thread.sleep(4000);
after opening the port in the java code and now it works. The problem was that the arduino is reset everytime the port is opened. When i was sending the data, the arduino wasn't ready to listen.
解决了。我
Thread.sleep(4000);
在java代码中打开端口后放了一个,现在它可以工作了。问题是每次打开端口时都会重置 arduino。当我发送数据时,arduino 还没有准备好倾听。
I'm new to arduino and Java, but i made a program so simple that I don't get why isn't working.
我是 arduino 和 Java 的新手,但我制作了一个非常简单的程序,我不明白为什么不起作用。
I send a String to the serial port that correspond to arduino (COM5):
我向对应于arduino(COM5)的串口发送一个字符串:
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "color FF00FFEND";
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM5")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
outputStream = serialPort.getOutputStream();
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream.write(messageString.getBytes());
System.out.println(messageString);
outputStream.close();
serialPort.close();
}
catch (IOException e) {System.out.println("err3");}
catch (PortInUseException e) {System.out.println("err");}
catch (IOException e) {System.out.println("err1");}
catch (UnsupportedCommOperationException e) {System.out.println("err2");}
}
}
}
}
}
and the code in arduino to get that string:
以及 arduino 中获取该字符串的代码:
char inputBuffer[10];
void setup() {
Serial.begin(9600);
}
void loop() {
while (true)
{
if (Serial.available() > 0) {
Serial.readBytes(inputBuffer, Serial.available());
delay(5000);
Serial.print("I got this ->");
Serial.print(inputBuffer);
Serial.println("<-");
}
}
}
the while(true) is for testing purposes. I dont get nothing printed, and I dont know where the problem is. I have seen all the post about arduino and java here and i dont find nothing that helps. Thanks for the help and sorry if it is a stupid question, im a newbie to this
while(true) 用于测试目的。我没有打印任何东西,我不知道问题出在哪里。我在这里看到了所有关于 arduino 和 java 的帖子,但我没有发现任何帮助。感谢您的帮助,如果这是一个愚蠢的问题,我很抱歉,我是这个新手
Im using RXTXcomm.jar. Version: RXTX-2.2-20081207
我使用 RXTXcomm.jar。版本:RXTX-2.2-20081207
采纳答案by takluiper
It is solved. I put a Thread.sleep(4000)
after opening the port in the java code and now it works. The problem was that the Arduino is reset everytime the port is opened, so I was sending the data when the Arduino wasn't ready to listen.
解决了。我Thread.sleep(4000)
在java代码中打开端口后放了一个,现在它可以工作了。问题是每次打开端口时都会重置 Arduino,所以我在 Arduino 还没有准备好监听时发送数据。
char inputBuffer[10];
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (true)
{
if (Serial.available() > 0)
{
Serial.readBytes(inputBuffer, 10);
delay(5000);
Serial.print("I got this ->");
Serial.print(inputBuffer);
Serial.println("<-");
}
}
}
回答by Hirday Gupta
You could use the Java-Arduino Communication Library. It can be found here: https://sourceforge.net/projects/javaarduinolibrary/. (be sure to download both jars) Then your code would look something like this:
您可以使用 Java-Arduino 通信库。可以在这里找到:https: //sourceforge.net/projects/javaarduinolibrary/。(一定要下载两个 jars)然后你的代码看起来像这样:
import arduino.*;
class JavaArduinoComm {
public static void main(String[] args) {
Arduino obj = new Arduino('PortDescription', BAUD_RATE);
obj.openConnection();
}
}
Then you can use any of the following methods:
然后您可以使用以下任何一种方法:
- String serialRead(int limit) - returns a string containing as many readings as the value of limit. recommended for reading
- void serialWrite(String s) - writes the contents of the entire string to the serial at once. written as string to serial.
- void serialWrite(String s, int noOfChars, int delay) - writes the contents of the strings to the serial gradually. It writes the string in incremental steps with 'noOfChars' charaacters each time, with a pause of 'delay' milliseconds between each write. written as string to serial. recommended to write String
- void serialWrite(char c) - writes the individual char to the serial in datatype char.
- void serialWrite(char c, int delay) - writes the individual char to the serial in datatype char and pauses the thread for delay milliseconds after. recommended to write char
- String serialRead(int limit) - 返回一个包含与 limit 值一样多的读数的字符串。推荐阅读
- void serialWrite(String s) - 一次将整个字符串的内容写入串行。作为字符串写入串行。
- void serialWrite(String s, int noOfChars, int delay) - 逐渐将字符串的内容写入串行。它每次使用“noOfChars”字符以增量步骤写入字符串,每次写入之间有“延迟”毫秒的暂停。作为字符串写入串行。建议写字符串
- void serialWrite(char c) - 将单个字符写入数据类型为 char 的串行。
- void serialWrite(char c, int delay) - 将单个字符写入数据类型为 char 的串行,并在延迟毫秒后暂停线程。建议写char