B4J Question wait for timeout

wle1036

New Member
Hi , i want set a timeout for wait for

example :Only 1000 ms wait then exit wait for and continue ....


B4X:
For Each port As Object In serial1.ListPorts
        Log(port)
        serial1.Open(port)
        serial1.SetParams("19200",8,1, 0)
        astream.Initialize(serial1.GetInputStream  , serial1.GetOutputStream, "astream")
         Wait For AStream_NewData (Buffer() As Byte)
         Dim result As String = TextAreaResive.Text & BytesToString(Buffer, 0, Buffer.Length, "utf8")
         Log(result)
        If result = "ok" Then Exit
         serial1.close
    Next

i want check online serial port and if request "ok" select this and config program
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is wrong as it assumes that all the data will arrive as a single packet.

Better solution:
B4X:
For Each port As Object In serial1.ListPorts
        Log(port)
        serial1.Open(port)
        serial1.SetParams("19200",8,1, 0)
        astream.Initialize(serial1.GetInputStream  , serial1.GetOutputStream, "astream")
         GlobalSB.Initialize 'global StringBuilder
         Sleep(1000)
         Dim result As String = GlobalSb.ToString
         Log(result)
        If result = "ok" Then Exit
         serial1.close
    Next
End Sub

Sub AStream_NewData (Buffer() As Byte)
 GlobalSb.Append(BytesToString(Buffer, 0, Buffer.Length, "utf8")) 'better to use BytesBuilder here.
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…