Android Question (solved)felUsbSerial-reading data

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi guys,

I'm trying to read data from a modules connected to my App by USB

using the library/example
https://www.b4x.com/android/forum/threads/felusbserial-alternative-usb-serial-library.62216/
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#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
    Private bc As ByteConverter
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 ScrollView1 As ScrollView
    Private EditText1 As EditText
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
   If FirstTime Then
     manager.Initialize
   End If
   Dim btnConnect As Button
   btnConnect.Initialize("btnConnect")
   btnConnect.Text = "Connect"
   'Activity.AddView(btnConnect, 10dip, 10dip, 100dip, 100dip)
End Sub

Sub btnConnect_Click
   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.Parity=usbserial.PARITY_NONE
        usbserial.StopBits=usbserial.STOP_BITS_1
       usbserial.StartReading
     End If
   End If
End Sub

Private Sub serial_DataAvailable (Buffer() As Byte)
   'Log("Buffer: " & Buffer)
    'ToastMessageShow( bc.StringFromBytes(Buffer,"UTF8"),False)
    Log(bc.StringFromBytes(Buffer,"UTF8"))
 
 'Log("New data: " & bc.HexFromBytes(Buffer))
'EditText1.Text=("data: "&bc.HexFromBytes(Buffer))  
   
End Sub

we're reading the data with missed parts:

0xCDC3647B,00077,00075,00079,15086,00081,00083,14973,00072 (correct)
0x8BC970DE,00077,00075,00079,15086,00081,00083,14973,00072 (correct)
0x1B46F30C,00096,00094,00098,15105,00100,00102,14992,00091 (correct)
0x14A78863,00098,00096,00100,15107,00102,00104,14994,00093 (correct)
0x0165DABA,00097,0009�� (wrong)
edit: should be:
0x0165DABA,00097,00095,00099,15106,00101,00103,14993,00092

"0x0165DABA,00097,0009��" isn't complete and after "��" we can't read the right data,
Does anyone understand what to do in this case?

thanks


the full data that we're reading is:
B4X:
Logger connected to:  BMXC K107
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
0xCDC3647B,00077,00075,00079,15086,00081,00083,14973,00072
0x8BC970DE,00077,00075,00079,15086,00081,00083,14973,00072
0x1B46F30C,00096,00094,00098,15105,00100,00102,14992,00091
0x14A78863,00098,00096,00100,15107,00102,00104,14994,00093
0x0165DABA,00097,0009��
0
x8BC970D
E,00077
,00075
,00079
,15086
,0008
1,0008
3,1497
3,0007
2
0
x1B46F30
C,00096
,00094
,00098
,15105
,00100
,00102
,1499
2,0009
1
0
x14A78863,
00098,0009
6,00100,1
5107,001
02,00104,
14994,000
93
0
x0165DABA,
00097,0009
5,00099,1
5106,0010
1,00103,
14993,000
92
0
x075BEF5B
,00087
,00085
,0008
9,1509
6,000
91,000
93,149
83,000
82
0
xCDC3647B,
00077,000
75,00079,15086,00081,00083,14973,000
72
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (serial_dataavailable)
Ignoring event (too many queued events: serial_dataavailable)
 
Last edited:

f0raster0

Well-Known Member
Licensed User
Longtime User
do you have a working example?
Edit: i moved to USB from BLE because I couldn't make it works using your examples.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Did you check the Examplecode in the Tutorial @Erel posted? Using it is simple.
yes, post #1
and using the new link, when added "felAsyncStreamsText "to the example code doesn't do anything.. probably because i don't know how to added "felAsyncStreamsText "to the example code.
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Added felAsyncStreamsText.bas to the project and trying to do the right mods to the original example

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#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
    Private bc As ByteConverter
    Private felAST As felAsyncStreamsText
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 ScrollView1 As ScrollView
    Private EditText1 As EditText
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
   If FirstTime Then
     manager.Initialize
   End If
   Dim btnConnect As Button
   btnConnect.Initialize("btnConnect")
   btnConnect.Text = "Connect"
   'Activity.AddView(btnConnect, 10dip, 10dip, 100dip, 100dip)
End Sub

Sub btnConnect_Click
   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)
            felAST.Initialize(Me, "felAST")
       usbserial.BaudRate = 115200
        usbserial.DataBits = usbserial.DATA_BITS_8
        usbserial.Parity=usbserial.PARITY_NONE
        usbserial.StopBits=usbserial.STOP_BITS_1
       usbserial.StartReading
     End If
   End If
End Sub

Private Sub serial_DataAvailable (Buffer() As Byte)
   'Log("Buffer: " & Buffer)
    'ToastMessageShow( bc.StringFromBytes(Buffer,"UTF8"),False)
    'Log(bc.StringFromBytes(Buffer,"UTF8"))
    felAST.astreams_NewData(Buffer)
 
 'Log("New data: " & bc.HexFromBytes(Buffer))
'EditText1.Text=("data: "&bc.HexFromBytes(Buffer))  
   
End Sub
Sub felAST_NewText (Text As String) '<--- will be raised when a new line character is found
    Log("Message received: " & Text)
End Sub
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Attached a complete working example :)
 

Attachments

  • USBSerial_working.zip
    389.1 KB · Views: 343
Last edited:
Upvote 0
Top