0

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

TMO
  • 1
  • 3

1 Answers1

0

If there is continual output from candump you could simply pipe the output to your program like this:

$ candump can0 | myprogram

Where myprogram.cpp looks a bit like this:

#include <iostream>
#include <string>

int main()
{
    std::string message;

    while(std::getline(std::cin, message))
    {
        // process message here ... eg. ...
        if(message == "can0  000   [2]  01 12")
        {
            // do something specific ...        
        }
    }
}
Galik
  • 47,303
  • 4
  • 80
  • 117
  • Hi Galik, thanks for the reply. This a good answer. But then I have the following problem (which I didn't explain. My fault!). To narrow down the message flow into my embedded system, I sometimes change the parametes of candump. For example. The first steps I use candump can0. But after when I have collected the start up data I change my candump to candump can0,712:1FFFFFFF. Like this I can filter the data stream partly before reading in into my embedded system. So IS there an other way to do this? – TMO Apr 16 '18 at 07:03
  • @TMO You can just pipe the new command to the program the same way: `candump can0,712:1FFFFFFF | myprogram` – Galik Apr 16 '18 at 07:18
  • Hi, I don't think I understand it completely. Sorry. So first of all I write myprogram.cpp. Then when I'm in the terminal I run candump can0 | myprogram. At a certain point I receive the following message can0 000 [2] 01 12. Now when I receive this message I want to change my command to candump can0,712:1FFFFFFF. Then if I understand correctly I need to stop my program, and enter the following command again in the terminal:candump can0,712:1FFFFFFF | myprogram . Probably I understand you completely wrong. I wish that it goes automatically. Hopefully you understand what I try to explain. – TMO Apr 16 '18 at 07:45