Hello everyone, first time poster here. I will preface this by saying that I am completely new to Arduino/Hardware in general. I started a new job a week ago and have been given the assignment to work on a project which involves Arduino and Android communication. I am currently using usb-serial-for-android to communicate between the two. As for the project, I have already been able to send messages to the Arduino and have it do some work with that, so I know that the library is integrated correctly.
My issue is that I am trying to read a value which is being printed from the Arduino and read/receive it in the Android app. However, when I attempt to read this value I get very different results everytime. For the purpose of testing and not introducing many variables, I am using this simple code on the Arduino side:
Arduino Code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial); //wait for Serial Port to initialize
}
void loop() {
Serial.println("Hello");
delay(1000);
}
As you can see, I am simply printing "Hello" over and over, hoping to read that on the Android side. My issue is that my results are very mixed. Sometimes I will receive "He", "Hell", a blank string or some other weird combination of those.
Android Code:
port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
port.setDTR(true);
byte buffer[] = new byte[16];
int numBytesRead = port.read(buffer, 1000);
String arduinoData = new String(buffer, "US-ASCII");
Toast.makeText(getApplicationContext(), "Received " + arduinoData.substring(0, numBytesRead), Toast.LENGTH_LONG).show(); // Toast debugging
I checked out the Google Group for the library I'm using and someone was having the exact same issue here and was able to resolve it (although he never replied to someone else's request to post the code).
I played around with the size of the buffer as well as the timeout number that goes in the port.read() call, but to no avail. I really don't know what the issue could be at this point and would be eternally grateful for any guidance.
Thank you.