Note that there are some explanatory texts on larger screens.

plurals
  1. POSerial Communication between Java RXTX and Arduino
    text
    copied!<p>I'm trying to communicate between my PC (Windows 7 using Netbeans and RXTX) with an Arduino Pro, using the serial port. The Arduino is actually connected to the PC using an FTDI cable. </p> <p>The code is based on the Java SimpleRead.Java found <a href="http://java.sun.com/products/javacomm/reference/docs/API_users_guide_3.html" rel="nofollow noreferrer">here.</a></p> <p>Currently the Arduino simply prints out a string when it starts up. My Java program should print the number of bytes that have been read and then print out the contents. The Java program works, sort of... </p> <p>If the string is long (>10 bytes or so) the output will get broken up. </p> <p>So if on the Arduino I print</p> <pre><code>Serial.println("123456789123456789"); //20 bytes including '\r' and '\n' </code></pre> <p>The output of my Java program may look something like:</p> <pre><code>Number of Bytes: 15 1234567891234 Number of Bytes: 5 56789 </code></pre> <p>or</p> <pre><code>Number of Bytes: 12 1234567891 Number of Bytes: 8 23456789 </code></pre> <p>I'm thinking it's a timing problem, because when I manually go through the code using the debugger, the result string is always what it should be: one 20 byte string.</p> <p>I've been messing with various things but I haven't been able to fix the problem.</p> <p>Here is the part of the code that is giving me problems:</p> <pre><code>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() &gt; 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"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload