2

I am writting a S Function that outputs a array of boolean (4 positions)

I am having a access memmory violation while writing the outputs inside mdlOutputs callback.

This error is not instantenous. I mean: the code runs fine until it crashes later on and Matlab gives a fatal error. I'm pretty sure the error is in the memcpy operation because I have debugged and after I comment this line of code, it works fine. Also, it runs 100% fine if the data type is a scalar.

So, I have a couple of questions:

  1. How can I use memcpy to write the values of an array in simulink?
  2. I have assumed that simulink output memmory is a continuous memmory block. Is it correct?
  3. Any hints on how to solve it?

Pseudo-Code is here:

static void mdlOutputs ( SimStruct* S, int_T tid ) {
(...)
//src variable defined here
(...)

void* dst;

// get output port data type id
type = ssGetOutputPortDataType ( S, i );  // type = SS_BOOLEAN

// get data type size
size = ssGetDataTypeSize ( S, type );   //size = 1 byte

// get output port signal pointer
dst = ( void* ) ssGetOutputPortSignal ( S, i );  //dst has a valid value

// get output port width
width = ssGetOutputPortWidth ( S, i );  //width is 4, since it is a 4 position array

size = size * width; // size now is 4

memcpy ( dst, src, size );     // Error
}
m.s.
  • 16,063
  • 7
  • 53
  • 88
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
  • 1
    Are you getting anything from an input port? You haven't shown that bit of code, but you may need to be using [ssSetInputPortRequiredContiguous](http://www.mathworks.com/help/simulink/sfg/sssetinputportrequiredcontiguous.html). – Phil Goddard Oct 29 '15 at 22:44
  • I´m not 100% sure, but it seems that input port is OK, and the problem is while writing to the SFunction Outport. I am able to get all data from the input and parse it to the correct type. I googled it but I couldn´t find a ssSet_OUTPUT_PortRequiredContiguous method. There must be a logical reason for that. – guilhermecgs Oct 30 '15 at 11:29
  • Do I need to get pointers to the output with ssGetOutputPortSignal ? What if input port number 5 is an array? – guilhermecgs Oct 30 '15 at 11:34
  • For your src input port, is the size, type and width identical to your output port i? – Navan Oct 30 '15 at 13:39
  • I encountered a similar problem when using an S-function that was compiled in an older version of Matlab. – Karlo Jan 05 '16 at 12:05

0 Answers0