Most convenient way to access variable typed input signal
1 view (last 30 days)
Show older comments
Hello there,
I am currently in the process of writing a C/C++ S-Function for Simulink which uses a dynamically typed Input Signal. I wanted to ask what would be the most convenient way to access a dynamically typed Signal. Basically I don't actually care about the actual datatype and the only Thing I Need to do with the data is to copy it to a (temporary) buffer and possibly do some Byte swapping (in order to Change Little endian to big endian or vice versa).
Now the Simulink functions ssGetInputPortSignalPtrs only provides me with an Array of pointers to the individual elements of the Signal if I read the documentation correctly. Is there any way I could easily Access the whole data (like get a pointer to the Signal as I get iut from ssGetOutputPortSignal) that I could just memcpy() the data into my buffer and perform my Byte swapping there?
Or do I have to specifically use a switch depending on my Input type(As there are different Input datatype ptrs for those) and copy them element per element?
Thanks in advance
Lukas
EDIT: I figured it might be useful to see some code of my current implementation so here it is:
int_T Input_port_dtype_id = ssGetInputPortDataType(S, PORT_2);
int_T num_elements = ssGetInputPortWidth(S, PORT_2);
int_T dtype_size = ssGetDataTypeSize(S, input_port_dtype_id);
int_T total = dtype_size * num_elements;
InputPtrsType input_ptrs = ssGetInputPortSignalPtrs(S, PORT_2);
switch(input_port_dtype_id){
case 0: /* double */
InputRealPtrsType r64_ptrs = ssGetInputPortRealSignalPtrs(S, PORT_2);
for(int cur=0;cur<num_elements;cur++){
memcpy_s(buffer+(cur*dtype_size),total-(cur*dtype_size),r64_ptrs[cur],dtype_size);
}
break;
case 2: /* single */
InputReal32PtrsType r32_ptrs = (InputReal32PtrsType)input_ptrs;
for(int cur=0;cur<num_elements;cur++){
memcpy_s(buffer+(cur*dtype_size),total-(cur*dtype_size),r32_ptrs[cur],dtype_size);
}
break;
//And so on for all the other cases, you get the idea
}
So instad of this Switch case bulk of code I would rather do something like:
void *Input_ptr = ssSomeFctToGetTheInputPtr(S, PORT_2) //this should Point to the "beginning" of my Input Signal
memcpy_s(buffer,total,Input_ptr,total); //Copy the whole Input Signal in one go
Maybe this makes my question a bit clearer. Thanks again
0 Comments
Accepted Answer
Don Zheng
on 18 Jul 2017
Consider using 'ssSetInputPortRequiredContiguous' to require signal elements entering a port to be contiguous and then you might be able to use 'ssGetInputPortSignal' to get the head pointer and process accordingly.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!