Go Down

Topic: char and integer with serial communication (Read 740 times) previous topic - next topic



i wan't to send some data with serial communication to a java (netbeans) program.

Buth the data contains a char (or string) and a analoge value (from A0)

now is my question, how i can send this to my java program? when i use
Code: [Select]
Code: [Select]

and in my netbeans program, use:
Code: [Select]
private char myByte
Code: [Select]

it works, buth i can only see the first character of my Analog pin.

when i use something else, like reading a string in stead of a char, i only get numbers.
so the string i converted to numbers.

is there a way how i can send both of them?

best regards


it works, buth i can only see the first character of my Analog pin.

Because you are only reading the first byte. Serial.write() of a multibyte value sends multiple bytes.

For ease of debugging, I'd avoid using Serial.write(). Send the outputValue as a string, followed by a delimiter (the cr/lf added by println() is perfect).

Then, read the data until the delimiter arrives, storing it in an array. Then use whatever Java's equivalent of atoi() is to convert the string to a number.


Java's equivalent of atoi()

Integer.parseInt(), or Integer.valueOf(), depending whether you wanted an int primitive or an Integer object.
I only provide help via the forum - please do not contact me for private consultancy.



i've got it to work, i can receive the string for example : "w: 125",
this is stored in variable : result.

i have also added this code, so i can de substring:

Code: [Select]

int spacePos = result.indexOf(" ");
number= result.substring(spacePos +1, (spacePos+1)+3);

this means i filter the w: and the space away.

when i print this, it works, i only see the number, buth if i wan't to do something with it, i have to convert it to a integer.

when i use :
Code: [Select]

number2 = Integer.valueOf(number);

Code: [Select]

number2 = Integer.parseInt(number);

and i print number 2, i don't see anything.
can somebody help me?

best regards


Don't use the String class with the Arduino!.


Go Up