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.
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 three sensor readings. If it doesn’t, it goes about its other business. Examples for PicBasic Pro, BX-24, and Arduino/Wiring can be found at this link.
Here’s the full Lingo code, the text for the main movie script:
-- simpleserial.dir
-- by Tom Igoe, 2000
-- based on Geoff Smith's SerialXtra
global serialObject
global myVar1, myVar2, myVar3
on startMovie
clearglobals
-- variables for setting up the xtra.
-- Fill in your own values from the email
-- that you get from the licence registrar:
-- fill in your email address below (see physicalbits.com for details)
programmerName = "youremail@some.com"
serialnum = "xxxx-xxxx-xxxx-xxxx" -- physicalbits.com will mail you this number
licenseLength = 90
-- fill in the name of your serial port below (e.g. COM1 on Windows):
mySerialPort = "/dev/cu.USA19QW191P1.1"
-- make a new instance of the xtra (mac OSX)
openxlib the pathname & "serialXtra.osx"
-- use this line instead for Windows:
-- openxlib the pathname & "serialXtra.xtr"
serialObject = new (xtra "SerialXtra", programmerName, serialnum, licenseLength )
-- check that it has been created correctly
if objectP( serialObject ) then
put serialObject
else
alert("Instance not valid")
end if
-- open the serial port:
serialObject.openPort(mySerialPort)
-- set the data rate, start bits, etc:
serialObject.setProtocol(9600, "n", 8, 1)
end
on stopMovie
-- dispose of the xtra and close the xtra file
serialObject.closePort()
set serialObject to 0
closeXlib
end
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
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.