Embedded Linux -- TI AM335x -- Send data on canbus and log at the same time -- c++

I would like to ask for some advice. I'm trying to read data from a canbus system. For this I cross compiled some code on ubuntu 12.04.

What I trying to accomplish:

So on the canbus I connected a CANopen sensor. While this sensor is sending continiously data to my embedded system, I need to read settings from the sensor. To read and write this settings I use socketCAN.

So I tried the following:

#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
#include <stdio.h>
#include <string>

using namespace std;

string IntToString(int number)
{
        stringstream temp;
        temp<<number;
        return temp.str();
}

string IntToHex(int data)
{
        stringstream ss;
        int sli;
        ss<< std::hex << data;
        string sdat = ss.str();
        sli= sdat.length();

         switch(sli){
                case 1: sdat = "0" + sdat;  break;
                default: sdat = sdat;
        }
        return sdat;
}


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;
}


void init_Can(int bus, int baudrate)
{
        string busnumber = IntToString(bus);
        string busbaudrate = IntToString(baudrate);

        const char *Reset;
        const char *Setup;
        const char *Conf;

        string reset = "ifconfig can" + busnumber + " down";
        string setup = "ip link set can" + busnumber + " up type can bitrate " + busbaudrate + "000";
        string conf = "ifconfig can" + busnumber + " up";
        cout<<reset + "\n";
        cout<<setup + "\n";
        cout<<conf + "\n";

        Reset=reset.c_str();
        Setup=setup.c_str();
        Conf=conf.c_str();

        system(Reset);
        system(Setup);
        system(Conf);

}

void canWrite(int busN, int baud, string id, int dlc, int dat0, int dat1, int dat2, int dat3, int dat4, int dat5, int dat6, int dat7)
{
        string busnumber = IntToString(busN);
        string busbaudrate = IntToString(baud);

    cout<<"send data?\n";

        string message[8];
        message[0] = IntToHex(dat0);
        message[1] = IntToHex(dat1);
        message[2] = IntToHex(dat2);
        message[3] = IntToHex(dat3);
        message[4] = IntToHex(dat4);
        message[5] = IntToHex(dat5);
        message[6] = IntToHex(dat6);
        message[7] = IntToHex(dat7);

        string cansend;

        switch(dlc)
        {
                case(0): cansend = "cansend can" + busnumber + " " + id + "#";  break;
                case(1): cansend = "cansend can" + busnumber + " " + id + "#" + message[0];     break;
                case(2): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1];  break;
                case(3): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1] + "." + message[2];       break;
                case(4): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1] + "." + message[2] + "." + message[3];    break;
                case(5): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1] + "." + message[2] + "." + message[3] + "." + message[4]; break;
                case(6): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1] + "." + message[2] + "." + message[3] + "." + message[4] + "." + message[5];      break;
                case(7): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1] + "." + message[2] + "." + message[3] + "." + message[4] + "." + message[5] + "." + message[6];   break;
                case(8): cansend = "cansend can" + busnumber + " " + id + "#" + message[0] + "." + message[1] + "." + message[2] + "." + message[3] + "." + message[4] + "." + message[5] + "." + message[6] + "." + message[7];        break;
        }

    const char *canSender;
        canSender = cansend.c_str();
        system(canSender);
    cout<<cansend + "\n";
}


int main()
{
    int baudrate, dlc,i;
    int data[8];
    string id;
        init_Can(0,250);

    string value;
    cout<<"Do we wish to start;";
    cin>>value;

    if(value!="Y")
    {
        usleep(1000000);
        return 0;
    }



    data[0] = 64;
    data[1] = 00;
    data[2] = 02;
    data[3] = 00;
    data[4] = 00;
    data[5] = 00;
    data[6] = 00;
    data[7] = 00;
    i = 0;

    id = "612";
    dlc = 8;


    string dump = "candump can0,592:1FFFFFFF";
    const char *senddump;
    senddump = dump.c_str();
    string outp;

    while(i<1)
    {
        cout<<"start new loop\n";

        canWrite(0,250000, id, dlc, data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
        outp = GetCmdOutput(senddump);
        cout<<outp + "\n";
    }
}

So as you can see in the last steps I send a command onto the canbus (canwrite), straight after I initiate the candump.

The result of the dump should be "can0 592 [8] 80 00 02 00 00 00 02 06". Most of the time I get no answer, sometimes I get the correct one. I think this is because the capture of the can signal is to slow. (Always starting a new dump)

Now it would work much better if could start the candump in one terminal and log this terminal continiously and then in a seperated terminal send the commands to onto the CANbus.(When I try this manually this works great.) I have no Idea on how program this at all.

I also tried to first start my dump and then send the canbus signal. This is impossible because I can never send a canbus signal. (stay in the candump loop)

Can somebody point me in the correct direction? Or give me an explaination on how I could improve this?

Thanks in advance, Kind regards, TMJJ