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
Erel, you wrote that you had decreased the interval to 10ms (#18). Could you decrease it to 1ms or 100 microsec. or to delete this interval at all? If no, could you explain how i can do it?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Could you just change this parameter for 1 ms (10ms)? I will be very indebted to understand how to do this (
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I change the interval by 1 or 10, but nothing changes. (sp.ReadingThreadInterval=1) What else could it be?
I draw a signal graph. When the signal is up to 50 Hz, the graph is drawn normally. If more, you can already see that there is not enough speed.
Here are the com port settings:
B4X:
    sp.Open(cmbPort.Value)
    sp.SetParams (115200,8,1,0)
    sp.ReadingThreadInterval=1
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
When I press a button I send a command (8 bytes):
B4X:
Sub btnStart_Action
    If cmbPort.Enabled Then
        fx.Msgbox(MainForm,"Сначало нужно выбрать COM порт","Ошибка:")
    Else
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
    End If
End Sub
Sub CRC8 (b0 As Int,b1 As Int,b2 As Int,b3 As Int,b4 As Int,b5 As Int,b6 As Int) As Int
    'Log(ForCRC(Bit.Xor(b6,ForCRC(Bit.Xor(b5,ForCRC(Bit.Xor(b4,ForCRC(Bit.Xor(b3,ForCRC(Bit.Xor(b2,ForCRC(Bit.Xor(b1,ForCRC(Bit.Xor(0,b0)))))))))))))))
    Return ForCRC(Bit.Xor(b6,ForCRC(Bit.Xor(b5,ForCRC(Bit.Xor(b4,ForCRC(Bit.Xor(b3,ForCRC(Bit.Xor(b2,ForCRC(Bit.Xor(b1,ForCRC(Bit.Xor(0,b0))))))))))))))
End Sub
Then I wait for an answer (8 bytes). I select the high and low byte:
B4X:
Sub AStream_NewData (Buffer() As Byte)

    For ii = 0 To Buffer.Length-1
        New123(j)=Buffer(ii)
        j=j+1
    If j = 8 Then
            'Dim n As Long = DateTime.Now
        j=0
        If New123(5)<0 Then
            Sbyte=Bit.And(New123(5),127)
            Sbyte=Bit.Or(Sbyte,128)
        Else
            Sbyte=New123(5)
        End If
        If New123(6)<0 Then
            Mbyte=Bit.And(New123(6),127)
            Mbyte=Bit.Or(Mbyte,128)
        Else
            Mbyte=New123(6)
        End If
       
        Sig=Sbyte
        Sig=Bit.ShiftLeft(Sig,8)
        Sig=Bit.Or(Sig,Mbyte)

        Sig=640-(Sig/102.4)
        If Sig>Por Then
            Col1=0
            Col2=0
            Col3=255
        Else
            Col1=255
            Col2=0
            Col3=0
        End If
        Graf
            'Log(DateTime.Now - n) 'in milliseconds
    End If
    Next
End Sub
And I display on the graph:
B4X:
Sub Graf
    'temp=1
    If x=0 Then
        x=1
        Canvas1.DrawLine(0,0,1,Sig,fx.Colors.Blue,1)
        OldSig=Sig
        Move
    Else
        Canvas1.DrawLine(t,OldSig,t+1,Sig,fx.Colors.RGB(Col1,Col2,Col3),2)
        OldSig=Sig
        t=t+temp
        Move
    End If
End Sub
As soon as the chart is filled, I erase it and draw it again and so in an infinite loop.
B4X:
Private Sub Move
    If t>=720 Then
        x=0
        t=1
        Canvas1.ClearRect(0, 0, 720, 640)
    End If
    If SS=0 Then
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
    Else
        SS=0
    End If
End Sub
The time of drawing the complete graph is 11,7s.
First I think that it hinders the drawing of the schedule.
Then I tried to fill it with random numbers by timer.
Everything turns out much faster 0.7s.
B4X:
Timer1.Initialize("Timer1", 1)
Timer1.Enabled=True

Sub Timer1_Tick
    Sig=Rnd(16384,49170)
    Sig=640-(Sig/102.4)
    If Sig>Por Then
        Col1=0
        Col2=0
        Col3=255
    Else
        Col1=255
        Col2=0
        Col3=0
    End If
    Graf
End Sub
This means that there is a delay in the part of the exchange.
Tell me please what could be the reason? How can I fix it?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
The signal is 5 Hz. The rendering is normal.
The signal is 60 Hz.
On an oscilloscope, I see that between a signal request and a response of 250 micro seconds. That is, the controller is very fast!
In order to properly select a signal in up to 100Hz, it is necessary that the time between sending is at least the time between the request the response is equal to 5 miles seconds.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Erel, yes, I set 0, nothing changed, you are right. I test in release mode.
Could you give your ideas, how to make data exchange faster?
 
Upvote 0

Toley

Active Member
Licensed User
Longtime User
Hi red30,

I follow your post and I'm not sure on what you base yourself to say that "transfer is slow". Do you use any kind of flow control? It can be RTS/CTS pins or simply you ask the MCU to send you another byte (or packet) every time when your app is ready. If you don't use any kind of flow control then 115200 is 115200 there is nothing that can slow it down. Maybe you could try at 230400 (most MCU are able to reach that speed) or event more. It's not possible for the uart receiver to slow down baudrate without flow control. If the rx is too slow then you miss packets. Your problem is most likely in your code. I would suspect your crc routine...
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
The program works as a "request-response" type. I send the parcel (8 bytes)
B4X:
Sub btnStart_Action
    If cmbPort.Enabled Then
        fx.Msgbox(MainForm,"Сначало нужно выбрать COM порт","Ошибка:")
    Else
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
    End If
End Sub
and wait for my reply (8 bytes).
B4X:
Sub AStream_NewData (Buffer() As Byte)

    For ii = 0 To Buffer.Length-1
        New123(j)=Buffer(ii)
        j=j+1
    If j = 8 Then
            'Dim n As Long = DateTime.Now
        j=0
        If New123(5)<0 Then
            Sbyte=Bit.And(New123(5),127)
            Sbyte=Bit.Or(Sbyte,128)
        Else
            Sbyte=New123(5)
        End If
        If New123(6)<0 Then
            Mbyte=Bit.And(New123(6),127)
            Mbyte=Bit.Or(Mbyte,128)
        Else
            Mbyte=New123(6)
        End If
    
        Sig=Sbyte
        Sig=Bit.ShiftLeft(Sig,8)
        Sig=Bit.Or(Sig,Mbyte)

        Sig=640-(Sig/102.4)
        If Sig>Por Then
            Col1=0
            Col2=0
            Col3=255
        Else
            Col1=255
            Col2=0
            Col3=0
        End If
        Graf
            'Log(DateTime.Now - n) 'in milliseconds
        End If
    Next
End Sub
From them I take 2 bytes and display them on the graph
B4X:
Sub Graf
    If x=0 Then
        x=1
        Canvas1.DrawLine(0,0,1,Sig,fx.Colors.Blue,1)
        OldSig=Sig
        Move
    Else
        Canvas1.DrawLine(t,OldSig,t+1,Sig,fx.Colors.RGB(Col1,Col2,Col3),2)
        OldSig=Sig
        t=t+temp
        Move
    End If
End Sub

Private Sub Move
    If t>=720 Then
        x=0
        t=1
        Canvas1.ClearRect(0, 0, 720, 640)
    End If
    If SS=0 Then
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
    Else
        SS=0
    End If
End Sub
After which I send another parcel to the microcontroller.
And so in an endless cycle.
I was looking at the oscilloscope the time between which I arrive at my send and the answer of the microcontroller - it is equal to 250ns.
That is, a lot of time is spent after sending a message from the microcontroller and when I get to:
B4X:
Sub AStream_NewData (Buffer() As Byte)
 
Last edited:
Upvote 0

Toley

Active Member
Licensed User
Longtime User
I cannot test/debug your soft, I don't have the hardware neither the time. But I can tell you I have made some progs with this library and never observed this kind of delay.

What I can suggest you is to get from the start and write a simple app that only send the same length string and receive it on the PC with the same setup. Then you will see if you have the same kind of delay and if it's the library or not.

The only way to track this kind of bugs is to make a minimal code to reproduce it.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I understood what's the matter !!! I tried changing the value ReadingThreadInterval. The transmission rate from 115200 to 25600
Everything works the same way. The time is about 10-15ms.
But when I send a parcel to the microcontroller, in response, it sends me data continuously, everything works fine.
That is, the case in the shipment, it takes too long in time!
Is there any way to increase the speed of sending a message "astream.Write"?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I can not use arduino ...
I realized that there is no delay. I do not understand where this pause is in 10ms. I was just trying to send data on the timer in 1ms, everything goes fast. In this case, if I send data from the microcontroller with a constant stream, everything happens, they also come quickly ... What else can it be?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
1. The microcontroller continuously sends data. Between packages ~ 2ms.
2. I continuously send data to the microcontroller. Between packets ~ 1ms.
B4X:
Private kan=1 As Int
Timer1.Initialize("Timer1", 1)
Timer1.Enabled=True
Sub Timer1_Tick
    astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x01,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
End Sub
Sub CRC8 (b0 As Int,b1 As Int,b2 As Int,b3 As Int,b4 As Int,b5 As Int,b6 As Int) As Int
    Return ForCRC(Bit.Xor(b6,ForCRC(Bit.Xor(b5,ForCRC(Bit.Xor(b4,ForCRC(Bit.Xor(b3,ForCRC(Bit.Xor(b2,ForCRC(Bit.Xor(b1,ForCRC(Bit.Xor(0,b0))))))))))))))
End Sub
3. There is a continuous exchange. Between packages ~ 14ms. Between the incoming parcel and the answer of the microcontroller ~ 250ns.
B4X:
Sub btnStart_Action
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
End Sub
Sub AStream_NewData (Buffer() As Byte)
For ii = 0 To Buffer.Length-1
    New123(j)=Buffer(ii)
    j=j+1
    If j = 8 Then
        j=0
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
    End If
Next
End Sub
Why is this happening?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Erel, the application is non-UI.
This is the whole programme.
B4X:
Sub btnStart_Action
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
End Sub
Sub AStream_NewData (Buffer() As Byte)
For ii = 0 To Buffer.Length-1
    New123(j)=Buffer(ii)
    j=j+1
    If j = 8 Then
        j=0
        astream.Write(Array As Byte(0x00,0x07,0x52,0x00,kan,0x00,0x00,CRC8(0x00,0x07,0x52,0x00,kan,0x00,0x00)))
    End If
Next
End Sub
 
Upvote 0
Top