0

I'm wrote some code which uses socketcan (to make a candump). With this code I initialize socketcan. After it is initialized I create a candump in the terminal. Those values I read into a variable. Now I would like to compare this variable with a string.

The string has a certain format. When I manually run a candump I get the following format:

can0 71A [1] 7F

(2x space) can0 (2x space) 71A (3x space) [1] (2x space) 7F.

I'm able to read the terminal output into the variable (result)

string result = //the string is loaded

** Now I wish to execute some code when the text in the variable "result" is not equal to the following string:

"  can0  71A   [1]  7F" 

The following chunk of code is to make the comparison.

if(result!="  can0  71A   [1]  7F"){
cout<<"input changes";
}

When I execute this code and I send it on the bus the following string. Which is the same as above:

"  can0  71A   [1]  7F" 

the code prints:

"input changes"

(Normally it shouldn't print anything)

When I send the following string on the bus:

"  can0  19B   [1]  6A" 

It also prints: "input changes"

(which is good)

I tried to change the spaces to tabs as well without any result.

Any idea what the problem could be and how to solve this?

* Edit 1 * The following is my code to read the value into the variable "result". (If you see other stupid things in my code, pleas feel free to give some constructive criticisme! I want to learn from my projects.)

    std::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<2){

                                result +=buffer;
                                cout<<result + "\n";
                                if(result!="  can0  71A   [1]  7F" || result != "  can0  71A   [1]  04" )
                                {
                                        cout<<"changed!\n";
                                        different = result;
                                }
                        }
        }catch(...){
                pclose(pipe);
                throw;
        }

        pclose(pipe);
        pauseCan();
        return different;
}

Kind regards, TM

  • I'll bet `result` has a newline at the end. But since you haven't posted how you set `result` it's hard to tell. – Barmar Mar 28 '18 at 15:35
  • I add the code. Thanks for the reply. – MertensToon Mar 28 '18 at 17:40
  • 1
    The code has unmatched braces. There's no `}` for `if(fgets(buffer, 128, pipe)){` – Barmar Mar 28 '18 at 17:46
  • 1
    Looks like my guess was right: `fgets()` includes the newline in the buffer. – Barmar Mar 28 '18 at 17:47
  • 1
    ALso `||` needs to be `&&`. See https://stackoverflow.com/questions/26337003/execute-block-if-a-variable-is-not-one-of-some-specific-values – Barmar Mar 28 '18 at 17:48
  • Barmar, Thanks for helping. So the best way is to change fgets to gets? So the new line isn't included? I will try it out tomorrow (Don't have the cannode with me now.) Are ther eother things that I can improve? Kind regards – MertensToon Mar 28 '18 at 18:01
  • 1
    Just remove the newline from `buffer`, or add it to the string you're comparing with. – Barmar Mar 28 '18 at 18:03
  • 1
    `gets()` is not a useful solution. It only reads from standard input, and it has been removed from the language because it doesn't have a `buffersize` parameter, so it can write outside the array bounds. – Barmar Mar 28 '18 at 18:03
  • 1
    You could also look here for a C++ equivalent to `popen()` so you can use `getline()`: https://stackoverflow.com/questions/3190514/popen-equivalent-in-c – Barmar Mar 28 '18 at 18:05
  • Hi all, Now I'm running into timing issues. So when I'm connected to the bus, I always need to run GetCmdOutput(const char * cmd). So I reinitialize the canbus. By doing this I'm missing lots of data. Is there a way to manipulate my code, so I only need to run the terminal command ones and after I read always the new line which enters the terminal? – MertensToon Apr 13 '18 at 08:59
  • `popen()` can run any shell command, so you can run a `while` loop that keeps restarting the program that reads from the canbus. But if data from the canbus is lost whenever that program restarts, there's nothing you can do in this program. You need a reliable way to read from the device. – Barmar Apr 13 '18 at 15:37

0 Answers0