B4A Library UsbSerial library 2.0 - supports more devices

Discussion in 'Additional libraries, classes and official updates' started by agraham, Apr 9, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    It requires Android 3.1 (API level 11). You can use the manifest editor to set this level as the minimum supported version.
     
  2. agraham

    agraham Expert Licensed User

    Needs Android 3.1, API level 12, or later with hardware host mode support. If you want to use on earlier versions use Try...Catch to catch the exception or check the version. I can't remember if one of the libraries has this but it's simple by Reflection.
    Code:
    Sub GetApi As Int
        
    Dim r As Reflector
        
    Return r.GetStaticField("android.os.Build$VERSION""SDK_INT")
    End Sub
    EDIT:- I missed that Erel had already replied!
     
  3. Quaki

    Quaki Member Licensed User

    Tahnks for the fast replyes :)
    Erel i have read your's thread about AsyncStreamsText class I think it will help :)

    agraham your idea about using IndexOf and SubString
    HTML:
    Every time you get some data add it to any previous data string and check if IndexOf finds both a "*" and a "#". Chop out the bit between them with SubString and shorten the string to keep any remaining data. Rinse and repeat.
    is aslo very interesting can you create any example code ?It is could be very helpfull to analize example code especialy the part of choping and shorten the the string to kept the ramianing data :)
    I was trying difrent way and the the code works almoust good but when the string once in a while comes without spliting the are two visible strings in label 2

    Code:
    Sub Astreams_NewData (Buffer() As Byte)
       
    Log("NewData")
       
    Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
        s= 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    If s.StartsWith ("p"AND s.EndsWith ("k"Then
       Label1.Text = s
    s=
    ""
    s1=
    ""
    Else

    If s1.Length = 28 Then
    s1=
    ""
    Label2.Text = s1
    End If

    s1 = s1 & s
    Label2.Text = s1
    If s1.Length = 28 Then

    s1=
    ""
    End If
    End If



    End Sub
    Best regards
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can check AsyncStreamsText code. It maintains the partial messages in a text buffer (StringBuilder).
     
  5. Quaki

    Quaki Member Licensed User

    Thanks Erel I will try to read and understend it :)
     
  6. agraham

    agraham Expert Licensed User

    Version 2.3 now posted includes an invisible change to the CDC ACM driver that might make it compatible with more devices.
     
  7. michw

    michw Member Licensed User

    Thanks for the tips:)
    I had to set the root and add the file "android.hardware.usb.host.xml" with the entry
    <permissions>
    <feature name="android.hardware.usb.host">
    </ feature> </ permissions>
    into the folder / system / etc / permissions
    Now it works to send and receive data.
    Thanks so much!

    Regards
     
  8. koep

    koep Member Licensed User

    Hi Andrew!

    Is it possible to add a verification of the device (for example product string) we are about to connect to before opening the port, and if not correct then looping through the device list until correct device found?

    Rgds,
    Tapio
     
  9. agraham

    agraham Expert Licensed User

    The underlying usb-serial-for-android code iterates the list of accessible devices and chooses the first one it recognises as being supported based on Vendor and Product IDs. Unless there is overwhelming need I don't want to change that behaviour by adding in a third variable.
     
  10. Rigsby

    Rigsby Member Licensed User

    First Off - Thanks For The Update To The Library

    It does seem to recognise the USB to Serial adaptor type OK, I have it working with FTDI & Prolific, but I wonder if you could possibly comment on how you could explicitly select 1 of the 4 driver types when you don't know either the vendor ID and productID. What values would you use ?

    usb.SetCustomDevice(usb.DRIVER_FTDI,0,0)

    It might not make any difference but I'd like to see if it does. Thanks, Rigsby.
     
  11. agraham

    agraham Expert Licensed User

    You can't, the underlying usb-serial-for-android code relies on matching VendorID and ProductID to recognise valid devices. If you don't know them you can see them with my UsbDetails app.
     
  12. Stulish

    Stulish Active Member Licensed User

    I just have to say great library, i have been trying to get my Pacific type USB/Serial adapters working for a while with no luck, so then i went out and brought an FTDI version and found this was the RL version not the R version and i couldn't get this working either.

    Now with the V2.0 lib i have all of them working flawlessly (it might also have something to do with the new OTG cable i brought, but having a lib that that is not specific to Pacific/FTD/...... is a lot better)

    Thanks a lot Andrew

    Regards

    Stu
     
    Last edited: Apr 19, 2013
  13. Stulish

    Stulish Active Member Licensed User

    I have noticed a couple of times on my NEXUS 7, that the app can freeze for between 1 to 10 seconds then the tablet resets.

    I will have to do some testing to see if it is a specific USB/Serial dongle attached or if it does it on any of my other android devices.

    I will let you know after i have investigated.

    Regards

    Stu
     
  14. koep

    koep Member Licensed User

    Hi!

    Is there any working example of write/read operation with usb-serial?

    With roboclaw I have several different writes(questions) to the board and I need to be able to sort the incoming reads(answers) to correct places.

    Now I have three consecutive write on timer tick but the problem is that newdata event is serviced after timer_tick finishes resulting in incoming ending up in wrong places.

    I need some way to run on timer:

    write1->read1->process1->write2->read2->process2->etc.

    perhaps with counting i/o streams?


    Rgds,
    Tapio
     
  15. agraham

    agraham Expert Licensed User

    You can't process a read in a Timer_Tick. The NewData event runs on the main thread so your timer code needs to exit before the NewData event is called.

    You should probably do the first write in the Timer code to kick things off and do subsequent writes in the NewData event after you have read the data. Note that not all your data may arrive tidily in one NewData event so you may have to reassemble the messages over two or more NewData events.
     
  16. koep

    koep Member Licensed User

    Hi Andrew!

    So you are suggesting something like:

    msgno as int = 0 in process globals
    --
    timer_tick
    write1
    msgno = 1
    --
    newdata
    select msgno
    case 1
    process1
    msgno = 2
    write2
    case 2
    msgno = 3
    process2
    write3
    etc.

    I tried to find solution in countinginputstream and countingoutputstream,
    but readbytes(buffer(),0,3,0) keeps giving me compile error saying there is argument missing. Any ideas why?

    Rgds,
    Tapio
     
  17. agraham

    agraham Expert Licensed User

    Yes, something like that although, like I said, all the data for one message may not arrive at the same time so you will probably have to reassemble complete messages.

    You cannot use the streams from UsbSerial with CountingStreams, they are a stream subset meant to be used with AsyncStreams.

    I've no idea about ReadBbytes without seeing the code. The only ReadBytes that takes four parameters I can find is in RandomAccessFile and I have no idea why or how you might be want to use that.
     
  18. davelew1s

    davelew1s Active Member Licensed User

    Hi!
    I am going to convert a VB program to B4A which is for a computer controlled radio......before I start I want to be sure I can communicate with it.
    I've tried this sample an can connect and receive "H100" which means the radio is switched off, to turn it on I need to send "H101" & crlf ......that's where my problem is, i've tried all I can think of but nothing works....any help would be appreciated. Thanks Dave.
     
  19. agraham

    agraham Expert Licensed User

    What does the radio trigger on in CRLF. In basic4android CRLF is Chr(10) which is LF. Perhaps you need to send Chr(13) which is CR.
     
  20. davelew1s

    davelew1s Active Member Licensed User

    Hi agraham! the crlf signifies the end of the command after initializing the radio it is not required. My problem sending the line this is the original

    Sub btnSend_Click
    astreams.Write("abcdef".GetBytes("UTF8"))
    End Sub
    I tried this astreams.Write("H101" & crlf .GetBytes("UTF8"))
    and other combinations without any success. The only way I can be sure it has been sent is if the radio turns on.Is there anyway to monitor what is being sent, can this line ...astreams.Write("abcdef".GetBytes("UTF8"))... be sent to the log?
    Thanks Dave.....
     
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