Pages: [1]   Go Down
Author Topic: Java and clock speed  (Read 1139 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I have successfully interfaced my Arduino Mega and accelerometer with Java, however I have a problem.

As the clocks aren't synced when I write to the USB and read from USB, I don't get the whole 'packet' i.e. I sometimes read half of it ( its probably being read as its being written).

I tried implementing some sort of ACK type packet switching, but thats not working too well. Just wondered if you guys have any bright ideas?

Thanks,
Lee.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not entirely sure what you're saying here (and without seeing any code), but have you used "Serial.available"?
Logged

Per Arduino ad Astra

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean by:

Quote
As the clocks aren't synced when I write to the USB and read from USB
???
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry if I wasnt clear,

What Im trying to say is this:

In arduino I write the xyz data from a tripple axis accelerometer. I write this to the USB like:
x + " " + y + "  " + z

so it should look like:

"xxx yyy zzz" where x, y, z are readings like 300 (for example)

The problem I have is reading these values from the USB using my java program.

Rather than obtaining:
"xxx yyy zzz"

I get:

"xxx yy"
"y zzz"

or something similar.

I am using the java code from here:
http://www.arduino.cc/playground/Interfacing/Java

I think the problem is that the Arduino board has a clock that ticks independantly from my computers clock. Therefore my java program reads indepentantly to the Arduino board tht is writing to the same place, therefore it 'reads' before all the data has been written to the USB.

My question is, what is a good way of buffering this, or just anything that might get around this problem: I need to process this data in its full form.

Many thanks,
Lee.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34213
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think the problem is that the Arduino board has a clock that ticks independantly from my computers clock.

What ever your problem is it is not this. This is totally irrelevant for an asynchronous communication system. That is communication between arduino and PC.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok,

Do you know what it could be why Im reading say 3 lots of results rather than 1? This indicates to me that the Arduino has pushed on 3 lots of data, and is read off at a slower rate. I understand Im probably barking up the wrong tree with the clock rate, however there must be something wrong here, i.e. I dont understand why the Arduino won't wait until their is no data left on the output stream before sending more data.

Could that instead be the problem? i.e. I need to implement a check to make sure the output stream is clear?

Thanks,
Lee
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I dont understand why the Arduino won't wait until their is no data left on the output stream before sending more data.

Serial communication is asynchronous. That means that one side does not wait for the other side. It works like a postal system. I can send you a letter. I can wait a couple days, and send you another letter. Then, I could wait for you to reply to both letters before sending another one.

You could reply to the first letter, and then reply to the second letter, or you could wait until you got both letters, and reply to both in one letter.

If you want the Arduino to wait until the Java application has processed the first packet, you can do that.

Have the Arduino send a packet. Then, have it wait for a reply from the Java application.

Have the Java application send a message when it has completed processing a packet.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It would help a lot if you psted some code :-)
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In your if (Serial.available()) block (which I presume you have somewhere) try adding this befor parsing data:
delay(20); //[20 is randomly selected] wait for the entire string to be buffered

But, as said above... Code would be helpful
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I tried implementing some sort of ACK type packet switching
Well, that sounds jolly complicated - what does the code for this look like?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Buffer the data from the serial input, then split the buffer at the linefeed and process the data before the LF, then wait for the buffer to fill with data for the next LF (hex 0x13 iirc)

"SerialPortEvent.DATA_AVAILABLE" doesn't mean it has got all the data you want, only that "some data" is available. The serial code at

http://www.arduino.cc/playground/Interfacing/Java

does not know that you want to process a data packet terminated with linefeed. you have to code that yourself.
Logged

Pages: [1]   Go Up
Jump to: