Most convenient way to access variable typed input signal

조회 수: 1 (최근 30일)
Lukas Abelt
Lukas Abelt 2017년 7월 11일
댓글: Lukas Abelt 2017년 7월 19일
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

채택된 답변

Don Zheng
Don Zheng 2017년 7월 18일
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.
  댓글 수: 1
Lukas Abelt
Lukas Abelt 2017년 7월 19일
Thanks a lot. This was the exact option I was looking for. I must've missed it during my Research.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by