Java RXTX和Arduino之间的串行通信

时间:2010-01-05 21:21:49

标签: java serial-port arduino rxtx

我正在尝试使用串口在我的PC(使用Netbeans和RXTX的Windows 7)与Arduino Pro之间进行通信。实际上,Arduino使用FTDI电缆连接到PC。

代码基于找到here.

的Java SimpleRead.Java

目前,Arduino只是在启动时打印出一个字符串。我的Java程序应该打印已读取的字节数,然后打印出内容。 Java程序有效,有点......

如果字符串很长(> 10个字节左右),输出将被分解。

所以如果在Arduino上打印

Serial.println("123456789123456789"); //20 bytes including '\r' and '\n'

我的Java程序的输出可能类似于:

Number of Bytes: 15   
1234567891234  
Number of Bytes: 5  
56789

Number of Bytes: 12   
1234567891  
Number of Bytes: 8  
23456789

我认为这是一个计时问题,因为当我使用调试器手动浏览代码时,结果字符串总是应该是:一个20字节的字符串。

我一直在搞乱各种各样的事情,但我无法解决问题。

以下是给我提出问题的代码部分:

static int baudrate = 9600,
           dataBits = SerialPort.DATABITS_8,
           stopBits = SerialPort.STOPBITS_1,
           parity   = SerialPort.PARITY_NONE;    

byte[] readBuffer = new byte[128];

...
...

public void serialEvent(SerialPortEvent event)
{
   if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

    try {
        if (input.available() > 0) { 
            //Read the InputStream and return the number of bytes read
            numBytes = input.read(readBuffer);

            String result  = new String(readBuffer,0,numBytes);
            System.out.println("Number of Bytes: " + numBytes);
            System.out.println(result);
        }
    } catch (IOException e) {
        System.out.println("Data Available Exception");
    }
}

3 个答案:

答案 0 :(得分:6)

串行数据只是一个数据流。根据您何时阅读以及正在进行的缓冲,只有部分数据在您阅读时可用。

由于您使用的是面向行的数据,您要做的就是缓冲数据,直到看到行终止符,然后才处理数据。

答案 1 :(得分:3)

我没有使用过Java RXTX,但是我使用过Arduino和Processing,从Arduino读取/写入值非常容易。 以下是Processing(文件>示例> Libraries> Serial> SimpleRead)附带的读取示例

/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  } 
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.print(1, BYTE);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.print(0, BYTE);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

*/

据我所知,当您实例化Serial时,您在Arduino中设置的波特非常重要。例如,如果您使用9600发送,则应使用相同的号码进行收听。

将信息作为 BYTE 发送非常重要,否则你会有像\ r或\ n这样的东西。

更短的版本,请尝试:

Serial.println(123456789123456789,BYTE);

越简越好。

答案 2 :(得分:1)

我认为你需要使用事件驱动的设计模式来解决这个问题。我强烈建议您访问:http://www.whatisarduino.org/bin/Tutorials/Java+Serial+API+and+Arduino

相关问题