German Mehrere USB-Geräte mit felUsbSerial Libary betreiben

Emanuel Straub

Member
Licensed User
Longtime User
Hallo an alle,

ich habe das Forum bereits durchsucht und leider keine Lösung gefunden. Deshalb versuche ich es auf diesem Weg.

Ich möchte 2 USB Geräte(Barcode-Scanner und Relaiskarte über OTG Adapter verbunden) mit der felUsbSerial Lib ansteuern. Leider klappt das nicht. Android stürzt nach dem Aufbau zum 2ten Gerät komplett ab.

Hier der Beispielcode:

B4X:
#Region  Project Attributes
    #ApplicationLabel: USB Example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: false
#End Region

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

Sub Process_Globals
    Dim usb As felUsbSerial
    Dim usb2 As felUsbSerial
    Dim manager As UsbManager
    Dim bc As ByteConverter
    Dim Timer1 As Timer
    Dim r_id As Int = 0x6001
    Dim r_vd As Int = 0x403
    Dim s_id As Int = 0x90a
    Dim s_vd As Int = 0xc2e
End Sub

Sub Globals

    Dim btnSend1,btnSend2,btnSend3, btnSend4, btnOpen As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    manager.Initialize
 
End Sub


Sub Activity_Resume

    btnSend1.Enabled = False
    btnSend2.Enabled = False
    btnSend3.Enabled = False
    btnSend4.Enabled = False
End Sub


Sub btnOpen_Click
    Dim UsbDevices() As UsbDevice
    UsbDevices = manager.GetDevices
    If UsbDevices.Length = 0 Then
        ToastMessageShow("No connected usb devices.", True)        
    Else
        For z = 0 To UsbDevices.Length - 1
            Dim UsbDvc As UsbDevice
            UsbDvc = UsbDevices(z)
             If (UsbDvc.ProductId = r_id) And (UsbDvc.VendorId = r_vd) Then            
                 If manager.HasPermission(UsbDvc) = False Then
                       manager.RequestPermission(UsbDvc)
                 Else
                       usb.Initialize("relais", UsbDvc,-1)
                       usb.BaudRate = 9600
                       usb.DataBits = usb.DATA_BITS_8
                    btnSend1.Enabled = True         
                    btnSend2.Enabled = True         
                    btnSend3.Enabled = True         
                    btnSend4.Enabled = True
                       ToastMessageShow("Relais verbunden!",True)
                    usb.StartReading
                    Timer1.Initialize("Timer1",5000)
                    Timer1.Enabled = True
                 End If
            End If
            If (UsbDvc.ProductId = s_id) And (UsbDvc.VendorId = s_vd) Then            
                 If manager.HasPermission(UsbDvc) = False Then
                       manager.RequestPermission(UsbDvc)
                 Else
                       usb2.Initialize("scanner",UsbDvc,-1)
                       usb2.BaudRate = 9600
                       usb2.DataBits = usb.DATA_BITS_8
                       usb2.StartReading
                    ToastMessageShow("Scanner verbunden!",True)
                 End If
            End If
        Next
    End If
End Sub

Sub scanner_DataAvailable (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg,True)
End Sub

Sub Timer1_Tick
    Dim b() As Byte
    Dim s As String
    Dim i As Int
    Dim wert As String
    For i = 1 To 4
        wert="00"
        If Rnd(0,2)=1 Then wert="01"
        s = s & "FF" & NumberFormat(i, 2, 0) & wert & "0D"
    Next
    b = bc.HexToBytes(s)
    usb.Write(b)
End Sub

Sub btnExit_Click
    ExitApplication
End Sub

Bin über jeden Tipp dankbar.

Gruß und schönes Wochenende.
 

Attachments

  • Usb.zip
    6.4 KB · Views: 274
Top