Go Down

Topic: Sending parameters to Java (Read 1 time) previous topic - next topic

azziplekkus

Oct 17, 2012, 10:08 pm Last Edit: Oct 17, 2012, 10:27 pm by azziplekkus Reason: 1
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 ;)

With kind regards

Azziplekkus

PaulS

Quote
Just for starters, I want to have my arduino to send numbers/words

Code: [Select]
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.

azziplekkus

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: [Select]
"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?


PaulS

Quote
Got any idea on how to do this?

Of course.

Code: [Select]
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 >.

azziplekkus

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

PaulS

Quote
Mhmmm, do you know how I can write that in Java?

So it runs on the Arduino? You can't.

azziplekkus

#6
Oct 20, 2012, 05:39 pm Last Edit: Oct 21, 2012, 12:50 am by azziplekkus Reason: 1
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 :)

Amjad


Mohammed90


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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy