App.sleepCurrentThread(mS) inhibits Serial port

my app sends commands over serial to an Arduino
that bit works

I want to receive data back from the Arduino
I can do that too, and display it in a nice text box

what I want to do is pause until the data arrives
I tried using App.sleepCurrentThread(mS) but that seems to stop serial data from arriving

if I put in a MsgBox(“timeout…”), the data arrives nicely
so what does MsgBox do while it’s waiting for me to click ok?
I want to do the same!

in a nutshell
how can I pause/sleep/wait/rest/… AND let the serial port do its thing?

Are you using a thread or are you calling the SleepCurrentThread on the main thread loop? If you’re not using a thread, you’re pausing the whole app with that call.

ah
I’m not using a thread

not sure how to set that up
any pointers, please?

Xojo is event-driven - why not simply let the application do thumb twiddling and wake up on events like DataAvailable or Timer actions?

I have a network of Arduinos measuring and controlling an industrial humidifier. I have several serialport objects with DataAvailable handlers receiving the data. Polling is done with Timers that sends out the correct command to the correct serialport and when data is received, the DataAvailable event makes sure we have got a complete packet (terminated with linefeed).

As the Arduinos are slow to respond I use a Semaphore to make sure I don’t send a new command before I receive an answer (or timeout) to the previous command.

fair enough
but I want to send a command and analyse the result
each command gets a different set of results
and I don’t need to do anything else until I get the data back

I could put lots of switches in dataAvailable, but that feels messy

What I did in my arduino network was that the Arduino replies start with the command I sent so that the DataAvailable event contains a switch statement based on this and simply passes the rest of the string to the correct parsing method and also passing the reference to the serialport receiving the message so the parser can route information to the correct place in the app.

Code from my DataAvaialble events:

[code] if right(Me.LookAhead, 1) = chr(10) then
edtSerialData.Text = “DataAvailable:” + EndOfLine + Me.LookAhead
dim s as string = trim(Me.ReadAll)

tmrTimeout.Mode = 0

select case mid(s, 1, 2)
case "?R"
  parse_read_reply(me.SerialPort, s)
case "!W"
  parse_write_reply(me.SerialPort, s)
case "?P"
  parse_pidparam_reply(me.SerialPort, s)
case "?S"
  parse_pump_up_status(me.SerialPort, s)
case "--"
  parse_error_message(me.SerialPort, s)
case "++"
  parse_ok_message(me.SerialPort, s)
  
end select

end if[/code]

Based on your protocol this may or may not be applicable in your situation. I use two-character combinations with optional arguments terminated by a line break and these two characters don’t add much overhead to the reply but makes it easy for me to parse the reply.

hmm
neat trick
might give that a try :slight_smile:
thanks