This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

RTX51 passing pointer to struct as a message

Hi. I'm trying to send a message using RTX51, where the message is a pointer to a struct. I'm having trouble with type conversions, and the pointer never comes out correctly on the other side.

This is what i'd like to do:

//TASK1
SOME_STRUCT xdata myStruct;
os_send_message(MBX_NUM, &myStruct, 255);
...
//TASK2
SOME_STRUCT xdata * pStruct;
os_wait(K_MBX+MBX_NUM, 255, pStruct);
...

Of course, this gives me all sorts of type conversion errors. To fix this, i've tried casting the variables going into and coming from the message functions. It gets rid of the type conversion errors, but still doesn't work correctly.

//TASK1
SOME_STRUCT xdata myStruct;
os_send_message(MBX_NUM, (int)&myStruct, 255);
...
//TASK2
SOME_STRUCT xdata * pStruct;
os_wait(K_MBX+MBX_NUM, 255, (word xdata*)pStruct);
...

Does anybody know how I can correctly cast the arguments to make this work? Any other suggestions?

  • Check the first call carefully to see if the cast to int really does what you want. & might always produce a generic pointer (24 bits), instead a pointer-to-xdata. If so, that would convert poorly to an int. You might want intermediate cast:

    (int)(U8 xdata*)&myStruct

    to try to make it clear to the compiler that you want to (1) lose the tag byte and then (2) pretend that those 16 bits are an int just to match the function signature.

    In the os_wait() call, it looks like you have the wrong level of indirection. C parameters are pass-by-value, so the third parameter can't output the received message itself. It has to be a pointer to the place where you want those 16 bits to go (and it's irrelevant to the os_wait call that the message is a pointer). You need an &pStruct to get the address where pStruct lives so os_wait() can write there. Probably no casts needed in this case. (Assuming os_wait() is prototyped as a void*, else you need a cast to match.)

  • Got it working now. The send_message function needed to be cast as unsigned. It wasn't necessary to use the (U8 xdata*) cast, although that's probably just my specific situation. Good to know anyway for future reference.

    Also, the os_wait function was passing back a pointer, which i neglected to dereference. The simple fix was to get the pointer, and then cast the actual dereferenced pointer into the desired struct pointer:

    //TASK1
    SOME_STRUCT xdata myStruct;
    os_send_message(MBX_NUM, (unsigned int)&myStruct, 255);
    ...
    
    
    //TASK2
    SOME_STRUCT xdata * pStruct;
    unsigned int xdata * pTemp;
    
    os_wait(K_MBX+MBX_NUM, 255, pTemp);
    pStruct = (SOME_STRUCT xdata*)(*pTemp);
    ...
    

    Thanks for the tips.