B4A Library GPIO and UART

Hi everyone,

I have been working on a project for some time where hardware plays a key role - UART and GPIO. In this particular case, I have to manage 4 RS232 ports and 12 GPIO's. For the GPIO's I developed a library. For UART communication I have used the SerialLibJab library with AsyncStreams from RandomAccessFile library. The SerialLibJab library works fine but the targetSdkVersion is limited to 19 and this annoyed me because the tablets I use have android 7.1.1 (API level 25) - so I decided to re-write this library and share the project.

The GPIO library:

The GPIO library was fully developed in Eclipse. In the attached zip folder is the whole Eclipse project, and of course the Java source code.

How to use in B4A :
B4X:
Private Sub buttonToggleGPIO_Click
    Dim myGPIO As GPIO
    Dim io As String = "gpio203"

    If myGPIO.getDirection(io) <> "out" Then
        myGPIO.setDirection(io, "out")
    End If
          
    If myGPIO.getValue(io).Contains("1") Then
        myGPIO.setValue(io, 0)
    Else
        myGPIO.setValue(io, 1)
    End If
End Sub


The UART library:

For this library I had to first build the .so file (shared library). There are several ways to do this, but Android Studio is currently an easy and friendly alternative to consider (check if NDK, CMake, and LLBD are installed).
- Start a new project
- Include C++ support
- Add NO Activity
Add the C/C++ files and edit the CMakeLits.txt file according to the project. Build the project and in the app/build/intermediates/cmake folder will be the shared library, in my case the libUART.so. This file will be used to build the UART.jar to be used in B4A. In the attached zip folder you will found the basic of the Android Studio project with C source code (I was unable to upload the entire project because of the size).
To build the UART.jar I used Eclipse, since it's a very well know topic in this forum. This library is basically constituted by 3 classes:
- UART - This is the link with the libUART.so (hardware) native library. Is based on SerialLibJab library.
- PortFinder - To discover the available UART devices.
- AsyncStreams - Ensures asynchronous data stream reading and writing in UART ports. This class has been extracted from the RandomAccessFile library and adapted to this project.
In the attached zip folder you will found the complete Eclipse project with Java source code.

How to use in B4A :
B4X:
......
    Dim myUART as UART
    Dim eventName As String = "myUART"
    Dim devPath As String = "/dev/ttyS0"
    Dim baudeRate As Int = 115200
    Dim flags As Int = 0
    Dim bigEndian As Boolean = False
    Dim prefix As Boolean = False
    
    myUART.Initialize(eventName, devPath, baudeRate, flags, bigEndian, prefix)
......    

Private Sub myUART_NewData (data() As Byte) 'the incoming data
   Dim bc As ByteConverter
   Log("RX: " & bc.HexFromBytes(data))
   'TODO... decodeTheData(data)
End Sub

Private Sub SendDataBytes(data() As Byte) 'the outgoing data
    Dim bc As ByteConverter
    Log("TX: " & bc.HexFromBytes(data))
    myUART.Write(data)  
End Sub

Private Sub myUART_Terminated
    Log("UART - Terminated")
    'TODO... 
End Sub

Private Sub myUART_Error
    Log("UART - Error: " & LastException)
    'TODO... 
End Sub

Private Sub buttonGetDevices_Click
    Dim pf As PortFinder 'UART library   
    labelDevices.Text = pf.GetAllDevicesPath
    'the result in my case is:
    '/dev/ttyUSB0
    '/dev/ttyUSB1
    '/dev/ttyUSB2
    '/dev/ttyUSB3
    '/dev/ttyS0
    '/dev/ttyS1
    '/dev/ttyS2
    '/dev/ttyS3
    '/dev/ttyS4
End Sub


I hope this is helpful for someone.

RC
 

Attachments

  • eclipse.zip
    96.6 KB · Views: 477
  • B4A.zip
    40.7 KB · Views: 549
  • android_studio.zip
    142 KB · Views: 411

EvgenyB4A

Active Member
Licensed User
Longtime User
As to GPIO. How do you define to set or get value for GPIO channel 1,2 or 12?
As to UART. What are the values for " flags" and "prefix" variables?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…