B4J Question jSerial library - Exchange is very slow...

red30

Well-Known Member
Licensed User
Longtime User
I connect microcontroller (USB CDC) to the android device (felUsbSerial library and USB) works fine. Rewrote the program on Windows (jSerial library), all the same only the exchange is very slow ... why?
 

red30

Well-Known Member
Licensed User
Longtime User
My programme works in endless loop request-response. USB of my microcontroller works as virtual COM port, so baudrate is permanent. I tried different speeds: sp.SetParams(9600,8,1,0) and sp.SetParams(115200,8,1,0), exchange doesnt work faster. AsyncStreams works without prefix.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Here's an example of my program for Android devices (data exchange is very fast):
B4X:
Sub btnOpen_Click
    Dim UsbDevices() As UsbDevice
    UsbDevices = manager.GetDevices
    If UsbDevices.Length = 0 Then
        ToastMessageShow("No device", True)          
    Else
        For z = 0 To UsbDevices.Length - 1
            Dim UsbDvc As UsbDevice
            UsbDvc = UsbDevices(z)
            If (UsbDvc.ProductId = s_id) And (UsbDvc.VendorId = s_vd) Then              
                 If manager.HasPermission(UsbDvc) = False Then
                       manager.RequestPermission(UsbDvc)
                 Else
                       usb2.Initialize2 ( "Fel" , UsbDvc, - 1 ,  "CDCSerialDevice" )
                       usb2.BaudRate = 9600
                       usb2.DataBits = usb.DATA_BITS_8
                       usb2.StartReading
                       usb2.Write(Array As Byte(addr,0x05,ForCRC(Bit.Xor(0x05,ForCRC(Bit.Xor(0,addr))))))
                 End If
            End If
        Next
    End If
End Sub
But under the Window, I connect to the same device (the exchange is slow):
B4X:
Sub btnOpen_Action
    sp.Open(cmbPort.Value)
    sp.SetParams (9600,8,1,0)
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    'astream.InitializePrefix(sp.GetInputStream, True, sp.GetOutputStream, "astream")
End Sub
Why?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I see, but where is the problem and how can i fix it to increase the speed of exchange?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Erel I tried to update, deleted and re-installed the driver. Everything is still, - the exchange is slow ...
Tried on different computers and different operating systems ...
Tried to use FT232RL , running at a speed of 38400 baud, instead of the standard USB CDC microcontroller, still slow ...
Any ideas please. If you want to have more checks - i will make.
I can make a video how the programm works on android and windows.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Erel, and can be introduced special software delays in the library? Not as I can not understand why this program works? It can be faster ..
Tablet:
PC:
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Thank you very much!!!!! With this library works just fine! And what have you changed? What was the reason?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
The reading is done with a background thread that polls the buffer for new data. I've decreased the thread waiting interval.
You have reduced the waiting interval in the YYY Library. And can you make it as small as possible? I will be very grateful...
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
On the USB comes every 125 mls of a packet of 1024 bytes. When the next premise arrives in 125 milliseconds, the library does not have time.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
The interval is set to 10ms. It should be fast enough. Are you testing it in release mode?
Yes!
Here is the setting:
B4X:
    sp.Open(cmbPort.Value)
    sp.SetParams (115200,8,1,0)
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
Then, when 1024 bytes arrive, I start processing:
B4X:
Sub AStream_NewData (Buffer() As Byte)
    If Buffer.Length=1024 Then
       ...
    End If
End Sub
I thought that processing goes on for a long time, then I timed (125ms) did the same thing only populated the array with random numbers:
B4X:
Sub Mytest
        For temp=0 To 1023
            DataTest(temp)=Rnd(0,512)
        Next
        ...
End Sub
It worked fine!
Where can there be such a delay?
And I noticed that time somehow floats ...
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I understand. The parcel did not always arrive 1024 bytes, and sometimes parts! I just collected it in one and worked it out!
Thank you, Erel!
 
Upvote 0
Top