java - Arduino not sending integers properly -
i trying send integers 0 through 10 arduino uno android device. however, arduino not sending integers separately, rather sending cluster (sometimes 2 @ time). want able send integer every 5 milliseconds , not delay longer that. ideas?
arduino code:
#include <softwareserial.h> const int rx_pin = 8; const int tx_pin = 9; softwareserial bluetooth(rx_pin, tx_pin); char commandchar; void setup (){ bluetooth.begin (9600); serial.begin(9600); } void loop () { if(bluetooth.available()){ commandchar = bluetooth.read(); switch(commandchar){ case '*': for(int = 0; < 11; i++){ bluetooth.print(i); delay(5); } break; } } }
android code:
public void run() { initializeconnection(); byte[] buffer = new byte[256]; // buffer store stream int bytes; // bytes returned read() while (true) { try { if(mmsocket!=null) { bytes = mminstream.read(buffer); string readmessage = new string(buffer, 0, bytes); log.e("received message ", readmessage); } } } catch (ioexception e) { log.e("error ", "reading btinputstream"); break; } } }
android monitor/console output:
08-18 19:46:32.319 6720-6749/? e/received message: 0 08-18 19:46:32.324 6720-6749/? e/received message: 1 08-18 19:46:32.324 6720-6749/? e/received message: 23 08-18 19:46:32.324 6720-6749/? e/received message: 4 08-18 19:46:32.379 6720-6749/? e/received message: 56 08-18 19:46:32.379 6720-6749/? e/received message: 78 08-18 19:46:32.379 6720-6749/? e/received message: 91 08-18 19:46:32.384 6720-6749/? e/received message: 0
it seems serial communication working stream (not datagram) , isn't keeping data boundary.
therefore, seems should add data separator (for example: newline) sending data , process in receiver (for example: use bufferedreader
) keep data boundary.
Comments
Post a Comment