通过 USB 从 Java 程序读取时,Arduino 出现 Serial.readbytes() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15869609/
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
Arduino trouble with Serial.readbytes() when reading from a Java program over USB
提问by Mark
I am using an Arduino Uno. I am experiencing weird behavior with Serial.readbytes()
. The Arduino is powered and communicating via USB on COM4. I am running Eclipseon 64-bit Windows 7.
我正在使用Arduino Uno。我遇到了奇怪的行为Serial.readbytes()
。Arduino 通过 COM4 上的 USB 供电和通信。我在 64 位 Windows 7 上运行Eclipse。
My current Arduino code looks like the following; the delays are so I can start and stop my Java service and look at the serial window in the Arduino IDE, inputBuffer is a character array.
我当前的 Arduino 代码如下所示;延迟是为了我可以启动和停止我的 Java 服务并查看 Arduino IDE 中的串行窗口,inputBuffer 是一个字符数组。
char inputBuffer[10]; // For incoming serial data
void setup() {
Serial.begin(9600); // Opens serial port, sets data rate to 9600 bit/s.
}
void GetTemp(){
int temp = 123;
Serial.print(temp);
}
void loop() {
if (Serial.available() > 0) {
Serial.readBytes(inputBuffer, Serial.available());
delay(5000);
Serial.print("I got this ->");
Serial.print(inputBuffer);
Serial.println("<-");
}
delay(5000);
Serial.println("delay");
Serial.println(inputBuffer[0], DEC);
}
Here is the code for my Java side. I modified it from Two way communcation with the serial port. This just sends one byte array upon start.
这是我的 Java 端的代码。我从与串口的双向通信中修改了它。这只是在开始时发送一个字节数组。
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.OutputStream;
public class TwoWaySerialComm
{
public TwoWaySerialComm()
{
super();
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public static class SerialWriter implements Runnable
{
OutputStream out1;
public SerialWriter ( OutputStream out )
{
this.out1 = out;
}
public void run ()
{
try
{
byte[] test = ("H567").getBytes();//testing string to send
this.out1.write(test);
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
public static void main ( String[] args )
{
try
{
(new TwoWaySerialComm()).connect("COM4");
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I run it, my Arduino just prints out a blank line. If I prepare the Arduino with "hiya" using the Serial window first, then when the java is executed, it will return back to a blank line.
当我运行它时,我的 Arduino 只是打印出一个空行。如果我首先使用串行窗口准备带有“hiya”的 Arduino,那么在执行 java 时,它会返回一个空行。
Since the char array is just over-written each time I sent "H567\r\n" and then in the Serial window typed sent "hiya" where the extra newline was still being executed, so I know characters are being stored somehow.
由于每次我发送“H567\r\n”时字符数组都被覆盖,然后在串行窗口中输入“hiya”,其中额外的换行符仍在执行,所以我知道字符正在以某种方式存储。
Another test was to change the last Serial.prinln()
to Serial.println(inputBuffer[0], DEC)
. Using the Serial Window, results are as expected, but from Java it just prints out "0". Serial communication works wonderful coming from the Arduino talking to the Java code, just not from Java code to the Arduino. How can I fix this problem?
另一个测试是将最后一个更改Serial.prinln()
为Serial.println(inputBuffer[0], DEC)
. 使用串行窗口,结果如预期,但在 Java 中它只是打印出“0”。来自 Arduino 与 Java 代码的串行通信工作非常好,而不是从 Java 代码到 Arduino。我该如何解决这个问题?
Per suggestion, I tried reading Serial.available()
instead of a fixed max of 10. No changes were experienced.
根据建议,我尝试阅读Serial.available()
而不是固定的最多 10 篇。没有发生任何变化。
采纳答案by Mark
I found a solution, but I am unsure of the reason. When starting the Serial writing thread, I must call thread.sleep(2000)
upon start for the Arduino to read characters correctly.
我找到了解决方案,但我不确定原因。启动串行写入线程时,我必须thread.sleep(2000)
在启动时调用Arduino 才能正确读取字符。
回答by npgall
Try adding out1.flush()
after out1.write()
.
尝试out1.flush()
在 之后添加out1.write()
。