static UINT arduniorecvthread(LPVOID lpvoid) {
//this is where we receive data from Arduino connected switches, buttons, rotaries etc.
std:string com = SPo1;
std::string slashes = "\\\\.\\";
string comport = slashes + com;
Serial* SP1 = new Serial(strdup(comport.c_str()));
char incomingData[256] = "";
int dataLength = 255;
int readResult = 0;
string var = "";
string val = "";
string var2 = "";
string val2 = "";
if (SP1->IsConnected())
{
Log.timeStamp() << " Arduino Input Board 1 connected " << ENDL << ENDL;
}
while (SP1->IsConnected())
{
readResult = SP1->ReadData(incomingData, dataLength);
incomingData[readResult] = 0;
string str(incomingData);
// the str above can look like this
// "2405=0\r\n2404=0\r\n85=1\r\n" or just like "85=1\r\n"
// it has a var value before every "=" and a val after every "="
// the var and val can both be 1 digit to 4 digit
// below here is my code for getting the first var and val
// which I later send as svar and sval in the sendVarData function
// the code below is working OK for a str with only 1 var/val combination
// like "85=1\r\n"
// but how do I change the code so it can handle a long string in str
// and send all vars and val from the sendVarData
"
std::string::size_type pos = str.find('=');
if (pos != std::string::npos)
{
var = str.substr(0, pos);
}
string val_temp = str.substr(str.find("=") + 1);
std::string::size_type pos2 = val_temp.find('\r');
if (pos2 != std::string::npos)
{
val = val_temp.substr(0, pos2);
}
int svar = atoi(var.c_str());
int sval = atoi(val.c_str());
if (svar != 0)
{
IOCP.sendVarData(svar, sval);
var = "";
val = "";
}
Sleep(200);
}
return 0;
}
1.string str(incomingData); if you are running this is c++, then it will not work.
try this "string str;"
2. Make "var" an array and store your data in it.
3. or you can use a loop to run trough your string send the char to your board.
**I think that answer 1 is where most of your troubles are**
It is from the below that code line I need some good advise on how I can grab all var/val, even if there is only 1 var/val or many var/val combination in the str string