kurbatov / firmata4j

Firmata client written in Java.
MIT License
87 stars 45 forks source link

array index bound exception #15

Closed francescoc93 closed 7 years ago

francescoc93 commented 7 years ago

There's a numerical overflow in "ask" method of FirmataI2CDevice class. The type byte in Java is signed. So, when the AtomicInteger return a value greater than 127, the value of variable "reg" is negative, and that, provoke an array index bound exception

kurbatov commented 7 years ago

I have commited the fix for that. Could you please confirm if it works now?

francescoc93 commented 7 years ago

now doesn't provoke an exception, but i can't get the response from the device when the index of array is greater than 127

francescoc93 commented 7 years ago

a workaround may be to set the size of the array to 128. The instruction register.compareAndSet(256, 1) will become register.compareAndSet(128, 1)

kurbatov commented 7 years ago

Unfortunately I haven't managed to reproduce the issue.

Another way around this bug is subscribing a listener and receiving updates continously. This way you don't have to invoke ask method all the time.

    i2cDevice.subscribe(new I2CListener() {...});
    i2cDevice.startReceivingUpdates(messageLength);
kurbatov commented 7 years ago

Register type was promoted from byte to int in 2.3.5 and it fixes described bug.