Using a Macro for Serial Port Communications
The EES Macro language provides the following macro commands to communicate with external devices through the serial port:
Serial.Open
Serial.ReadChar
Serial.ReadNumber
Serial.ReadString
Serial.ReadtoLookup
Serial.WriteChar
Serial.WriteNumber
Serial.WriteString
Serial.Close
Documentation for the use of these serial port commands is found in the Macro Command list.
Shown below is a very simple example that communicates with the Arduino Uno board.
The Arduino program is:
/*
myBlinker
Turns on an LED on or off by a signal sent through the serial port.
The Arduino acknowledges the action by writing a string to the serial port.
*/
char C;
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
delay(1000); // wait for a second
if (Serial.available() >0) {
C=Serial.read();
if (C=='H') {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("Bulb is on");
}
if (C=='L') {
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
Serial.println("Bulb is off");
}
}
}
The EES Macro program is:
//Test of communication with the Arduino UNO board.
//This macro simply causes the Arduino to alternately turn on an off the LED.
ict=0
Serial.Open Port=Com4 Baud=9600 Parity=n BufferSize=2048 TimeOut=1000
repeat
ict=ict+1
Serial.WriteChar(COM4,'H') //Note that the parentheses are optional
Serial.ReadString COM4 H$
pause 1
Serial.WriteChar COM4 'L'
Serial.ReadString COM4 H$
pause 1
until (ict>=5)
Serial.Close(COM4)
A listing of the EES Macro file after running this macro is:
Starting execution of MACROWINDOW at: 11/6/2015 4:10:25 PM
ict=0
Serial port COM4 has been opened.
ict=ict+1
H written to serial port H.
String 'Bulb is on' read from serial port COM4.
L written to serial port L.
String 'Bulb is off' read from serial port COM4.
ict=ict+1
H written to serial port H.
String 'Bulb is on' read from serial port COM4.
L written to serial port L.
String 'Bulb is off' read from serial port COM4.
ict=ict+1
H written to serial port H.
String 'Bulb is on' read from serial port COM4.
L written to serial port L.
String 'Bulb is off' read from serial port COM4.
ict=ict+1
H written to serial port H.
String 'Bulb is on' read from serial port COM4.
L written to serial port L.
String 'Bulb is off' read from serial port COM4.
ict=ict+1
H written to serial port H.
String 'Bulb is on' read from serial port COM4.
L written to serial port L.
String 'Bulb is off' read from serial port COM4.
Serial port COM4 has been closed.
Stopping at: 11/6/2015 4:10:45 PM