Short: I want to read data from the terminal into a variable and compare it with a string.
Long: I am using a TI AM3358 development board from GOEMBED which runs embedded linux. I use this kit to read data from a canbus. To read the data from the canbus into the terminal I use socketcan.
When I run the following command into the terminal I get a datastream of can messages from the bus.
candump can0
I wrote some code which execute the above command and returns the output.
string GetCmdOutput(const char * cmd)
{
char buffer[128];
string result = "";
string different;
FILE* pipe = popen(cmd,"r");
int counter=0;
if(!pipe) throw runtime_error("popen() failed!");
try {
if(fgets(buffer,128,pipe) !=NULL){
while(counter<1){
result +=buffer;
counter++;
}
}
}catch(...){
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
In int main() I run the following code which compares the terminal output to a string:
string dump = "candump can0";
const char *senddump;
senddump = dump.c_str();
string newOutput;
int senddata = 0;
int i = 0;
int x = 0;
int amountS = 0;
int y = 0;
string datas;
while(i<1)
{
newOutput = GetCmdOutput(senddump);
cout<<newOutput + "\n";
if(newOutput==" can0 000 [2] 01 12\n")
{
canWrite(busn,baudrate, sendID, dlcn,data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
}
}
The code itself works, but I miss a lot of data from the canbus. I think the reason for this is that I always need to reinitialize the canbus (cmd --> candump can0).
Now my question is: How to change my code that I only need to run the command "candump can0" ones and that the code always compares the next messafes to the string. If this is possible, I think I will gain already a lot of perfomance.
If you have other commands or other options to improve the performancy, pleas do not hesitate to give constructive critisisme. (I'm not a professional! I try to learn)
/ EDIT 1 / The following datastream is how it is outputted into the terminal
can0 712 [1] 05
can0 192 [6] 1C 0F 19 00 00 00
can0 70B [1] 00
can0 70B [1] 85
can0 703 [1] 00
can0 707 [1] 00
can0 709 [1] 00
Thanks in advance, Kind regards, TMO