Pages: [1]   Go Down
Author Topic: Sending parameters to Java  (Read 1523 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

Even though I'm kinda new to Arduino, I'm a bit ashamed to ask something simple as USB communication here. Especially because there are loads of tutorials to be found online (even on the arduino.cc homepage)

But still... I haven't been able to get it to work =/

Just for starters, I want to have my arduino to send numbers/words to the PC, and read it in Java.
Anyone have a good tutorial for that? one that does include Java example code?

Thanks in advance smiley-wink

With kind regards

Azziplekkus
« Last Edit: October 17, 2012, 03:27:35 pm by azziplekkus » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Just for starters, I want to have my arduino to send numbers/words
Code:
Serial.print("Some words...");
int stuff = 14;
Serial.print(stuff); // Send a number

Quote
and read it in Java.
Java doesn't run on the Arduino. Can't help you there.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I know how to "send" it. But if I want to read this in Java it will say

"Some words...14"

Sometimes it even does this (if I had put it in a loop)

Code:
"Some word
s...14"

"So
me words...14"

"Some words
...14"

"Some words...1
4"

"So
me words...14"

It reads every character seperatly, and prints it in console.
This way, I can't get the strings and integers out of the stream... =/

Got any idea on how to do this?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Got any idea on how to do this?
Of course.

Code:
Serial.print("<"); // Send a start marker
Serial.print("Some words");
Serial.print(",");
Serial.print(someVariable);
Serial.print(",");
Serial.print(anotherVariable);
Serial.print(">"); // Send an end marker

Then, the Java applications needs to append characters to the string, starting when it sees the <, and stopping when it sees the >. Use the data only after you see a >.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mhmmm, do you know how I can write that in Java? I'm a noob programmer =/
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Mhmmm, do you know how I can write that in Java?
So it runs on the Arduino? You can't.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I mean how do I write that in Java? So I can get the strings out again?

EDIT: nevermind, a friend of me helped me with writing the code smiley
« Last Edit: October 20, 2012, 05:50:33 pm by azziplekkus » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please write the java code
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Mhmmm, do you know how I can write that in Java?
So it runs on the Arduino? You can't.

He meant that he wants with the Arduino via serial port using Java as the in-between-er to fetch and send the data over the serial port to the Arduino. That, sir, can be done. He does not want to run Java code on the Arduino. He says:
Quote
I want to have my arduino to send numbers/words to the PC, and read it in Java

Anyhow, here are a few links on how to start a serial connection on Java to read data from a serial port:
http://www.java-samples.com/showtutorial.php?tutorialid=11
and
http://stackoverflow.com/questions/900950/how-to-send-data-to-com-port-using-java
Logged

Pages: [1]   Go Up
Jump to: