Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private sp As Serial
Private astream As AsyncStreams
Private SerialBuffer As String
Dim timer1 As Timer
Dim counter1 As Int = 0
Dim bytesSec As Int = 0
'Dim SerialBuffer As String ' replaced by B4XBytesBuilder
Dim bBuilder As B4XBytesBuilder
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
timer1.Initialize("timer1", 1000)
timer1.Enabled = True
SerialBuffer=""
sp.Initialize("sp")
Log(sp.ListPorts)
'SerialBuffer = ""
bBuilder.Initialize
bBuilder.Clear
'Dim searchFor() As Byte = Array As Byte(0xFA,0xFD)
'Log("0" & searchFor(0))
'Log("1" & searchFor(1))
'Return
If sp.ListPorts.Size > 0 Then
sp.Open(sp.ListPorts.Get(0))
sp.SetParams(9600, 8, 1, 0) ' baud: 9600, 115200, 500000
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
Else
Log("No serial port found")
End If
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Sub timer1_tick
bytesSec = counter1
counter1 = 0
Log("bytes/Sec: " & bytesSec)
End Sub
Sub AStream_NewData (Buffer() As Byte)
' Old way to avoid breaking the message (replaced by B4XBytesBuilder):
SerialBuffer = SerialBuffer & BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Lung. Buffer: " & SerialBuffer.Length & CRLF)
Log("Serial Buffer" & SerialBuffer)
Return
End Sub