I have a function I'm trying to make in Swift (X-Code). I'm basically sending a few commands over bluetooth and I have a question about how the Main Asyncafter works. Here's my set-up code:
func tPodSetUp()
{
var delayForResponse = DispatchTime.now() + 0.4 //seconds to wait for response
writeValue(data: "231") //Put t-Pod in command mode, burst mode is OFF returns OK
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
}
writeValue(data: "202") //load calibration constants of probe, returns ok or 0
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("calibration Loaded")
}
if receivedString1 == "0"
{
print("No probe connected")
}
}
}
I want it basically to do the following (IN THIS ORDER):
writeValue
wait .4seconds
read response / check response
writeValue (AFTER IT FINISHED READING/CHECKING RESPONSE)
read response /check response
I am afraid that if the code is as it is right now it will just overrun the writeValues while waiting for the other ones and will have them on separate threads running asynchronously.
Also, what's confusing me is the fact that I declared delayForResponse at the beginning, and say will this change every time it's called? like if I do it at 12:00:00:00 it will make it now + .4 seconds (so its supposed to be called at 12:00:00:40, but then what will happen at 12:00:00:41 when it runs the second part (that has another callout to delayForResponse) will it suddenly say "what? this was supposed to be done already .01 seconds ago!?
EDIT HEre's another take on the code based on some feedback, will this do what I think it will?
let setupQueue = DispatchQueue(label: "setupQueue")
let delayForResponse = DispatchTime.now() + 0.4 //seconds to wait for response
setupQueue.sync {
writeValue(data: String(UnicodeScalar(UInt8(231)))) //231: Put t-Pod in command mode, burst mode is OFF returns OK
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
}
writeValue(data: String(UnicodeScalar(UInt8(202)))) //202: load calibration constants of probe, returns ok or 0
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
if receivedString1 == "0"
{
print("No probe connected")
}
}
writeValue(data: String(UnicodeScalar(UInt8(204)))) //204: load t-Pod serial number
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if (receivedString1.count > 5)
{
print("received t-Pod SN: \(receivedString1)")
tPodSN = receivedString1
}
}
writeValue(data: String(UnicodeScalar(UInt8(205)))) //205: load probe serial number
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if (receivedString1.count > 3)
{
print("received Probe SN: \(receivedString1)")
probeSN = receivedString1
}
}
//AFTER FINISHING SETUP, RESET TPOD AND TURN BEACON OFF
writeValue(data: String(UnicodeScalar(UInt8(202)))) //200: resets t-Pod
writeValue(data: String(UnicodeScalar(UInt8(202)))) //211: turns beacon off (temperature output)
}