|
Intro to Physical Computing Syllabus code, circuits, & construction
|
Multi-byte serial exchanges: a Call-and-Response method(Lingo) |
|||
| Here's an example where a desktop computer running Director receives data from a BX-24. In this case, the BX-24 is sending 3 bytes, one for each of 3 analog sensors. the Director program checks to see how many bytes there are in the serial port buffer. If there are none, it asks for data. If there are five, it reads the data into five variables. It runs this checking routine repeatedly (in the exitframe or idle), so that if there are some bytes, but not a full three, it skips the whole routine and goes about its other business. This example uses Geoff Smith's SerialXtra version 1.0 for Director. The full version of the Director code can be downloaded as a Director .dir file here, or as a text file (the main movie script) here. Lingo side: on exitFrame
doSerial
go the frame
end
on doSerial
-- see if the port's been opened:
If serialObject.isPortOpen() then
If serialObject.charsavailable() = 0 then
--send a byte to ask for data
SerialObject.writeChar("A")
End if
If SerialObject.charsAvailable() >=3 then
--first byte sent by microcontroller:
MyVar = SerialObject.readNumber()
--next byte sent by microcontroller:
MyVar2 = SerialObject.readNumber()
--third one sent by microcontroller:
MyVar3 = SerialObject.readNumber()
end if
end if
end
The microprocessor is reading its sensors, then listening for a byte of data from the Director program. If it gets a byte, it sends out its five sensor readings. If it doesn't, it goes about its other business. Here are examples for PicBasic Pro and the BX-24. These methods won't work in every situation, and they will need to be modified to fit most situations, but they are a starting point. |
||||