Sorry for the delay. I confirm that the arduino will receive spaces. Lets say _ is a space. The arduino may receive: 'ID_Restrict_=_255CRLF' The idea is I want to remove the spaces, then use sRX.startsWith and parse the string so I end up with the value 255 that I convert to an int and store.
I understand I can load an array of chars and will look into this way of receiving serial data but I would still like to understand why the trim function does not work in my code.
From what I've read, it should be quite straight forward and sRX.Trim(); should get rid of the spaces. But it does not.
Thanks for the help.
Here is my complete code:
#include <stdio.h> // for function sprintf
// Declare variables to be used in the software
String sTemp; // sTemp is a temporary string used to convert different types into a string and later used to store the RX data
String sRX; // sRX is used to store the received data from the serial port
// Following integers are for storing values
int iAutoSet;
int iBaudRate;
int iBrightness;
int iButtons;
int iFade;
int iFadeSpeed;
int iFlickerRed;
int iIDRestrict;
int iImageFreeze;
int iInfraRed;
int iKeyLevel;
int iKeyer;
int iLocked;
int iMode;
int iOverscan;
int iOutHCenter;
int iOutHWidth;
int iOutVCenter;
int iOutVHeight;
int iPanXPos;
int iPanYPos;
int iReset;
int iRGBTerm;
int iRS232ID;
int iSCPhase;
int iSense;
int iSignalType;
int iStoreSettings;
int iSound;
int iSyncOut;
int iTotalLines;
int iVertFreq;
int iVGABot;
int iVGALeft;
int iVGAStore;
int iVGATop;
int iVGAWidth;
int iVideoStd;
int iZoom;
// Configuring the arduino
void setup() {
// Open the serial port
Serial.begin(9600); // open the serial port to send
// data back to the computer at
// 9600 bits per second
Serial.setTimeout(10); //set a timeout so the arduino does wait for too long on the serial.readstring
//Initialize values with ramdom numbers to emulate stored settings
iAutoSet = (random(0, 2)); //Auto Set Value 0 or 1
iBaudRate = (random(0, 192));
iBrightness = (random(0, 251));
iButtons = (random(0, 2));
iFade = (random(0, 2));
iFadeSpeed = (random(0, 26));
iFlickerRed = (random(0, 2));
iIDRestrict = (random(0, 256));
iImageFreeze = (random(0, 2));
iInfraRed = (random(0, 2));
iKeyLevel = (random(0, 251));
iKeyer = (random(0, 5));
iLocked = (random(0, 2));
iMode = (random(0, 5));
iOverscan = (random(0, 2));
iOutHCenter = (random(0, 641));
iOutHWidth = (random(0, 641));
iOutVCenter = (random(0, 577));
iOutVHeight = (random(0, 577));
iPanXPos = (random(0, 641));
iPanYPos = (random(0, 577));
iReset = (random(0, 2));
iRGBTerm = (random(0, 2));
iRS232ID = (random(0, 256));
iSCPhase = (random(-12, +13));
iSense = (random(0, 4));
iSignalType = (random(0, 2));
iSound = (random(0, 2));
iStoreSettings = (random(0, 2));
iSyncOut = (random(0, 2));
iTotalLines = (random(0, 641));
iVertFreq = (random(40, 70));
iVGABot = (random(0, 641));
iVGALeft = (random(0, 641));
iVGAStore = (random(0, 641));
iVGATop = (random(0, 641));
iVGAWidth = (random(0, 641));
iVideoStd = (random(0, 2));
iZoom = (random(0, 2));
// Welcome message
Serial.println();
Serial.println();
Serial.println(" TVone Emulator Ver 1.0");
Serial.println(" 10th August 2017");
Serial.println();
Serial.println("This code is used to emulate the TVone board in order to test software");
Serial.println(" developed in Delphi. ");
Serial.println();
Serial.println();
Serial.println();
Serial.println();
}
// This function gets rid of any character that is not part of the number and returns an integer corresponding to the value sent in the command
int GetValue(String myString)
{
int iStart = myString.indexOf('=')+1; // Get the position of the = symbol
String s = myString.substring(iStart); // Keep only the characters in between
s.replace("/r"," "); // Replace any carriage return with white space
s.replace("/n"," "); // Replace any line feed with white space
s.trim(); // Get rid of all white spaces
return s.toInt(); // Convert the string into an integer and return the value
}
// This function processes the string command and sends the appropriate output
String GetOutput(String myString, int *myInt) {
if (sRX.indexOf('=') == -1) //If '=' is not present then it is a query for value
{
return String(*myInt); //Return the value from the pointer
}
else //Else its a command change the value
{
*myInt = GetValue(myString); // get the value and store it to the pointed integer
return ">"; //Return acknowledge expected to be sent if successfull
}
}
// Software that runs in a non-stop loop
void loop() {
sRX = Serial.readString(); // Read from the serial port and put the values in the string sRX
sRX.trim(); //get rid of any white spaces
// Check which command is received and process accordingly
if(sRX.startsWith("Auto")) sTemp = GetOutput(sRX,&iAutoSet); // If the string starts with "Auto" then process get output and point to iAutoset
else if(sRX.startsWith("Baud")) sTemp = GetOutput(sRX,&iBaudRate);
else if(sRX.startsWith("Brig")) sTemp = GetOutput(sRX,&iBrightness);
else if(sRX.startsWith("Butt")) sTemp = GetOutput(sRX,&iButtons);
else if(sRX.startsWith("Fade S")) sTemp = GetOutput(sRX,&iFadeSpeed); //FadeS must be before Fade else Fade would always be true for both conditions
else if(sRX.startsWith("Fade")) sTemp = GetOutput(sRX,&iFade);
else if(sRX.startsWith("Flic")) sTemp = GetOutput(sRX,&iFlickerRed);
else if(sRX.startsWith("ID Re")) sTemp = GetOutput(sRX,&iIDRestrict);
else if(sRX.startsWith("Imag")) sTemp = GetOutput(sRX,&iImageFreeze);
else if(sRX.startsWith("Infr")) sTemp = GetOutput(sRX,&iInfraRed);
else if(sRX.startsWith("Key L")) sTemp = GetOutput(sRX,&iKeyLevel);
else if(sRX.startsWith("Keye")) sTemp = GetOutput(sRX,&iKeyer);
else if(sRX.startsWith("Lock")) sTemp = GetOutput(sRX,&iLocked);
else if(sRX.startsWith("Mode")) sTemp = GetOutput(sRX,&iMode);
else if(sRX.startsWith("Over")) sTemp = GetOutput(sRX,&iOverscan);
else if(sRX.startsWith("Out H-C")) sTemp = GetOutput(sRX,&iOutHCenter);
else if(sRX.startsWith("Out H-W")) sTemp = GetOutput(sRX,&iOutHWidth);
else if(sRX.startsWith("Out V-C")) sTemp = GetOutput(sRX,&iOutVCenter);
else if(sRX.startsWith("Out V-H")) sTemp = GetOutput(sRX,&iOutVHeight);
else if(sRX.startsWith("Pan X")) sTemp = GetOutput(sRX,&iPanXPos);
else if(sRX.startsWith("Pan Y")) sTemp = GetOutput(sRX,&iPanYPos);
else if(sRX.startsWith("Rese")) sTemp = GetOutput(sRX,&iReset);
else if(sRX.startsWith("RGB T")) sTemp = GetOutput(sRX,&iRGBTerm);
else if(sRX.startsWith("RS23")) sTemp = GetOutput(sRX,&iRS232ID);
else if(sRX.startsWith("SC Ph")) sTemp = GetOutput(sRX,&iSCPhase);
else if(sRX.startsWith("Sens")) sTemp = GetOutput(sRX,&iSense);
else if(sRX.startsWith("Sign")) sTemp = GetOutput(sRX,&iSignalType);
else if(sRX.startsWith("Soun")) sTemp = GetOutput(sRX,&iSound);
else if(sRX.startsWith("Stor")) sTemp = GetOutput(sRX,&iStoreSettings);
else if(sRX.startsWith("Sync")) sTemp = GetOutput(sRX,&iSyncOut);
else if(sRX.startsWith("Tota")) sTemp = GetOutput(sRX,&iTotalLines);
else if(sRX.startsWith("Vert")) sTemp = GetOutput(sRX,&iVertFreq);
else if(sRX.startsWith("VGA B")) sTemp = GetOutput(sRX,&iVGABot);
else if(sRX.startsWith("VGA L")) sTemp = GetOutput(sRX,&iVGALeft);
else if(sRX.startsWith("VGA S")) sTemp = GetOutput(sRX,&iVGAStore);
else if(sRX.startsWith("VGA T")) sTemp = GetOutput(sRX,&iVGATop);
else if(sRX.startsWith("VGA W")) sTemp = GetOutput(sRX,&iVGAWidth);
else if(sRX.startsWith("Vide")) sTemp = GetOutput(sRX,&iVideoStd);
else if(sRX.startsWith("Zoom")) sTemp = GetOutput(sRX,&iZoom);
else sTemp = "?"; // If nothing matches then send "?"
if (sRX != "") Serial.println(sTemp); // Transmit the completed sTemp out of the arduino
} //end of the loop, start again at the top of void loop()