B4A Library felUsbSerial - Alternative Usb Serial library

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jan 10, 2016.

Thread Status:
Not open for further replies.
  1. Wal

    Wal Member Licensed User

    I found the Error.

    FTDISioIds.java:
    new ConcreteDevice(0x04d8, 0x000a)

    but the device is a cdcserial Device
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    V1.00 is released (first post). It adds a new Initialize2 method that lets you to explicitly choose the serial class.

    For example:
    Code:
    fel.Initialize2("fel", Device, -1"CDCSerialDevice")
     
    Wal likes this.
  3. Wal

    Wal Member Licensed User

    Thank Erel it works, we need now is SetRTS and SetDTR. ;)
     
  4. Kurt McCullum

    Kurt McCullum Member Licensed User

    Is it possible to have both CTS/RTS and Xon/Xoff active at the same time. I've got a device that I'm trying to connect to that requires CTS/RTS to be active even though it doesn't always use it. With VB.Net I can set the port to Both which allows me to connect to the device but I don't see the setting here.

    Kurt
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    No. They cannot be combined.
     
  6. JordiCP

    JordiCP Well-Known Member Licensed User

    I need to communicate with a CDC-device to stablish a simple protocol. On the other side there is an embedded board. I haven't used Android USB up to now, so I am quite new in this.

    Should I use this library for this, or better use USB-Host library? (or both are good for the purpose?)
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can either use UsbSerial library or this one.
     
    JordiCP likes this.
  8. Yuretz2

    Yuretz2 Member Licensed User

    Hi
    Im using example from the first post.

    I connected FTDI device, but i know that in my tablet also is some internal device that usb.manager finds as device(0), so im using GetDevices(1) to connect to FTDI
    Code:
    Dim device As UsbDevice = manager.GetDevices(1'assuming that there is exactly one device
    After "usbserial.StartReading" my log look like that:

    Code:
    Installing file.
    PackageAdded: package:b4a.example
    ** 
    Service (starter) Create **
    ** 
    Service (starter) Start **
    ** 
    Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    Data obtained from Read buffer: �`
    Raw data from Read buffer: 
    0xB1 0x60
    Number of bytes obtained from Read buffer: 
    2
    Data obtained from Read buffer: �`
    Raw data from Read buffer: 
    0xB1 0x60
    Number of bytes obtained from Read buffer: 
    2
    Data obtained from Read buffer: �`
    Raw data from Read buffer: 
    0xB1 0x60
    Number of bytes obtained from Read buffer: 
    2
    Data obtained from Read buffer: �`
    Raw data from Read buffer: 
    0xB1 0x60
    Number of bytes obtained from Read buffer: 
    2
    My FTDI device does not transmit any data at that time. "serial_DataAvailable" is not rised as you can see.
    Is it normal? Can this unknown data interfere with my data then FTDI will start transmit?
    Thank You
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    If DataAvailable is not raised then you can probably ignore those messages.
     
    Yuretz2 likes this.
  10. yo3ggx

    yo3ggx Active Member Licensed User

    Does this library supports FTDI based USB/Serial interfaces with different VID/PID than the standard chip?
    Dan
     
  11. Yuretz2

    Yuretz2 Member Licensed User

    Yes, you can specify that you device is FTDI with:
    Code:
    fel.Initialize2("fel", Device, -1"FTDISerialDevice")
    For autostart app then device is connected:

    Manifest:
    Code:
    AddActivityText(main, <intent-filter>
        <action android:name=
    "android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </
    intent-filter>
        <meta-data android:name=
    "android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource=
    "@xml/device_filter" />)
    YourAppFolder\Objects\res\xml\device_filter.xml:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- 
    0x0403 / 0x6001: FTDI FT232R UART -->
        <usb-device vendor-
    id="1027" product-id="24577" />

        <!-- 
    0x067b / 0x2303 Prolific PL2303 -->
        <usb-device vendor-
    id="1659" product-id="8963"/>

        <!-- Your device PID/VID -->
        <usb-device vendor-
    id="YourVID" product-id="YourPID"/>

    </resources>
     
    Last edited: Apr 9, 2016
  12. Erel

    Erel Administrator Staff Member Licensed User

    Note that you can use the new CreateResource keyword in the manifest editor to create the XML file:
    Code:
    CreateResoutce(xml, device_filter.xml,
    <resources>
    <!-- 
    0x0403 / 0x6001: FTDI FT232R UART -->
    <usb-device vendor-
    id="1027" product-id="24577" />

    <!-- 
    0x067b / 0x2303 Prolific PL2303 -->
    <usb-device vendor-
    id="1659" product-id="8963"/>

    <!-- Your device PID/VID -->
    <usb-device vendor-
    id="YourVID" product-id="YourPID"/>

    </resources>
    )
     
  13. yo3ggx

    yo3ggx Active Member Licensed User

    It seems that my chip is already included in the library. There is any way to get the USB device name using this library?
    If i try device.DeviceName I get the full path to the device, not the device name.

    The XML file is mandatory if I don't want the application to start automatically when the device is connected?

    Thank you.
    Dan
     
  14. Yuretz2

    Yuretz2 Member Licensed User

    No
    I'm not sure, but i think no. May be Erel can say for sure.
     
  15. yo3ggx

    yo3ggx Active Member Licensed User

    Thank you.
    Dan
     
  16. yo3ggx

    yo3ggx Active Member Licensed User

    Maybe I will try to use USBserial v2 just to get all the device information with usb.DeviceInfo.
     
  17. Emanuel Straub

    Emanuel Straub Member Licensed User

    Is it possible to connect multiple Usb devices with this libary?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Yes. You need to create multiple felUsbSerial objects.
     
    Emanuel Straub likes this.
  19. Emanuel Straub

    Emanuel Straub Member Licensed User

    Thanks for your reply.

    I wrote this sample code to test the multiple connection:

    Code:
    #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("Relaise connected!",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 connected!",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, 20) & wert & "0D"
        
    Next
        b = bc.HexToBytes(s)
        usb.Write(b)
    End Sub

    Sub btnExit_Click
        
    ExitApplication
    End Sub
    After the first run into Scanner_DataAvailable my Tablet (Huawei MediaPad X1/Andoid 4.4.2) shows the Toastmessage and then reboot.
    Any ideas?
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Does it only happen when both USB devices are connected?

    The second DataAvailable event is missing.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice