Go Down

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

jeffreyy

Hello,

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]
Serial.print('a'); 
Code: [Select]
Serial.write(outputValue);

and in my netbeans program, use:
Code: [Select]
private char myByte
Code: [Select]
myByte=((char)(input.read()));

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

PaulS

Quote
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.

PeterH


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.

jeffreyy

Hello,

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);

or
Code: [Select]

number2 = Integer.parseInt(number);


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

best regards

holmes4

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

Mark

jeffreyy

how do you mean?, don't send strings with the arduino to my java program?

Arrch


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

Mark


I get the feeling the latest code he posted was Java code.

PaulS

Are those code snippets from your Arduino code, or from your Java code?

In either case, perhaps the good folks at http://snippets-r-us.com can help. We're a little weird here. We need to see ALL of the code.

jeffreyy

whow sorry, the code i posted was indeed java  code.
my bad.
i shall ask it on another forum.

ps: PaulS, i can't open your link, site doesnt work.

best regards

PaulS

Quote
ps: PaulS, i can't open your link, site doesnt work.

Of course it doesn't. That's exactly the point.

PeterH


Code: [Select]

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


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



I know it's not an Arduino question any more, but you're using the wrong substring function. There are two overloaded function. What you're using takes the three characters following the space, which assumes that the string does actually contain exactly three more characters. If you use this instead, it will take the characters after the separator up to the end of the string:
Code: [Select]

int spacePos = result.indexOf(" ");
number= result.substring(spacePos);


If your Integer parsing doesn't produce the value you expect, print the string you parsed and the value you got back and it will probably be obvious why you got the result you did.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up