views:

52

answers:

3

I tried to ask this question earlier, but I was unclear in my question. http://stackoverflow.com/questions/3239740/java-bufferedreader-action-on-character

Here is my problem.. I have a BufferedReader set to read from a device. It is reading well. I have it set to

if (Status.reader.ready()) {
    Lines = Status.reader.readLine();
}
if (Lines.contains(">")) {
    log.level1("ready to send data")
}

Buffered reader does not report the > until I've sent more data to the device. The problem is that when reader contains > it is not reporting ready. It holds onto the > until I input more data.

I tried the following and it returns nothing. It does not even return the log.level0()

Lines = ""

try {
    Lines = Status.reader.readLine();
} catch (IOException e) {
    Log.level0("Attempted to read blank line");
}

Here is the actual data sent:

^M^M01 02 F3^M00 01 F3 3E^M>

But BufferedReader ignores the > until more data has been sent then get a result like this:

>0102

When I check the actual data from the device from the command prompt, it returns what I'd expect, the > is present.

BufferedReader will not give me the >. Is there some way I can check for this char otherwise?

+4  A: 

The BufferedReader.readLine() method reads data a line at a time. That is, it will attempt to read characters until it sees an end-of-line sequence (e.g. "\n", "\r" or "\r\n") or the end of stream.

If your input data is not line oriented, then you should not be using readLine() to read it. I suggest that you do your own record / message extraction; e.g.

BufferedReader br = ...
StringBuilder sb = new StringBuilder(...);
int ch = br.read();
while (ch != -1 && ch != '>') {
    sb.append((char) ch);
    ch = br.read();
}
String record = sb.toString();
Stephen C
That is a very long procedure which adds objects. the final code ended up going from what it was to a while loop with a (char)lines.read();
Adam Outler
@Adam Outler - I don't understand what you are saying, or how it is relevant to my answer.
Stephen C
+1  A: 

Check this: http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/BufferedReader.html

I recommend that you use the function public int read() instead.

At google you can find a lot of examples1

oagostinho
We don't lmgify on SO.
Stephen C
A: 

With those F3s in there it looks to me like your data isn't even character-oriented let alone line-oriented. Is your device really Unicode-compliant?

I would use a BufferedInputStream.

EJP
It transfers hex data with ^M delimiters. Then it Requests data with >. It's integer data.
Adam Outler
Exactly, so you shouldn't be using a Reader at all. See above.
EJP
no, it works fine. I just needed to use read instead of readline. it is character oriented. It is very much char oriented. Passing data through (char) read(); worked very well and changed my delmiters to \r instead of raw ^M
Adam Outler
hexidecimal is a way of displaying chars.
Adam Outler
This answer misses the point. A device does not need to be Unicode compliant to produce data that you can read as characters in Java. Simply use an InputStreamReader providing an explicit `encoding` argument to specify the stream's character encoding.
Stephen C