vendredi 29 juin 2018

s-Function TCP/IP does not send because of if-else

my problem is specific, but hopefully I can describe it simple.

I wrote a level-2 s-Function in C++ to connect my simulink model with my C++ - code. The s-Function is the client, C++ - code the server. Connecting works well. I also can send data from client to server in a session or from server to client in an other session. The s-Function has a sample time of 0.001 seconds.

The source ot the client sends data every 0.5 seconds, but because of my sample time I put a rate-transition block between the source and the client, so that the client has data available every step (Input1 and Input2). I decieded to use two memory blocks (Input3 and Input4 / Output2 and Output3) and to check every time step if there is new data or not. If there is new data, the client should send the data to my c++ - code. If not, the client shouldn't do anything.

My problem is, that I can't send AND receive data with the client in a session. It only sends the data one time at the beginning of the simulation and receives the data. After the start of the simulation it should send data every 0.001 seconds, but it doesn't. My C++ - code is waiting for the data the whole time, so it can't send new data, because it is in blocking mode.

Here is the part of the s-Function Code:

    double* Input1 = (double*)ssGetInputPortSignal(S, 0); 
    double* Input2 = (double*)ssGetInputPortSignal(S, 1); 
    double* Input3 = (double*)ssGetInputPortSignal(S, 2); 
    double* Input4 = (double*)ssGetInputPortSignal(S, 3);  

    int *Output1 = (int*)ssGetOutputPortSignal(S, 0); //Output of received values
    double* Output2 = (double*)ssGetOutputPortSignal(S, 1); //Output to the memory block
    double* Output3 = (double*)ssGetOutputPortSignal(S, 2); //Output to an other memory block

    if (*Input1 != *Input3 && *Input2 != *Input4) //New data available
    {
        send(socket, (char *)Input1, sizeof(double), 0);
        send(socket, (char *)Input2, sizeof(double), 0);
        *Output2 = *Input1; //set output to new value, so that it can't send this data again
        *Output3 = *Input2; //set output to new value, so that it can't send this data again
    }
    else //Don't do anything. Just specify the output
    {
        *Output2 = *Input3;
        *Output3 = *Input4;
    }
    //Receive
    int recvBytes;      
    recvBytes = recv(socket, (char *)Output1, sizeof(int), 0);

    if (recvBytes < 0) //No Bytes Available
    {
        *Output1 = -1;
    }

All of inputs and outputs are 1-D. I also checked the if-else: Every 500 steps the if-statement is true (like it should be), but my client doesn't send anything.

The programm is running without the if-else-statement, but I need it this way.

I appreciate any help! Thank you.

Aucun commentaire:

Enregistrer un commentaire