Android Question FTDI FT312D problem

lucky5822

Member
Licensed User
Hi guys,

I just bought this device https://www.startech.com/it/m/Sched...tatore-micro-usb-seriale-android~ICUSBANDR232.

I'm trying to send some commands using this RS232 adapter.
What is the best way to use it?
I'm using the usb library to discover the adapter as an accessory and sending commands using the asyncstream. Everything works fine for the first start of the app but when i close it and open again an error appear: libcore.io.ErrnoException: write failed: ENODEV (No such device). I have to unplug/plug the adapter to make it work again.
I tried different libraries (felusb,usb serial) without results.

If i use the inputstream and outputstream instead asyncstream the problem disappear but i can't read the answer correctly. (Partial answer)

Help please..
 

Peter Simpson

Expert
Licensed User
Longtime User
Upvote 0

lucky5822

Member
Licensed User
Thanks peter for your reply. I just saw that post. I searched deeply in the forum before start a new thread. The guy in that post have my same problem but the thread end with a post that says is an android bug. I think is not correct because if i don't use the async library the problem desappear. So it means that Android manage correctly this accessory. Probably i'm using the asyncstream library wrongly or i'm using the wrong library.
 
Upvote 0

lucky5822

Member
Licensed User
i'm using the USB library https://www.b4x.com/android/help/usb.html version 0.98.
i took a look at your application and you used a PL2303 Chip based hardware. it is recognized by android as a device. so you were able to use the usb serial library. my serial adapter is a FT312 chip based and is recongnized as an accessory so i have to use the USB library.

here is my code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim scrivi As OutputStream
    Dim leggi As InputStream
    Dim manager As UsbManager
    Dim access As UsbAccessory
        Dim astream As AsyncStreamsText

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("layout_lw3")
    tb_quota.Color = Colors.White
   
    Dim jo As JavaObject = Activity
    Dim Window As JavaObject = jo.RunMethodJO("getContext", Null).RunMethod("getWindow", Null)
    Window.RunMethod("addFlags", Array As Object(524288)) 'FLAG_SHOW_WHEN_LOCKED
    Window.RunMethod("addFlags", Array As Object(128)) 'FLAG_KEEP_SCREEN_ON
   
    If FirstTime Then
        manager.Initialize
    End If
   
    'chiedo i permessi
    'controllo se il convertitore è collegato
    Dim FT31found As Int = -1
    Dim UsbAcc() As UsbAccessory
   
    UsbAcc = manager.GetAccessories
    If UsbAcc.Length <1 Then
       
        ToastMessageShow("COLLEGARE IL CONVERTITORE", True)
       
        Return
       
    End If
   
    For i= 0 To UsbAcc.Length -1
       
        If UsbAcc(i).Model.Contains("FT31") Then
            FT31found = i
            access=UsbAcc(i)
        End If
       
        If FT31found < 0 Then
            Return
        End If
       
    Next
   
    'controllo se ha i permessi
    If manager.HasAccessoryPermission(access) = False Then
       
        manager.RequestAccessoryPermission(access)
       
    Else
       
        If FirstTime = True Then   
           
        manager.OpenAccessory(access)
        'setto il convertitore
        scrivi = access.OutputStream
        'leggi = access.InputStream
       
        'NUOVO
       
        SetConfigFT31x(19200,8,1,0,0)
        astream.Initialize(Me,"astream",access.InputStream,access.OutputStream)
                   
        End If
       
    End If
   
   

End Sub

when i stop the debug and start it again i have the error: libcore.io.ErrnoException: write failed: ENODEV (No such device)

i have to unplug and plug again to reset the rs232 adapter and make it work again.

thanks for the support.
 
Upvote 0

lucky5822

Member
Licensed User
Hi Erel,

thanks for your answer.

i tried usbserial and felusbserial.

the big problem is that my RS232 adapter is recognized by my tablet as an accessory and not as a device.

so with usbserial i have the error "error opening usb".
instead, using felusbserial, i can't initialize the usbmanager because no devices were found. following are part of your code where i have the error.

B4X:
If manager.GetDevices.Length = 0 Then
     Log("No connected usb devices.")
   Else
     Dim device As UsbDevice = manager.GetDevices(0) 'assuming that there is exactly one device
     If manager.HasPermission(device) = False Then
       ToastMessageShow("Please allow connection and click again.", True)
       manager.RequestPermission(device)
     Else
       usbserial.Initialize("serial", device, -1)
       usbserial.BaudRate = 115200
       usbserial.DataBits = usbserial.DATA_BITS_8
       usbserial.StartReading
     End If
   End If

my code always returns "no connected usb device" and if i try to discover it as an accessory i have the error in usbserial.initialize because argument number 2 have to be a device not an accessory.

i'm stucked please help me.....
 
Upvote 0
Top