Android Question How to distinguish the data received from multiple USB serial ports

MomoWen

Member
Licensed User
I'm using the felUsbSerial library for reading and writing, but when I have multiple serial ports sending messages to Android devices, I don't know that the devices in them are sending data
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
This is the english part of the Forum. Please write, at least, english here.
Alternatively you cn create a thread in the Forum of your language.
 
Upvote 0

MomoWen

Member
Licensed User
Dear Erel, I am following your tips to encapsulate the related operations of felUsbSerial into a class.And do data read and write operations in the class.But I can read, but I can't write. Can you help me understand why
 
Upvote 0

MomoWen

Member
Licensed User
FelUsbSerial has a problem on sending:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private usbserial As felUsbSerial
    Private manager As UsbManager
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Spinner1 As Spinner
    Private Button2 As Button
    Private Button3 As Button
    Private EditText1 As EditText

    
    Dim  usblist As List
    Type DeviceInfo (        DeviceID As String, _
                                Baud As Int, _
                                StopBits As Int , _
                                DataBits As Int , _
                                Parity As Int , _
                                Flow As Int     )
    Dim devInfo As DeviceInfo
    Dim D_str
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("t1")
    manager.Initialize
    usblist.Initialize
    devInfo.Initialize
    devInfo.DeviceID = ""
    devInfo.Baud = 115200
    devInfo.StopBits = usbserial.STOP_BITS_1
    devInfo.DataBits = usbserial.DATA_BITS_8
    devInfo.Parity = usbserial.PARITY_EVEN
    devInfo.Flow =usbserial.FLOW_CONTROL_RTS_CTS

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button3_Click
    Dim b(4) As Byte
    b=StringToHas(EditText1.Text)
 
    If usbserial.IsInitialized Then
        usbserial.Write(b)
        Label1.Text=b(0)&b(1)&b(2)&b(3)
    End If           
    
End Sub
Sub StringToHas(StrValue As String)As Byte()
    

    StrValue = StrValue.Replace(Chr(13),"")
    StrValue = StrValue.Replace(Chr(10),"")
    
    Dim str As String
    Dim buffstr() As String
    
    buffstr=split(" ",StrValue)
    Dim b(buffstr.Length) As Byte
    Log(buffstr.Length)
    Log(b.Length)
    For i=0 To buffstr.Length-1
        Dim sum As Int
        str=buffstr(i)
        
        If str.CompareTo("")<>0 Then
        
            For j=0 To str.Length-1
                If HasToInt(Asc(str.CharAt(j)))<>17 Then
                    Log(Power(16,str.Length-j-1))
                    sum=sum+HasToInt(Asc(str.CharAt(j)))*Power(16,str.Length-j-1)
                End If
            Next
            b(i)=sum
        End If
    
    Next
    Label1.Text=(0)&b(1)&b(2)&b(3)
    Return b
        
End Sub

Sub HasToInt(i As Int) As Int
    If i >47 And i<58 Then
        i=i-48
    Else if i>64 And i<71 Then
        i=i-65+10
    Else if i>96 And i<103 Then
        i=i-97+10
    Else
        i=17
    End If
    Return i
End Sub
Sub split(Cutting As Char,buff As String)As String()
    Dim str As String
    Dim buffstr(buff.Length) As String
    
    Dim j As Int = 0
    For i=0 To buff.Length-1
        If  buff.CharAt(i)=Cutting Then
            If str.Length <>0 Then
                buffstr(j)=str
                j=j+1
                str=""
            End If
        Else
            str=str&buff.CharAt(i)
        End If
        buffstr(j)=str
    Next
    Dim buffstr1(j+1) As String
    For i=0 To buffstr1.Length-1
        buffstr1(i)=buffstr(i)
    Next
    Return buffstr1
End Sub
Sub Button1_Click
    If manager.GetDevices.Length >0 Then
        For i=0 To manager.GetDevices.Length-1
            Spinner1.Add(manager.GetDevices(i).DeviceName)           
            usblist.Add(manager.GetDevices(i))
        Next
    End If
End Sub

Sub Button2_Click
    

        
        Dim usb_D As UsbDevice
        usb_D=manager.GetDevices(0)
            
        
    
        If usb_D.DeviceName.CompareTo(D_str)=0 Then
            
            If manager.HasPermission(usb_D) = False Then
                ToastMessageShow("Please allow connection and click again.", True)
                manager.RequestPermission(usb_D)
            Else
    
                usbserial.Initialize2("serial", usb_D, -1,"CH34xSerialDevice")
                usbserial.BaudRate = devInfo.Baud
                usbserial.DataBits = devInfo.DataBits
                usbserial.StopBits = devInfo.StopBits
                usbserial.Parity = devInfo.Parity
                usbserial.FlowControl = devInfo.Flow
                usbserial.StartReading    '开始监听
                Label1.Text="开始监听"
            End If
        End If
        Label1.Text=Spinner1.SelectedItem

    
End Sub
Private Sub serial_DataAvailable (Buffer() As Byte)As String
    Msgbox("usbserial_DataAvailable","usbserial_DataAvailable")

    
    Label1.Text=Buffer.Length
    Dim str,RText As String
    
    RText=""
    If Buffer.Length = 0 Then
        Msgbox(    "return_s","show_text.Text")
        Return Null
    End If
    For i=0 To Buffer.Length-1
        str=Bit.ToHexString(Buffer(i))&" "
        If str.Length<>3 Then
            If str.Length>2 Then
                str=str.SubString(str.Length-3)
            Else
                str="0"&str
            End If
        End If
        RText=    RText&str
        
    Next
    Label1.Text=RText
    Return RText
End Sub
Sub Spinner1_ItemClick (Position As Int, Value As Object)
    D_str=Spinner1.GetItem(Position)

End Sub
 
Upvote 0

MomoWen

Member
Licensed User
Forgive me for being a newbie, but here's my little test code for the library class.It can read data, but it cannot send it.
 
Upvote 0

MomoWen

Member
Licensed User
I believe I have many errors, but I am sure the connection to my device is working and supported.So I was suspicious of my sending code, but I couldn't really find the problem.I apologize for my clumsiness
 
Upvote 0

MomoWen

Member
Licensed User
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private usbserial As felUsbSerial
    Private manager As UsbManager
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Spinner1 As Spinner
    Private Button2 As Button
    Private Button3 As Button
    Private EditText1 As EditText
    Private Label1 As Label
    
    Dim d_usb As UsbDevice
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("t1")
    manager.Initialize
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
    For i=0 To manager.GetDevices.Length-1
        Spinner1.Add(manager.GetDevices(i))
    Next
End Sub


Sub Spinner1_ItemClick (Position As Int, Value As Object)
    
End Sub

Sub Button2_Click
    Dim d_usb As UsbDevice
        d_usb=manager.GetDevices(0)
    If manager.HasPermission(d_usb) = False Then
        Label1.Text=Label1.Text&Chr(13)&Chr(10)&"HasPermission:"
        ToastMessageShow("Please allow connection and click again.", True)
        manager.RequestPermission(d_usb)
    Else

    usbserial.Initialize("usbserial",d_usb,-1)
    usbserial.BaudRate=115200
    usbserial.StopBits=usbserial.STOP_BITS_1
    usbserial.DataBits=usbserial.DATA_BITS_8
    usbserial.Parity=usbserial.PARITY_EVEN
    usbserial.FlowControl=usbserial.FLOW_CONTROL_RTS_CTS
    usbserial.StartReading    '
    If usbserial.IsInitialized Then
        Label1.Text="开始监听"
    End If
    End If
End Sub

Sub Button3_Click
    usbserial.Write("a".GetBytes("UTF-8"))
End Sub

Private Sub usbserial_DataAvailable (Buffer() As Byte)As String
'    Msgbox("usbserial_DataAvailable","usbserial_DataAvailable")
'    Dim Buffer_L As Int
'    Buffer_L=Buffer.Length
'    If cache_attribute.Fill_bit+Buffer_L>1023 Then
'        cache_attribute.cover_ok=True
'    End If
'    cache_attribute.Start_bit=cache_attribute.Fill_bit
'    For i=0 To Buffer_L
'        If cache_attribute.Fill_bit+i = 1023 Then
'            cache_attribute.Fill_bit=0
'        End If
'        cachebuff(cache_attribute.Fill_bit)=Buffer(i)   
'    Next
    
'    Msgbox(Buffer.Length,"show_text.Text")
    Dim str,RText As String
    
    RText=""
    If Buffer.Length = 0 Then
        Msgbox(    "return_s","show_text.Text")
        Return Null
    End If
    For i=0 To Buffer.Length-1
        str=Bit.ToHexString(Buffer(i))&" "
        If str.Length<>3 Then
            If str.Length>2 Then
                str=str.SubString(str.Length-3)
            Else
                str="0"&str
            End If
        End If
        RText=    RText&str
        Msgbox(    RText,"show_text.Text")
    Next
    Label1.Text=RText
    Return RText
End Sub
 
Upvote 0

Similar Threads

Replies
1
Views
599
Replies
1
Views
485
  • Question
Android Question Port Detect
Replies
1
Views
975
Top