Hi
I am using a random access AStreams to talk to an embedded processor (EP) with WiFI. In summary code I do the following:
The idea is that I use the global variable cmdAckglbl as a semaphore set by the AStreams_NewData sub, to signal that data from the EP is ready using a,
Do While cmdAckglbl = False
Sleep(2)
Loop
This works fine within the Sub Sock1_Connected sub but does not work in the subroutine btnRefresh_Click. In the btnRefresh_Click sub. The do while loops do not detect the cmdAckglbl becoming True and so never move forward to the next statements.
I have tried 0 ms in the sleep statement, but then it just seems to fall straight through with no waiting for the semaphore to become true.
I also tried a "Wait For" event statement, using cmdAckglbl as the event, but this didn't work either.
I read up on events, but could not work out how to generate a suitable event from the AStreams_NewData sub.
I have uploaded the whole project in a zip file.
I am used to embedded C programming using deferred interrupts and all those wonderful things so I struggle a bit with higher level languages running on general purpose operating systems.
Please Help
I am using a random access AStreams to talk to an embedded processor (EP) with WiFI. In summary code I do the following:
B4X:
======================================================
Dim cmdAckglbl As Boolean
======================================================
Sub Activity_Create(FirstTime As Boolean)
Sock1.Initialize("Sock1")
Sock1.Connect("20.0.0.1", 8899, 5000)
End Sub
======================================================
Sub Sock1_Connected(Successful As Boolean)
AStreams.InitializePrefix(Sock1.InputStream, False, Sock1.OutputStream, "AStreams")
cmdNoglbl = 0 'Set Date
sendCommand
Do While cmdAckglbl = False
Sleep(20)
Loop
cmdNoglbl = 14 'Get Serial No
sendCommand
Do While cmdAckglbl = False
Sleep(20)
Loop
.... Other commands here
voltageGraph(False)
End Sub
======================================================
Sub sendCommand()
cmdAckglbl = False
Select cmdNoglbl
Case 0 ' Set date
strBuff = DateTime.Date(DateTime.now)
strBuff = "$SetDate," & strBuff & Chr(13) & Chr(10)
buffer = strBuff.GetBytes("UTF8")
AStreams.Write(buffer)
Case 14 ' get Serial Number
strBuff = "$GetSerNo,"
buffer = strBuff.GetBytes("UTF8")
AStreams.Write(buffer)
Case 16 ' get Waveforms
strBuff = "$GetWaveForms,"
buffer = strBuff.GetBytes("UTF8")
AStreams.Write(buffer)
Case 17 ' get Last Log Entry
strBuff = "$GetLastLog,"
buffer = strBuff.GetBytes("UTF8")
AStreams.Write(buffer)
End Select
End Sub
======================================================
Sub AStreams_NewData (buffer() As Byte)
Dim i As Int
Dim k As Int
Dim testFloat As Float
Dim strBuff As String
Select cmdNoglbl
'------------------------------------------------------------------------------
Case 0 ' Set date
msg = BytesToString(buffer, 0, buffer.Length, "UTF8")
lblStatus.Text = lblStatus.Text & " - Time Set: " & msg.SubString(9)
cmdAckglbl = True
'------------------------------------------------------------------------------
Case 14 ' Get Serial Number
msg = BytesToString(buffer, 0, buffer.Length, "UTF8")
msg = msg.SubString(10)
msg = " Fence Ser No: " & msg
lblTitle.Text = msg
cmdAckglbl = True
'------------------------------------------------------------------------------
Case 16 ' Get WaveForms
Conv.LittleEndian = True
'Convert block of bytes from buffer to floats
fieldFloatBlock = Conv.FloatsFromBytes(buffer)
'Append this block to the data set
For i = 0 To blockSize - 1
k = blockSize * blockNum + i
waveFloat(k) = fieldFloatBlock(i)
Next
If blockNum < 7 Then
'Send the acknowledge'
strBuff = "ack"
buffer = strBuff.GetBytes("UTF8")
AStreams.Write(buffer)
blockNum = blockNum + 1
blockNum = blockNum
Else
cmdAckglbl = True
End If
'------------------------------------------------------------------------------
Case 17 ' Get Last Log Entry
msg = BytesToString(buffer, 0, buffer.Length, "UTF8")
cmdAckglbl = True
End Select
End Sub
======================================================
Sub btnRefresh_Click
msg = ""
cmdNoglbl = 16 'Get Last Waveform
sendCommand
Do While cmdAckglbl = False
Sleep(2)
Loop
msg = ""
cmdNoglbl = 17 'Get Last Log
sendCommand
Do While cmdAckglbl = False
Sleep(2)
Loop
msg = ""
'edtDebug.Text = ""
cmdNoglbl = 16 'Get Last Waveform
sendCommand
Do While cmdAckglbl = False
Sleep(2)
Loop
voltageGraph(tglBtnZoom.Checked)
End Sub
The idea is that I use the global variable cmdAckglbl as a semaphore set by the AStreams_NewData sub, to signal that data from the EP is ready using a,
Do While cmdAckglbl = False
Sleep(2)
Loop
This works fine within the Sub Sock1_Connected sub but does not work in the subroutine btnRefresh_Click. In the btnRefresh_Click sub. The do while loops do not detect the cmdAckglbl becoming True and so never move forward to the next statements.
I have tried 0 ms in the sleep statement, but then it just seems to fall straight through with no waiting for the semaphore to become true.
I also tried a "Wait For" event statement, using cmdAckglbl as the event, but this didn't work either.
I read up on events, but could not work out how to generate a suitable event from the AStreams_NewData sub.
I have uploaded the whole project in a zip file.
I am used to embedded C programming using deferred interrupts and all those wonderful things so I struggle a bit with higher level languages running on general purpose operating systems.
Please Help
Attachments
Last edited: