1

我在 64 位 Linux 上工作,我想在 C++ 代码中使用 GUI(使用 Matlab 创建并部署为共享库,感谢 MCR),我的微积分函数在哪里。

问题是在 GUI 和 C++ 代码之间共享数据。

我在 C++ 的两个不同线程中隔离了 GUI 和微积分函数,并且我能够在 C++ 读取时从 GUI 中写入命名管道(单击打开回调的按钮后)。

因此,我将数据提供给微积分函数,但是,当 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

当我单击一个按钮(在填充 a 和 b 的值之后)时,GUI 调用回调函数。C++ 的主要功能只是初始化 MCR 和适当的库并启动线程。

如果有人有想法。

谢谢并恭祝安康

4

0 回答 0