0

I´m using

public abstract void write(int b) throws IOException;

from class OutputStream on Android Studio to sent Integer (degrees from 0 to 360) to the Serial Port of my Arduino over HC06 Bluetooth module.

Degrees from 0 to 256 are read well by using

 int angle = Serial.read();

Values over 256 (1 Byte) are received as 0 are something weird.

I tried using

 Serial.parseInt();

as it should return an long int but all I get is one digit mostly 0.

I also tried using

Serial.readBytes(threeDigitsInt, 3);
angle = (threeDigitsInt[0] * 100) + (threeDigitsInt[1] * 10) + threeDigitsInt[2];

but I cannot tell what ends up in the array. Definitly not the correct single digit. I tried char[] and byte[] but I´m not able to reconstruct my send Integer value.

Summary: I want to send 360 as Integer from Android and have it as Integer on Arduino.

Appreciate your help!

  • You are only sending one byte from 0 to 255. Read the manual. developer.android.com/reference/java/io/… – Majenko Aug 30 at 10:39
  • Ok. I see you´re right! Can you help me parse that int angle into an byte b [ ] so I can use the public void write(byte b[]) throws IOException { write(b, 0, b.length); } – Corazon Aug 30 at 10:42
  • You can't just throw data randomly at a serial connection. You need to have some kind of format so the receiver knows what byte is what. – Majenko Aug 30 at 10:43
  • Hm. I mean the format is recognized well on Serial.read() – Corazon Aug 30 at 10:46
0

First off, you are only sending one byte from Android:

public abstract void write (int b)

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

OutputStream.write(byte) can never write more than one byte.

Also it's clear you have no clue about what format the data is sent in if you are trying to then read 3 bytes and use Binary Coded Decimal to reconstruct it.

Instead I suggest you work in a purely ASCII format. Forget bytes. Bytes are not what you want, unless you feel like spending some extra time defining, and then implementing, your own protocol.

Start by formatting your data into an ASCII printable string. If Android Java is anything like normal Java I'd start by turning your integer into a string with proper line endings. Something like:

String val = String.format("%d\n", angle);

Then get the bytes that represent that formatted string:

byte[] valBytes = val.getBytes();

Now you have a valid byte array containing your textual representation of the data that you can send with OutputStream.write(byte[]).

Now your task is to read that data on the Arduino. That means reading each byte that arrives in turn storing it away in some form until you receive the \n line ending. At that point you can take each byte as a string and convert them into an integer again.

It's possible to do that on the fly:

static int angle = 0;

if (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') {
        Serial.print("I got: ");
        Serial.println(angle);
        angle = 0;
    } else if ((c >= '0') && (c <= '9')) {
        angle *= 10;
        angle += (c - '0');
    }
}

Basically, have a variable that stores the incoming angle. For each character that arrives on the serial port - if it's a line ending \n then do whatever with the angle value. If it's a character between 0 and 9 then multiply your angle by 10 (decimal shift left), convert your character to the equivalent integer by subtracting character '0', and add it to your angle.

  • Angle does not receive any value? – Corazon Aug 30 at 10:57
  • Maybe Android is sending things differently. You should first confirm exactly what is being sent by Android. – Majenko Aug 30 at 10:59
  • Also test the Arduino separate from Android. If it works using the serial monitor by typing in numbers then the Arduino code is fine. – Majenko Aug 30 at 11:00
  • Alright thanks a lot. I´ll take my time and try it today! – Corazon Aug 30 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.