Sub Process_Globals
Private usbserial As felUsbSerial
Private manager As UsbManager
Private bc As ByteConverter
End Sub
Sub Globals
Private txtMessage As EditText
Dim btnConnect As Button
Dim btnClose As Button
Dim btnSend As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Test_felUsbSerial")
If FirstTime Then
manager.Initialize
End If
' Dim btnConnect As Button
btnConnect.Initialize("btnConnect")
btnConnect.Text = "Connect"
Activity.AddView(btnConnect, 10dip, 10dip, 100dip, 40dip)
' Dim btnClose As Button
btnClose.Initialize("btnClose")
btnClose.Text = "Close"
Activity.AddView(btnClose, 120dip, 10dip, 100dip, 40dip)
' Dim btnSend As Button
btnSend.Initialize("btnSend")
btnSend.Text = "Send"
btnSend.Enabled = False
Activity.AddView(btnSend, 230dip, 10dip, 100dip, 40dip)
End Sub
Sub btnConnect_Click
If manager.GetDevices.Length = 0 Then
Log("No connected usb devices.")
btnSend.Enabled = False
Else
Dim device As UsbDevice = manager.GetDevices(0) 'assuming that there is exactly one device
If manager.HasPermission(device) = False Then
btnSend.Enabled = False
ToastMessageShow("Please allow connection and click again.", True)
manager.RequestPermission(device)
Else
usbserial.BUFFER_READ_SIZE = 16 * 1024 ' different settings make no difference
usbserial.Initialize("RxD", device, -1)
usbserial.BaudRate = 57600
usbserial.DataBits = usbserial.DATA_BITS_8
usbserial.StopBits = usbserial.STOP_BITS_1
usbserial.Parity = usbserial.PARITY_NONE
usbserial.FlowControl = usbserial.FLOW_CONTROL_OFF ' tested all settings here
usbserial.StartReading
btnSend.Enabled = True
End If
End If
End Sub
Sub btnClose_Click
If usbserial.IsInitialized Then
usbserial.Close
btnSend.Enabled = False
End If
End Sub
Sub btnSend_Click
If usbserial.IsInitialized Then
Dim s As String = "list" & Chr(13)
Dim msg() As Byte = s.GetBytes("UTF8")
txtMessage.Text = ""
usbserial.Write(msg)
End If
End Sub
Private Sub RxD_DataAvailable (Buffer() As Byte)
txtMessage.Text = txtMessage.Text & bc.StringFromBytes(Buffer, "UTF8")
' even if we don't add the new chars, but only show the last received ones, still there are lost chars,
' so seems like processing time of the txtMessage rendering doesn't seem to be the problem either
End Sub