UPLOAD THIS SKETCH TO ARDUINO:
// Demonstrate 2-way communication between
// Mac [OSX 10.6.4] running REALStudio 2010r3 and Arduino
// Demo requires this sketch be uploaded to Arduino
// and program entitled "twoway" to be run on the Mac side
// Written by: Ken Jenkins - August 2010 [No rights reserved]
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication: Serial.begin(9600); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data: if (Serial.available() > 0) { // read the oldest byte in the serial buffer: incomingByte = Serial.read(); // if it's a capital H (ASCII 72), turn on the LED: if (incomingByte == 'H') { digitalWrite(ledPin, HIGH); } // if it's an L (ASCII 76) turn off the LED: if (incomingByte == 'L') { digitalWrite(ledPin, LOW); } } // You can play with this delay, but without it as the // program stands now you will get more sync errors on // the Mac side
delay(100);
Serial.print("@:AA:BB:CC:DD:EE:FF:");
}
THIS IS THE REALSTUDIO CODE FOR THE MAC SIDE
I CAN'T FIGURE OUT HOW TO UPLOAD THE "PROJECT" AS A WHOLE SO IT CAN BE LOADED UNDER REALStudio HERE AND I CAN'T FIGURE OUT HOW TO LOAD THE SCREEN LAYOUT EITHER. SORRY.
Class TwoWaySerial
Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Main Inherits Window
Main.Close:
Sub Close()
SerialCom.close
quit
End Sub
Main.Open:
Sub Open()
//Initialization //Set globals for record and field separators and //number of fields GB_recordseparator="@" GB_recordlength=20 GB_fieldseparator=":" GB_fieldcount=6
//Open a serial port If SerialCom.Open then //MsgBox "The serial port is open." Else MsgBox "The serial port could not be opened." quit End if
End Sub
Main Control SerialCom:
Sub DataAvailable()
do // Attempt to process some data dim count as Integer = GB.process_packet( me.LookAhead ) // If we have a full packet,then count will be >0 if count > 0 then // Remove the data from the RB internal buffer call me.Read( count ) else // We didn't process an entire packet, so we want to bail out exit end if loop until me.BytesAvailable <= 0
End Sub
End Class
Module GB
GB.process_packet:
Function process_packet(data as string) As integer
//Process Packet dim packet as string dim ldata as integer static sync_error_count as integer static good_count as integer dim hexstring as string dim fieldarray() as string dim i as integer
ldata=len(data)
//Wait until you have a complete packet if ldata >=GB_recordlength then packet=left(data,ldata) //Check that the record separator is the first byte in the packet and that the //packet is the expected length .... else reject it if left(packet,1)=GB_recordseparator and ldata=GB_recordlength then //This is where you do stuff with good packets **************************************** //Here's a easy way to split the fields into a string array fieldarray() = split(mid(packet,1),GB_fieldseparator) //Update display for i = 1 to GB_fieldcount Main.field(i).text = fieldarray(i) next i //Keep track of good packet count good_count=good_count+1 Main.GoodPackets.AppendText " [ "+str(ldata)+" ]"+packet+chr(13) Main.GoodCount.text=str(good_count) //Just to demonstrate that while the Arduino is talking to me I can talk to it as well //Toggle status of LED on every good packet //Send "H" or "L" back to Arduino to change staus on PIN 13 LED If GB_LED_status=TRUE then Main.LED.fillcolor=&cFF0000 //Update display RED Main.SerialCom.write ("H") GB_LED_status=FALSE else Main.LED.fillcolor=&c00FF00 //Update display GREEN Main.SerialCom.write ("L") GB_LED_status=TRUE end if else //This is where you reject packets which either do not begin with the record separator //or are not the expected length. //Keep track of sync errors sync_error_count=sync_error_count+1 Main.BadPackets.AppendText " [ "+str(ldata)+" ]"+packet+chr(13) hexstring=EncodeHex(packet,TRUE) Main.BadPackets.AppendText hexstring+chr(13) Main.SyncErrorCount.text=str(sync_error_count) end if return ldata else return 0
end if
End Function
GB_fieldcount As Integer
GB_fieldseparator As string
GB_LED_status As Boolean
GB_recordlength As Integer
GB_recordseparator As string
End Module