B4J Question Raspberry Pi serial with jSerial

Jon Eskdale

Member
Licensed User
Longtime User
Trying to read and write to the serial port on the Raspberry Pi 3B+

Using B4J 7, jSerail library

I get the following error when I do the astream.initialize

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc


B4X:
Sub btnRead_Click
    Dim s As String
    Dim o As Object
   
    If splist.Size = 1 Then
        sp.Open(splist.Get(0))
        sp.SetParams(115200,8,1,0)
        'o=sp.GetInputStream
        'o=sp.GetOutputStream
        'astream.Close
        astream.Initialize(sp.GetInputStream,sp.GetOutputStream,"astream")
        'astream.InitializePrefix(sp.GetInputStream, True, sp.GetOutputStream, "astream")
    End If
End Sub

splist is a list of serial ports from splist = sp.ListPorts
after the sp.Open you can see it has opened the device /dev/ttyAMA0 which is the only device it finds




You can see I tried o=sp.GetInputStream and o=sp.GetOutputStream. These don't fail but hovering over the o doesn't show anything - (should it?)
Also tried the astream.InitializePrefix but get the same error


Have run out of ideas on what to try - any ideas please?
Has anyone had success?

EDIT: After hours of trying I tried a reboot and now I see two serial ports and seem to have got rid of the error so will continue and update

Thanks
Jon
 
Last edited:

techknight

Well-Known Member
Licensed User
Longtime User
I know the primary serial port is taken over by the console. on the RPI you have to disable the console on the serial port.
 
Upvote 0

Jon Eskdale

Member
Licensed User
Longtime User
@techknight Thanks for your reply it is appreciated - Sorry for the late reply - got attacked by the "Man Flu". For anyone following the thread what I've learnt.

The Pi has two serial ports - one of which is fully featured and one that is only TX and RX
on the older Pi's the full-featured one went to the Serial port pins
on the new Pi's the full-featured serial port is used for the Bluetooth and the simple ones go to the Serial port pins
You can disable the Bluetooth and switch the full serial port back to the Serial port
To enable the Serial port you have to edit
/boot/config.txt and add
enable_uart=1 at the end
To disable the Bluetooth and switch the UARTS
also add
dtoverlay=pi3-disable-bt
Then reboot

This then gave me the /dev/ttyAMA0 which I could use
I also stopped the bluetooth service with
sudo systemctl disable hciuart

It did basically work but I was missing some characters sometimes so then opted not to disable the Bluetooth and use the simple UART
in this case, I can see two UARTS
/dev/ttyAMA0 and
/dev/ttyS0

Using the device /dev/ttyS0 then works
Jon
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…