Как я могу обмениваться данными между Matlab GUI (развернутым с MCR) и кодом C ++?

Я работаю над 64-битной версией Linux и хочу использовать графический интерфейс (созданный с помощью Matlab и развернутый как общая библиотека благодаря MCR) в коде C++, где находятся мои функции исчисления.

Проблема заключается в том, чтобы обмениваться данными между графическим интерфейсом и кодом C++.

Я изолировал GUI и функции исчисления в двух разных потоках в C++, и я смог написать в именованный канал из GUI (после нажатия на кнопку, которая включает обратный вызов), пока C++ читал.

Таким образом, я получаю данные для функций исчисления, но затем, когда GUI собирается читать, все блокируется.

Вот некоторые фрагменты моего кода:

Мой поток для GUI (C++):

static void *fn_gui(void *p_data)
{
    std::cout << "the gui should launch now" << std::endl;

    /* Call of the Matlab Gui*/ 
    gui();

    mclWaitForFiguresToDie(NULL);

    return NULL;
}

Моя ветка для исчисления (C++):

static void *fn_calculus(void *p_data)
{
    mkfifo("mypipe1",S_IRWXU);
    mkfifo("mypipe2",S_IRWXU);  

    /*****************/
    /*   READING     */
    /*****************/

    /* Declaration of the input structures of the calculus functions */
    const char * in_fieldnames[] = {"x","y"};

    mwArray a(1,1,2,in_fieldnames);
    mwArray b(1,1,2,in_fieldnames);
    mwArray c(1,1,2,in_fieldnames);

    /* Opening of the first fifo */
    int mypipe1=open("mypipe1",O_RDONLY);

    /* Determination of the length of the length of the data (1st character of the message) */
    char * lc;
    lc = (char*) malloc(sizeof(char));
    read(mypipe1,lc,1);
    int l = atoi(lc);

    /* Determination of the length of the data (the l following characters) */
    char * Lc;
    Lc = (char*) malloc(l*sizeof(char));
    read(mypipe1,Lc,l);
    int L = atoi(Lc);

    /* Memory allocation of the message and storage of the data (the L following characters)*/
    char * message1;
    message1 = (char*) malloc(L*sizeof(char));
    read(mypipe1,message1,L);
    close(mypipe);

    std::cout << "message = " << message1 << std::endl;

    /* Formatting of the data */
    mwArray flow1(message1);
    mwArray data;

    split(1,data,flow1);  /* Put the string "a:b:c:d" under the "a" "b" "c" "d" form */
                    /* temporary Matlab deployed function to improve */

    /*****************/
    /*   CALCULUS    */
    /*****************/

    /* Filling of the input structures */
    a.Get(1,1).Set(data.Get(1,1));
    a.Get(1,2).Set(data.Get(1,2));mkf
    b.Get(1,1).Set(data.Get(1,3));
    b.Get(1,2).Set(data.Get(1,4));

    /* Call to the Matlab Calculus Function (it fills c) */
    minpyt(1,c,a,b);

    std::cout << "c = " << c << std::endl;

    /* Formatting of the data */

    double result[2];

    c.Get("x",1,1).GetData(&result[0],1);
    c.Get("y",1,1).GetData(&result[1],1);

    int length=sizeof(result[0])+sizeof(result[1])+1;  /* length of the two figures plus the extra ':' character between them */
    int length2 = (int) log10(length) + 1; /* number of digits of the previous length */

    char message2[length+length2+1]; /* length for the message and the 2 lengths */

    sprintf(message2,"%d%d%f:%f",length2,length,result[0],result[1]);


    /*****************/
    /*   WRITING     */
    /*****************/

    int mypipe2 = open("mypipe2",O_WRONLY);

    write(mypipe2,message2,sizeof(message2));

    close(mypipe2);

    return NULL;
}

обратный вызов моего GUI (Matlab):

function buttonCallback(gcbf,data)

    % Getting the data from the GUI
    data = guidata(gcbf);

    a.x = get(data.Ax,'String');
    a.y = get(data.Ay,'String');
    b.x = get(data.Bx,'String');
    b.y = get(data.By,'String');

    guidata(gcbf,data);

    % Formatting the data

    message = [a.x ':' a.y ':' b.x ':' b.y];
    L = num2str(length(message));
    l = num2str(length(L));

    message = [l L message];

    % WRITING

    mypipe1 = fopen('mypipe1','w');
    fwrite(mypipe1,message,'char');
    fclose(pipe);

    % READING

    mypipe2 = fopen('mypipe2','r');
    l = fread(mypipe2,1,'double');
    L = fread(mypipe2,l,'double');
    flow = fread(mypipe2,L,'*char');
    fclose(mypipe2);

    fprintf(1,'message read = %s\n',flow);

    % Formatting the data

    message = regexp(flow,':','split');

    c.x = str2double(message{1,1});
    c.y = str2double(message{1,2});

    % Updating the GUI

    set(data.Cx,'String',c.x);
    set(data.Cy,'String',c.y);

    plot(data.axes,[a.x;b.x;c.x;a.x],[a.y;b.y;c.y;a.y],'r-');

    axis equal;

end

GUI вызывает функцию обратного вызова, когда я нажимаю на кнопку (после заполнения значений a и b). Основная функция C++ просто инициализирует MCR и соответствующие библиотеки и запускает потоки.

Если у кого есть идея.

Спасибо и всего наилучшего

0 ответов

Другие вопросы по тегам