The read section code snippet is written as “tight” as I can devise. As you can see, there are actually two reads. The Arduino sends the same “packet” twice to improve the reliability (yes, I’ve tried just one with a vast increase in packet loss). The packet itself consists of 5 bytes … the first is a “!” (ascii 33) and the last is a “*” (ascii 42) to frame the 3 data bytes in between. The “getData” command sets up the serial port after having acquired the actual port ID (variable PBPort) earlier in the script.
The call to “changeBar” within the “readData” command is to a small command call to update a do-it-yourself scrolling bar indicating the value being processed on the Arduino.
As I said, this works fine about 80% of the time but I need nearly 100% reliability and speed. I’ve tried many, many combinations of the timers and wait conditions and all of the other “read from driver” options available … this combination seems to provide the greatest overall success. I suspect I’ll need to go to an external code to improve the performance unless some kind soul from this forum can suggest an alternate approach of idea.
Yes, I know I could do a “full” ACK/NAK communication protocol but then I’d lose the desired processing speed on the Arduino side, which would then have to wait for an ACK or NAK and respond accordingly. I’m going to try that but I’d VERY much appreciate any feedback on this subject from the forum first.
And while you’re pondering that issue, also in the read loop, I want the read exited when I press the mouse button. HOWEVER, this ONLY works when I hold the mouse button down while a packet is arriving from the Arduino. That doesn’t make any sense to be but it is 100% consistent. So the question is: How do I interrupt this loop simply by pressing the mouse button? … whether or not a packet is coming in from the Arduino or not?
Code: Select all
on getData
put "BAUD=9600 PARITY=N DATA=8 STOP=1 to=off xon=off" into setSerial
set serialControlString to setSerial
open driver PBPort for binary update
put the result into y
readData
close driver PBPort
end getData
on readData
repeat until the mouse is down
put empty into xx
read from driver PBPort for 5 uInt1 in 10 milliseconds
put the result into z1
put it into y1
wait 5 milliseconds with messages
read from driver PBPort for 5 uInt1 in 10 milliseconds
put the result into z2
put it into y2
if (item 1 of y1 = 33) and (item 5 of y1 = 42) and (z1 is empty) then
put y1 into xx
else if (item 1 of y2 = 33) and (item 5 of y2 = 42) and (z2 is empty) then
put y2 into xx
end if
if xx is not empty then
changeBar ("bar" & (item 3 of xx)), ((item 2 of xx) * 4.54)
end if
end repeat
end readData