B4J Question Bluetooth serial slow

vdudukov

Member
Licensed User
Longtime User
Hello.

I am trying to connect two sensors via bluetooth serial port. Data came every 400 ms when timer tick in form $data1#data2*. When I separate to text fields, program is slowing down, and i don`t know why?

Here is code, so if can somebody help. Thanks!

Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim btnOpen As Button
Dim cmbPort As ComboBox
Dim txtinput As TextField
Dim txtInput2 As TextField
Dim txtinput3 As TextField
Dim txtLog As TextArea
Dim lblStatus As Label
Dim lblTime As Label
Private sp As Serial
Private tmr As Timer
Private textreader1 As TextReader
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
MainForm.Title = "BLUETOOTH SERIAL"
MainForm.BackColor = fx.Colors.Blue
sp.Initialize("Serial1")
tmr.Initialize("Timer1", 400)
cmbPort.Items.AddAll(sp.ListPorts)
txtinput.Style = "-fx-font-size: 40;"
txtInput2.Style = "-fx-font-size: 30;"
txtinput3.Style = "-fx-font-size: 30;"

End Sub

Sub cmbPort_SelectedIndexChanged(Index As Int, Value As Object)
btnOpen.Enabled = Index > -1 'enable the button if there is a selected item
End Sub

Sub btnOpen_Action
If btnOpen.Text = "Connect" Then
sp.Open(cmbPort.Value)
textreader1.Initialize(sp.GetInputStream)
tmr.Enabled = True
txtinput.Enabled = True
btnOpen.Text = "Disconnect"
lblStatus.text = "Status: Connected"
Else
If btnOpen.Text = "Disconnect" Then
tmr.Enabled = False
sp.Close
btnOpen.Text = "Connect"
End If
End If

End Sub

Sub MainForm_Closed
sp.Close
End Sub
Sub Timer1_Tick
Dim n1 As Int,n2 As Int,n3 As Int, temp_str As String, S As String

temp_str=textreader1.ReadLine
txtinput3.text = temp_str

n1=temp_str.IndexOf("$")
n2=temp_str.IndexOf("#")
n3=temp_str.IndexOf("*")

S=temp_str.SubString2(n2+1,n3)
txtInput2.Text = temp_str.SubString2(n1+1,n2)
txtinput.Text = S


End Sub

btserial.png
 

vdudukov

Member
Licensed User
Longtime User
You should use AsyncStreams instead of this timer (check AsyncStreamsText class).

Thanks, I tried and found problem with buffer.

When using AsyncStreams, my text line is not complete.

In b4a this code works very good, but i have sleep function.
 
Upvote 0

vdudukov

Member
Licensed User
Longtime User
Could you explain? I had run your code and observed the same problem (getting slow after about 5 numbers).

Well, I use AsyncStreams to read my data, and data came in normal form for about maybe 10 -20 seconds $12345#1.5*. but then 345#1.5* or $1234 or something else.

I tried to put timer to 1000 ms, but then is to slow because, i need data faster because it is bluetooth scale, and that #1.5* is gram.
 
Upvote 0

vdudukov

Member
Licensed User
Longtime User
You do not need any timer. You should use AsyncStreams and you should understand that the messages can be split.

This is why I recommended you to use AsyncStreamsText class.

Sorry, now i saw that is class:confused:

Now I understand;)

Thank You.
 
Upvote 0

Similar Threads

Top