serial.read() not working after upgrade

37 views
Skip to first unread message

Craig Colbert

unread,
Aug 28, 2015, 11:19:10 PM8/28/15
to Pi4J
below is the code I've used for well over a year...dediced I should upgrade now the program will not work or compile.

        Timer timer2 = new Timer(10, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            serial.write('\r');
for(int i=0; i<16; i++)
{
radecArray[i]= serial.read();
}
radecString = new String(radecArray);
                                try {
processNexus(radecString);
} catch (PhidgetException exception) {
printError(exception.getErrorNumber(), exception.getDescription());
}
            }
        });
        timer2.start();


The compile error I get is...

/home/pi/scopeDogFolder/scopedog/scopedog.java:323: error: incompatible types

radecArray[i]= serial.read();

                            ^

  required: char

  found:    byte[]

-------------------------------------------------------------------

Docs say read() returns a char and this was the case before the upgrade. It appears now to return a byte


How do I fix this???




Robert Savage

unread,
Aug 29, 2015, 10:49:30 AM8/29/15
to Pi4J
Upgrade from which older version to which newer version ? :-)

If you upgraded to the latest 1.1-SNPSHOT build, then yes the Serial interface and underlying impl has changed significantly to handle data as bytes rather than as strings.  1.1 is not backwards compatible API for the Serial API.  

Here is the example code for the 1.1 serial impl:

And the interface: 


Thanks, Robert

Robert Savage

unread,
Aug 29, 2015, 10:56:30 AM8/29/15
to Pi4J
Yeah ... the new serial::read() returns a byte array.

You can see it here:

If you are really wanting a CHAR, then you can use the overloaded read(charset) method where you specify the explicit charset you want the raw bytes decoded into.
It returns a CharBuffer, not a single char.

public CharBuffer read(Charset charset) throws IllegalStateException, IOException;


Thanks, Robert



On Friday, August 28, 2015 at 11:19:10 PM UTC-4, Craig Colbert wrote:

Craig Colbert

unread,
Aug 29, 2015, 2:10:20 PM8/29/15
to Pi4J
Thanks for the quick response and for any experienced java program what I'm looking at might make sense but I'm not getting it...I tried a few changes i.e. read(charset)...but getting the import in and getting it to compile it just crashes...I don't have the time to spend to figure this out atm. This is a program running tracking and goto on several telescopes and a star party is in 1 week and I have a regular job. It's a good thing I backed it all up before I upgraded. 

The examples you sent me to use the listener and back when I first wrote the code I was doing it that way. Only problem was that it missed half of the data postings and this is why I went to the single char read which worked perfect. 

Not sure what my solution is other than stick with the older version unless you can give me a more guidance. 

Thanks

Craig Colbert

unread,
Aug 29, 2015, 2:45:20 PM8/29/15
to Pi4J
Never mind the below...I was trying to make it much harder than it was... I just changed the radecArray[i] to a byte array. read the buffer all at once and the problems all went away. 
.

Robert Savage

unread,
Aug 29, 2015, 3:11:31 PM8/29/15
to Pi4J
Glad to hear you got it up and running!  Thanks for the update.

Some of the original eventing problems as well as many users really wanting to access the serial data as bytes prompted this re-write of the serial impl.  I hope it's a lot more stable than the original version.  The original implementation was simply a small wrapper around the WiringPi serial impl but exposing the data as chars and strings was a mistake.  It worked OK for ASCII data, but was not well suited for anything more complex.  The new serial API is so much more sophisticated than the original :-)

Thanks, Robert
Reply all
Reply to author
Forward
0 new messages