jaydrive2000
Number of posts : 83 Registration date : 2012-09-05
| Subject: Example Serial Decoder for PCLC & Arduino 7/5/2014, 12:53 | |
| Hi all. I finally got sat down and started working with PCLC and Serial Commands. I figured I'd share the starting bit which is getting PCLC and Arduino to talk, so have a look at my example code below. It may be a little wonky, so if you've corrections to advise, please share so we can all benefit. =) PLEASE NOTE - This example decoder does NOT handle the newer commands in V1.03 of the Arduino Protocol supported by PCLC. First, get your arduino to talk to the port by activating the serial library... you can see an example below... - Code:
-
void setup() { Serial.begin(9600);
//your stuff here }
void loop() { //your stuff here }
Now, We're going to take advantage of some automated functionality here. After "Setup" and "Loop" you can use "serialEvent" to run a series of code automatically to read off anything coming in from the Serial Port (IE, whatever PCLC is trying to tell the Arduino.... - Code:
-
void serialEvent() // By using this function, Serial decoding happens between each run of "loop", and makes it an a more automatic feature. { String Message = ""; while (Serial.available()) { //While there's stuff coming in on the Serial Port.... char SerialChar = (char)Serial.read(); //Read the current character and store it to memory Message += SerialChar; //Now add that character to the Message we're building } PCLCDecode(Message); // Now that all the message is in, pass it on to the next function to decode it }
and finally, now that we have the message stored, it's time to decode it to determine what the Arduino will do... - Code:
-
void PCLCDecode(String Message) // This function decodes a serial string sent by PC Lap Counter { CommandLetter = ""; // We'll store the command letters here LaneNumber = 0; // The lane number being affected here Buffer = ""; // This is for any data manipulation State = false; // and this acts as our onoff switch to do with the commands. CommandLetter = String(Message[1] + Message[2]); // Look for characters 3 & 4, change to integer, assign to LaneNumber Buffer = String(Message[3] + Message[4]); // Let's pull the characters that make the lane number up, and transfer them to the buffer... LaneNumber = Buffer.toInt(); //We then change that to an Integer which makes it easier to work with in Arduino Code =)
if (Message[5] == "1") // Look for character 5, change it to a boolean, which is Arduino's equivalent of an onoff switch, and make "State" reflect that { State = true } else { State = false } // All Parameters have been interpreted - Now to decide functionality FunctionCall(CommandLetter,LaneNumber,State); // We'll then pass on all these bits to a bit of code I'll share shortly which then decides how to handle that data. }
Last edited by jaydrive2000 on 7/5/2014, 13:05; edited 2 times in total | |
|
jaydrive2000
Number of posts : 83 Registration date : 2012-09-05
| Subject: Next bit 7/5/2014, 12:56 | |
| alright, here's the next bit - deciding how to handle the code. Since each command letter represents something different, we'll have a different function for each of them. what this does is take a look at the command letters (For example "SL" or "PW") and then calls up the function that handles their commands. - Code:
-
void FunctionCall(String CommandLetter, int LaneNumber, boolean State) { if (CommandLetter == "SL") { SetSL(LaneNumber,State); } if (CommandLetter == "PW") { SetPW(LaneNumber,State); } if (CommandLetter == "FS") { SetFS(LaneNumber,State); } if (CommandLetter == "SG") { SetSG(LaneNumber,State); } if (CommandLetter == "FP") { SetFP(LaneNumber,State); } if (CommandLetter == "OF") { SetOF(LaneNumber,State); } if (CommandLetter == "LF") { SetLF(LaneNumber,State); } if (CommandLetter == "MF") { SetMF(LaneNumber,State); } if (CommandLetter == "PT") { SetPT(LaneNumber,State); } } | |
|