Hello everybody!
After using felUSBSerial for some while, I realized that once a while some bytes are lost
in the input stream. The procedure is as follows:
Clicking the button "SEND" a short message is transmitted over an FTDI-cable to an external
device. The device then answers with a byte message (simple ASCII chars) of a few hundred bytes.
While receiving the continuous input stream (all bytes are chained together, i.e., after each stop bit the
next start bit starts immediately) always some bytes are lost a random positions.
Here is the code:
Now the whole communication works properly using a standard terminal program on the same mobile
device, i.e., many things can be ruled out: the missing bytes are not due to baud rate mismatch,
half duplex switching, driver problems or the like.
Any ideas?
Best,
Fred
After using felUSBSerial for some while, I realized that once a while some bytes are lost
in the input stream. The procedure is as follows:
Clicking the button "SEND" a short message is transmitted over an FTDI-cable to an external
device. The device then answers with a byte message (simple ASCII chars) of a few hundred bytes.
While receiving the continuous input stream (all bytes are chained together, i.e., after each stop bit the
next start bit starts immediately) always some bytes are lost a random positions.
Here is the code:
B4X:
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
Now the whole communication works properly using a standard terminal program on the same mobile
device, i.e., many things can be ruled out: the missing bytes are not due to baud rate mismatch,
half duplex switching, driver problems or the like.
Any ideas?
Best,
Fred