Android Question Serial hardware port

Nizze

Active Member
Licensed User
Longtime User
Hi All
I found a way if anyone else needs it
This works to read/write to Uart for me on my device

B4X:
Sub Class_Globals
    Private SerialOut As OutputStream
    Private SerialIn As InputStream
    Private astream As AsyncStreams
    Private phone As Phone
 
    Private mConnuri As String
    Private mPortspeed As String
    Private mCallback As Object
    Private mEvent As String
    Dim Response, Error As StringBuilder

    Dim SerialTimer1 As Timer
    Dim RestData As String
End Sub

Public Sub Initialize(Callback As Object, EventName As String, PortSpeed As String, URI As String)
 
    Log("Initializing Uart serial " )
    sb.Initialize

    mCallback = Callback
    mEvent = EventName
    mPortspeed = PortSpeed
    mConnuri = URI
    Log("Callback " & Callback )
    Log("Event Name " & EventName )
    Log("Port Name " & mConnuri )
    Log("Port speed " & PortSpeed )
 
    Response.Initialize
    Error.Initialize

    phone.Shell("stty -F " & mConnuri & " " & mPortspeed & " cs8 -parenb -cstopb -crtscts", Null, Response, Error)
    Log("Response : " & Response)
 
    Log("Error : " & Error)
    Log(" ")


End Sub

Public Sub start
    Log("Starting Uart serial " )
    SerialIn = File.OpenInput("",mConnuri) ' open serial input
    SerialOut = File.OpenOutput("",mConnuri,True) ' open serial output
    astream.Initialize(SerialIn,SerialOut,"Astream")
    UartState = True
    SerialTimer1.Initialize("SerialTimer1",30)
End Sub

Public Sub Disconnect
     
    astream.Close
    UartState = False
 
End Sub

Private Sub Astream_Error

    Log ("AStream_Error")
    Try
        astream.Close
        UartState = False    
        If SubExists(mCallback, mEvent & "_Error") Then
            CallSub2(mCallback, mEvent & "_Error", "Astream error")
        End If
    Catch
        Log(LastException)
    End Try

End Sub

Private Sub Astream_Terminated
    Log ("AStream_Terminated")
    If SubExists(mCallback, mEvent & "_Error") Then
        CallSub2(mCallback, mEvent & "_Error", "Astream Terminated")
    End If
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    SerialTimer1.Enabled = False
    Try
    Dim Recdata As String
    Recdata = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    RestData = RestData & Recdata
    Catch
        Log(LastException)
    End Try
    SerialTimer1.Enabled = True
End Sub

Private Sub SerialTimer1_Tick
    SerialTimer1.Enabled = False
    If SubExists(mCallback, mEvent & "_DataRecived") Then
        CallSub2(mCallback, mEvent & "_DataRecived", RestData)
    End If
    RestData = ""
End Sub

Public Sub WriteData(data As String)
    Log("Sending Serial data " & data )
    File.WriteString("","/dev/user_external_tty",data)
 
End Sub
 
Last edited:
Upvote 0

Pietro Pancino

Member
Licensed User
Longtime User
Hi All
I found a way if anyone else needs it
This works to read/write to Uart for me on my device

B4X:
Sub Class_Globals
    Private SerialOut As OutputStream
    Private SerialIn As InputStream
    Private astream As AsyncStreams
    Private phone As Phone
 
    Private mConnuri As String
    Private mPortspeed As String
    Private mCallback As Object
    Private mEvent As String
    Dim Response, Error As StringBuilder

    Dim SerialTimer1 As Timer
    Dim RestData As String
End Sub

Public Sub Initialize(Callback As Object, EventName As String, PortSpeed As String, URI As String)
 
    Log("Initializing Uart serial " )
    sb.Initialize

    mCallback = Callback
    mEvent = EventName
    mPortspeed = PortSpeed
    mConnuri = URI
    Log("Callback " & Callback )
    Log("Event Name " & EventName )
    Log("Port Name " & mConnuri )
    Log("Port speed " & PortSpeed )
 
    Response.Initialize
    Error.Initialize

    phone.Shell("stty -F " & mConnuri & " " & mPortspeed & " cs8 -parenb -cstopb -crtscts", Null, Response, Error)
    Log("Response : " & Response)
 
    Log("Error : " & Error)
    Log(" ")


End Sub

Public Sub start
    Log("Starting Uart serial " )
    SerialIn = File.OpenInput("",mConnuri) ' open serial input
    SerialOut = File.OpenOutput("",mConnuri,True) ' open serial output
    astream.Initialize(SerialIn,SerialOut,"Astream")
    UartState = True
    SerialTimer1.Initialize("SerialTimer1",30)
End Sub

Public Sub Disconnect
    
    astream.Close
    UartState = False
 
End Sub

Private Sub Astream_Error

    Log ("AStream_Error")
    Try
        astream.Close
        UartState = False   
        If SubExists(mCallback, mEvent & "_Error") Then
            CallSub2(mCallback, mEvent & "_Error", "Astream error")
        End If
    Catch
        Log(LastException)
    End Try

End Sub

Private Sub Astream_Terminated
    Log ("AStream_Terminated")
    If SubExists(mCallback, mEvent & "_Error") Then
        CallSub2(mCallback, mEvent & "_Error", "Astream Terminated")
    End If
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    SerialTimer1.Enabled = False
    Try
    Dim Recdata As String
    Recdata = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    RestData = RestData & Recdata
    Catch
        Log(LastException)
    End Try
    SerialTimer1.Enabled = True
End Sub

Private Sub SerialTimer1_Tick
    SerialTimer1.Enabled = False
    If SubExists(mCallback, mEvent & "_DataRecived") Then
        CallSub2(mCallback, mEvent & "_DataRecived", RestData)
    End If
    RestData = ""
End Sub

Public Sub WriteData(data As String)
    Log("Sending Serial data " & data )
    File.WriteString("","/dev/user_external_tty",data)
 
End Sub
Hi,
Do you have a full example code ?
 
Upvote 0
Top