alejanrobellotti / java-simple-serial-connector

Automatically exported from code.google.com/p/java-simple-serial-connector
0 stars 0 forks source link

Garbage results when reading after writing #30

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I am communicating with custom code written on an Arduino UNO.  When a specific 
byte pattern is sent over the serial port (9600, 8 bits, no parity, 1 stop 
bit), the UNO will read its UUID from EEPROM and write them back over the 
serial port.

I have verified the Arduino is operating correctly by using CoolTerm and 
sending the byte pattern manually.  However when I do the steps in code, using 
JSSC, I get seeming random data back.

This is the code I am using:

> int retVal[] = null;
> byte msg[] = new byte[5];

> msg[0] = 0x48;                // Header 1
> msg[1] = 0x57;                // Header 2
> msg[2] = (byte) deviceID; // Addr
> msg[3] = 0;                   // Length
> msg[4] = 0;                   // Output Mode

> try {
>   this.serial.writeBytes(msg);

>   try {
>       retVal = this.serial.readIntArray(maxLen);
>   } catch (SerialPortException e) {
>       AppLog.get().log("Error reading from device " + deviceID);
>       AppLog.get().log(e);
>   }
> } catch (SerialPortException e) {
>   AppLog.get().log("Error writing to device " + deviceID);
>   AppLog.get().log(e);
> }

If I put a breakpoint on the line that reads "this.serial.writeBytes(msg);" and 
then single step though the code, I get the correct results.  If I run 
normally, or don't put a breakpoint there, then I get seemingly random data 
back.

I have tried putting a Thread.sleep(100) between the write and the read, 
thinking that it might be a timing issue, but that seems to have no effect.

I know that the Arduino is receiving the data correctly, because the other 
commands that don't reply work perfectly.  It is just when I read after writing 
that things go wrong.

What version of the product are you using? On what operating system?

JSSC 0.9, OSX 10.8.2

Any help is greatly appreciated

Original issue reported on code.google.com by jockmur...@gmail.com on 20 Jan 2013 at 1:00

GoogleCodeExporter commented 8 years ago
some additional information: If I change the line 

> retVal = this.serial.readIntArray(maxLen);

to be

> retVal = this.serial.readIntArray();

then readIntArray always returns null.  Even if I put a healthy sleep in front 
of it, or loop until it returns a non null value (It loops forever).  So I am a 
bit baffled where it is getting data from if I pass in maxLen.

Original comment by jockmur...@gmail.com on 20 Jan 2013 at 4:08