-1

Example data from serial port using System.out.print()

$GPGGA,134658.00,5106.9792,N,11402.3003,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60

This message ends with a carriage return followed by a newline. When I use the code below, I get this output.

$
G
P
G
G
A
,
.
.
.
   (Carriage return)
   (New Line)
$
G
P
G
S
A

My goal is to get a string that I can then split by ",". I want to reset/refresh that string on a carriage return/new line sequence.

    static class SerialPortReader implements SerialPortEventListener {

        public void serialEvent(SerialPortEvent event) {

            try {
                byte[] tester = serialPort.readBytes(1);

                //System.out.print(getdata);
                InputStream gpsStream = new ByteArrayInputStream(tester);

                BufferedReader in = new BufferedReader(new InputStreamReader(gpsStream, StandardCharsets.UTF_8));
                String output = in.readLine();
System.out.println(in);
            } catch (SerialPortException | IOException ex) {                  
            }    
        }
    }

Lets Assume this is the console output. I want to parse the most recent line. In this case the $GPGSA string.

$GPGGA,134658.00,5106.9792,N,11402.3003,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60
$GPGSA,134658.00,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60

I guess my question is how do I get the input stream into a format that I can parse?

New edit: When I use this code, it prints exactly how I need it.

How can I replicate the System.out.print(); function?

String wtf = serialPort.readString(event.getEventValue());
                System.out.print(wtf);

Console output from above string:

 $GPGSA,M,3,30,11,28,07,01,17,08,13,,,,,2.3,1.2,2.0*3D
$GPGSV,3,1,12,30,74,226,33,11,64,090,31,28,55,317,32,07,55,169,27*75
$GPGSV,3,2,12,01,51,118,22,17,31,239,25,08,28,050,34,13,18,300,22*75
$GPGSV,3,3,12,48,22,240,,19,08,233,,15,01,324,,22,00,118,*7A
javanewbie14
  • 13
  • 1
  • 5
  • 1
    When you use the "code below" you get nothing, because the "code below" doesn't print or write anything. --- Besides, `readLine()` will ***never*** return Carriage Return and New Line characters, so you're surely not using `readLine()` if you get those. – Andreas Jan 12 '18 at 23:26
  • See my edit. Besides, I do get the carriage return and newline as blanks with this code when i use println(); – javanewbie14 Jan 12 '18 at 23:41
  • How about: `System.out.println(output);`. What are you using to hold the reader open so as to continue retrieving data? Do you ever close anything? – DevilsHnd - 退職した Jan 13 '18 at 00:08
  • I am reading from a gps receiver. I will close it with a button in the GUI. – javanewbie14 Jan 13 '18 at 03:17

1 Answers1

0

for each SerialPortEvent it seems you're reading only one byte (=one letter)

serialPort.readBytes(1);

Then you're storing this one letter in a ByteArrayInputStream

To read the ByteArrayInputStream you're using a BufferedReader (when there isn't anything to buffer cause everything is already in your ByteArrayInputStream) to read the only one letter available

For now the only thing i need to know to go further in the answer is : what is the class of

serialPort

?

EDIT:

After a bit of googling, let's say you're using jssc

if you change the line

serialPort.readBytes(1);

to

serialPort.readBytes(serialPort.getInputBufferBytesCount());

is it better ?

if it is, you will then need a while loop around your readline function:

String output = null;
while((output=in.readLine()) != null){
    System.out.println(output);
}
Mumrah81
  • 2,034
  • 2
  • 16
  • 23
  • This is a no go. Doesnt print anything for me. – javanewbie14 Jan 13 '18 at 03:16
  • But the gist was there.you needed to read more than 1 byte. What do you mean by replicate system.out.print function? – Mumrah81 Jan 13 '18 at 08:10
  • If I use String getdata = serialPort.readString(event.getEventValue()); System.out.print(getdata); properly prints the entire string and splits. My understanding is it fills a buffer and flushes when it reads a newline. – javanewbie14 Jan 16 '18 at 15:36