B4J Question B4J libraries for hardware under Linux

peacemaker

Expert
Licensed User
Longtime User
Hi, All

According the info i have found - under Linux the COM-ports, Bluetooth and stereo-audio output do not work with B4J libraries that are well known under Windows.

Maybe anyone touched it under Linux ?
What libs are needed for B4J under Linux ?
 

aeric

Expert
Licensed User
Longtime User
I only use COM ports in Linux with jSerial.
Search the forum. You need to add a special user group if you want to avoid using sudo.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
So, code is the same as for Windows ? Do you mean the port cannot be open due to some permission ?
Yes, the code is same for async stream.
The difference is the name of the com port.
You need permission to work with the COM port in Linux.

 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, Aeric, indeed.
I have set up WSL2 like here https://learn.microsoft.com/en-us/windows/wsl/connect-usb, and it can detect the serial device, but only under SUDO.

B4X:
    #if B4J
        #if LinuxDebugging or LinuxProduction
            Dim USBportPrefix As String = "/dev/tty"    '/dev/ttyUSB0
        #else
            Dim USBportPrefix As String = "COM" 'Windows COM-ports
        #End If
    #Else if B4A
        Dim USBportPrefix As String = "/dev/bus/usb/"    '/dev/bus/usb/001/003
    #End If

But how the app should be installed\provided for end-user ? With setting up the permissions\group rights ?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks, Aeric, indeed.
I have set up WSL2 like here https://learn.microsoft.com/en-us/windows/wsl/connect-usb, and it can detect the serial device, but only under SUDO.

B4X:
    #if B4J
        #if LinuxDebugging or LinuxProduction
            Dim USBportPrefix As String = "/dev/tty"    '/dev/ttyUSB0
        #else
            Dim USBportPrefix As String = "COM" 'Windows COM-ports
        #End If
    #Else if B4A
        Dim USBportPrefix As String = "/dev/bus/usb/"    '/dev/bus/usb/001/003
    #End If

But how the app should be installed\provided for end-user ? With setting up the permissions\group rights ?
I only can think that the step for setting permission is outside the installer.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
And if to detach the serial device from USB port - fatal crashing under Linux:
/dev/ttyUSB0: data is ОК
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
If the special permission is required for access to the COM-ports under Linux, and moreover after "sudo usermod..." the re-login is required - it means each app user must setup manually it for his user account 🤯 ... brain explosion.
It is not for "windows clickers"...
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I only use COM ports in Linux with jSerial.
Hi, @aeric

How does your Linux app process with the COM-port detaching ? If to unplug device's USB-cable.
I have internal system error that cannot be catched\wrapped and app is crashed:

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

peacemaker

Expert
Licensed User
Longtime User
Ha !
jSerial works OK under Windows, as well known and using, but ... it works OK also under Ubuntu Linux ! Tested as part of Win10 WSL2 - virtual Linux.
But if to try on real laptop under Linux Mint21 - it does not work, port is detected, but connection is done very rare and some system error is:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

Anyone seen, why different ?
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
I have been using a few B4J apps that access the serial port on both Windows and a variety of Linux Platforms (Ubuntu, Debian, etc.). Never had any issues with permissions. The default permission for the serial ports work fine. The application always runs under a sudo user. The serial ports tested are native serial ports as well as Virtual USB serial ports (/dev/ttyUSBx) using a serial/USB adapter. No issues with either.

Yes, depending on the driver, the program will crash if the USB/Serial adapter is unplugged. In some cases (driver/hardware) it will throw an exception and then automatically recover when the adapter is plugged back in. I have been unable to catch these exceptions using Try-Catch. So what I do is run the application as a systemd service and set it up so if the program crashes the service is restarted automatically. This will happen continuously until the devices is plugged in and detected again.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
application always runs under a sudo user
Ha, this allows all.

unable to catch these exceptions using Try-Catch
Yes ! Stupid. Under Windows all works automatically and all errors catched OK.

application as a systemd service
GUI app as a service ?! How to do it ?

My app under Windows can work with many USB-COM-ports at the same time in "plug-n-play mode", any can be detected when plugged or removed in the app when unplugged OK.
But now you explained - under Linux desktop it is impossible.... unbelievable.

Even the alone USB-device cannot be unlugged without crash ?! Maybe we have to track the USB devices events some how to close the COM-port on time?
 
Upvote 0
Top