• Post Reply Bookmark Topic Watch Topic
  • New Topic

RS232 - Receiving Data

 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to receive data from device which is sending data.
Instead of getting:
132
133
133
135
...
I'm getting:
13133
132
33
1133
133
132
133
132
13132
2
....

I tried to solve the problem. Im convinced thats format (byte to int,....) Problem. But I'm still confused. By the way: Hyperterminal did it without any problems.

The interesting part of the sourcecode
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

"new String(readBuffer)" is something to be suspicious of, because it uses the platform default encoding to convert bytes to String. Are you sure that that is the encoding used by the incoming data?

Also, Java assumes that incoming data is in network byte order (big endian). Is that the case here?
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Welcome to JavaRanch.

"new String(readBuffer)" is something to be suspicious of, because it uses the platform default encoding to convert bytes to String. Are you sure that that is the encoding used by the incoming data?


Well, since a few days i'm not sure of having my head on my shoulders. But I can post the whole code (isn't much 155 lines)

Originally posted by Ulf Dittmer:
Welcome to JavaRanch.
Also, Java assumes that incoming data is in network byte order (big endian). Is that the case here?


Yeah i know my code is with byte order.
In fact I didnt know exactly whats sending. It could be int, char,...

I only know there a values from 0 to 255.

Itried myself to change the byte order. But getting more and more javac-messages drives me crazy.

But before i'll forget:
Why is Hyperterminal able to read the values without mistakes?

I'm very very thankful for every answer I'll get
[ September 14, 2007: Message edited by: Sven Bayer ]
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the values "132" a single byte or a stream/array of characters? I'm wondering why you are reading 5 bytes at a time from the stream instead of single bytes. The output you show us is not all on one line. Where is the carriage return coming from? It is not in your code.
You should also have a look at our FAQ entries:
Available Doesnt Do What You Think It Does
Read Doesnt Do What You Think It Does
There's some subtle errors in your code that you'll want to fix.
As Ulf mentioned, converting bytes to Strings can be problematic. When dealing with bytes, I write out the hex value for each byte (via Integer.toHexString(int i)) to make sure I'm getting what I think I'm getting.
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are the values "132" a single byte or a stream/array of characters?

First of all I have to say its a Bluetooth-Device (ECG) sending the values to a dongle. Sorry, thats part of my Problem. I tried both. But I'm not convinced i did it the right way and throwed the files out of the window.


I'm wondering why you are reading 5 bytes at a time from the stream instead of single bytes. The output you show us is not all on one line. Where is the carriage return coming from? It is not in your code.

Why 5 Bytes and not one? Reading One Byte doesn't give anything out, it seems to stop without breaking up. You could also do the same thing with 10 Bytes. The output includes then the same plus some free lines.
Yes the carriage return is not in this part of the code. I'll posted at the bottom.


I didn't found a way of solution.


There's some subtle errors in your code that you'll want to fix.
As Ulf mentioned, converting bytes to Strings can be problematic. When dealing with bytes, I write out the hex value for each byte (via Integer.toHexString(int i)) to make sure I'm getting what I think I'm getting.


Yeah, this will make sens, but I'll have one day without Java, because trying to manage the Hex-Thing, i wasn't really successfull.

But I'll will follow that way.

I'll post the whole code even today. Now I'm not at home.


Finally I have to say: The members are still so helfpful.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I think I know what's going on here. The COMM API has an asynchronous API, that is, when data is available on the port, the COMM driver invokes the serialEvent method on your class. Since you are getting this data all the time from your device, it may be possible that serialEvent is invoked before you are done processing the data you've read in from the previous call, causing a race condition in your code.
You should probably look into using synchronous IO if you want to receive your data in the order that it is sent.
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it could be a race condition. I will try to fix it

I already forgot the code


[ September 17, 2007: Message edited by: Sven Bayer ]
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got your PM. Let's Use The Forum, Not Email.
I understand what you mean. But how do I manage it?


Have you read through the Comm API? You already have a SerialPort instance. Read through it and see if anything jumps out at you.
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I thought I did it, but now look at this
http://img374.imageshack.us/my.php?image=001gw4.jpg

Evrything seems OK for a while but a String like

"2\n
31"

causes the failure.

Any ideas?

[ September 18, 2007: Message edited by: Sven Bayer ]
[ September 18, 2007: Message edited by: Sven Bayer ]
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You attempt use Double.valueOf() to parse "2\n13" and it fails. Sounds like it is working correctly as that string is not a double numeric value. Your assumptions as to what data you are receiving are what is wrong.
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are 132 should be, there shouldnt be a 2\n13.

Perhaps I'm still wrong. But the offset of the device is ~132 and so a 2\n13 is terrible worth.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like the same problem you were having earlier (from your original post):

your code would probably parse the 3rd line as "131" then "32\n2" or something like that.
It doesn't look like you've gotten rid of the asynchronous code since the exception you post occurs in commListener.serialEvent(). Probably the same race condition you had earlier.
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i know I'm still abotu the problem.

Well thank you all for your help.
I'll make a step forward in this case someday.

It seems so easy but I'm still

OK closed for a while
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seriously? Try something like this:
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
your suggestion was not so well as it seemed.

Instead of a row of 132s
I got 50,13,10,49,51,50,...

Thank you for your help.
But what about my method. What do you mean, when I'm writing an exception like:

if 3\n12 don't print on screen

or something like that

Have you got a idea of realising that. IO Stream is not my favorite java-part
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sven Bayer:

I got 50,13,10,49,51,50,...


Look at your ASCII Table. Those are the integer values for the characters '2', 'CR', 'LF', '1', '3', '2'. . .
'CR' and 'LF' together being the line separator on a Windows system.
I'm not going to do your work for you, just point you in the right direction. The important thing about my code is that you receive all your data in order (the synchronous IO I spoke of earlier). It's up to you to do something useful with it.
 
Sven Bayer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thank you very much
I didn't recognized that thing.
I Suggest i was still

So



AND you are right I will do MY work on MY own.


I forgot: thread can be closed. I got my solution. Thank you Joe
[ October 10, 2007: Message edited by: Sven Bayer ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!