The AT command structure has endured. Originally a modem control language developed by Hayes, it finds itself controlling lots of serial devices still - in my experience most commonly GPSs and Bluetooth modules.
Each time I've needed to write code to send successive AT strings and receive the responses I've never been comfortable with the result - although, of course, it worked. This time I am sending Bluetooth data from an archer's bow, as it happens, and needed to change several modes and the data rate of a common module.
Sending the AT strings is easy. I just built a list as StringVectorData and looped through it. Handling the intervening responses, though, has always been awkward and this time wasn't an exception.
This must be a common task. I'm wondering how others have treated the standard <CR><LF>OK<CR><LF> and <CR><LF>ERROR<CR><LF> strings (and incomplete fragments like <CR><LF>) and reduced them to a boolean that fires the next string or terminates the process.
Currently, here's mine. The only True result is from a six-byte <CR><LF>OK<CR><LF>; anything else yields False after hearing an <LF> - except the first legitimate <LF> in byte two - as does a timeout.
Code: Select all
function GetOK() as boolean ' look for <CR><LF>OK<CR><LF>
dim bComIn as byte, tX as single, t0 as single, charcount as byte = 0
GetOK = True
t0 = Timer
Do
Do
tX = Timer - t0
if tX < 0.0 then tX = tX + 86400.0
if tX > 2.0 then GetOK = False : exit function ' timeout
Loop While GetQueueCount(InQueue_7) = 0
Call GetQueue(InQueue_7, bComIn, 1)
Select Case bComIn
case asc("O")
charcount = charcount + 1
case asc("K")
charcount = charcount + 1
case 10
charcount = charcount + 1
if charcount = 2 then ' allow first LF to pass
else
if charcount <> 6 then GetOK = False ' second LF must be sixth character
exit function
end if
case 13
charcount = charcount + 1
case else
GetOK = False
end select
Loop
end function
Has anyone done one?