Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WString.h>
- #define MSG_METHOD_SUCCESS 0 //Code which is used when an operation terminated successfully
- #define WRG_NO_SERIAL_DATA_AVAILABLE 250 //Code indicates that no new data is AVAILABLE at the serial input buffer
- #define ERR_SERIAL_IN_COMMAND_NOT_TERMINATED -1 //Code is used when a serial input commands' last char is not a '#'
- const String StartTX = "\x02";
- const String ResumeTX = "$";
- const String EndTX = "\x03";
- String self = "1A"; // Just a sort of mode select, or listener group perhaps.
- char charBuf[12] = {0};
- char message1FromPC[4];
- char message2FromPC[4];
- char message3FromPC[4];
- //String messages[5] = {0};
- int integerFromPC = 0;
- float floatFromPC = 0.0;
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(115200);
- }
- void loop()
- {
- String command = ""; //Used to store the latest received command
- int serialResult = 0; //return value for reading operation method on serial in put buffer
- serialResult = readSerialInputCommand(&command);
- if(serialResult == MSG_METHOD_SUCCESS)
- {
- command.toCharArray(charBuf, 32);
- parseData();
- useParsedData();
- }
- if(self == "1A")
- {
- if(serialResult == WRG_NO_SERIAL_DATA_AVAILABLE)
- {//If there is no data AVAILABLE at the serial port, let the LED blink
- digitalWrite(LED_BUILTIN, HIGH);
- delay(750);
- digitalWrite(LED_BUILTIN, LOW);
- delay(750);
- }
- else
- {
- if(serialResult == ERR_SERIAL_IN_COMMAND_NOT_TERMINATED)
- {//If the command format was invalid, the led is turned off for two seconds
- digitalWrite(LED_BUILTIN, LOW);
- delay(2000);
- }
- }
- }
- else if (self == "1B");
- digitalWrite(LED_BUILTIN, LOW);
- }
- void parseData()
- {
- char * strtokIndx; // this is used by strtok() as an index
- strtokIndx = strtok(charBuf, " "); // get the first part - the string
- strcpy(message1FromPC, strtokIndx); // copy it to messageFromPC
- strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
- strcpy(message2FromPC, strtokIndx); // copy it to messageFromPC
- strtokIndx = strtok(NULL, "#");
- strcpy(message3FromPC, strtokIndx); // copy it to messageFromPC
- //floatFromPC = atof(strtokIndx); // convert this part to a float
- //integerFromPC = atoi(strtokIndx); // convert this part to an integer
- }
- void useParsedData() //GO 111 222#
- {
- Serial.println("Yes.");
- Serial.println(message1FromPC);
- Serial.println(message2FromPC);
- Serial.println(message3FromPC);
- if (strcmp(message1FromPC,"GO")==0){
- Serial.println("GO command.");
- int moveX = message2FromPC;
- int moveY = message3FromPC;
- Serial.println(moveX+" "+moveY);
- }
- }
- int readSerialInputCommand(String *command)
- {
- int operationStatus = MSG_METHOD_SUCCESS; //Default return is MSG_METHOD_SUCCESS reading data from com buffer.
- if (Serial.available()) //check if serial data is available for reading
- {
- char serialInByte; //temporary variable to hold the last serial input buffer character
- do
- { //Read serial input buffer data byte by byte
- serialInByte = Serial.read();
- *command = *command + serialInByte; //Add last read serial input buffer byte to *command pointer
- } while(serialInByte != '#' && Serial.available()); //until '#' comes up or no serial data is available anymore
- if(serialInByte != '#')
- operationStatus = ERR_SERIAL_IN_COMMAND_NOT_TERMINATED;
- }
- else
- operationStatus = WRG_NO_SERIAL_DATA_AVAILABLE;
- return operationStatus;
- }
- void SendOutput(String Send1)
- {
- Serial.print(StartTX);
- Serial.print(Send1);
- Serial.print(EndTX);
- }
- void SendOutput(String Send1, String Send2)
- {
- Serial.print(StartTX);
- Serial.print(Send1);
- Serial.print(ResumeTX);
- Serial.print(Send2);
- Serial.print(EndTX);
- }
- void SendOutput(String Send1, String Send2, String Send3)
- {
- Serial.print(StartTX);
- Serial.print(Send1);
- Serial.print(ResumeTX);
- Serial.print(Send2);
- Serial.print(ResumeTX);
- Serial.print(Send3);
- Serial.print(EndTX);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement