3
votes

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 Eclipse on 64-bit Windows 7.

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.

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.

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.

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.

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?

Per suggestion, I tried reading Serial.available() instead of a fixed max of 10. No changes were experienced.

2
Which version of RXTX are you using? Which version of Java are you using?Jeffrey
Can you post all of your Java code? Preferably wrapped up nice and neat in a SSCCE.Jeffrey
You must forgive me but I am having trouble with the SSCCE, I'm not running on much sleep right now.Mark
RXTX is I beleive 2.1-7 I found the 64 bit dllsMark
Jre7, I'm changing the code now,Mark

2 Answers

2
votes

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.

0
votes

Try adding out1.flush() after out1.write().