Using C code in s-functions

1 vue (au cours des 30 derniers jours)
Ufuk  Irmak
Ufuk Irmak le 27 Fév 2013
Hi everyone;
I have a c code which need to be used in simulink block. The c code is working well, but I could not build a proper s-function with it. There are several errors;
1- In my c code there is a function other than main, How it should be implement in s function?
2- There is a struct which occurs a trouble also.
3- Does normal c syntax problem for matlab 'int' cause a error
4- Although I gave an initial value for my variables s function builder always wants a Ivalue error
If you help me I appreciate, I could not find a proper source yet. Thank you

Réponses (3)

Kaustubha Govind
Kaustubha Govind le 5 Mar 2013
  1. It depends on where this function needs to be called from. Can you elaborate on how the function needs to be used?
  2. For structure inputs/outputs, you need to use bus signals.
  3. I don't know that you see errors using 'int' - could you please paste the offending code and the error message.
  4. Again, please paste the offending code and the error message.
  2 commentaires
Ufuk  Irmak
Ufuk Irmak le 7 Mar 2013
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int serial_port_open(void);
static const char *PORT_NAME = "/dev/ttyO2"; // for default RS232 console at BB
int serial_port;
struct termios options_original;
int main (void)
{
serial_port_open();
int temp[] = {1,2};
int a = sizeof(temp);
write(serial_port, &temp, a);
printf("%d\n",a);
return 0;
}
int serial_port_open(void)
{
struct termios options;
serial_port = open(PORT_NAME, O_RDWR | O_NONBLOCK);
if (serial_port != -1)
{
tcgetattr(serial_port,&options_original);
tcgetattr(serial_port, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD | CRTSCTS | CS8);
options.c_cflag &= (~PARENB & ~CSTOPB & ~CSIZE);
options.c_lflag |= ICANON;
options.c_iflag |= (IGNPAR |ICRNL);
if (tcsetattr(serial_port, TCSANOW, &options)!=0)
{
printf("error serial port could not open!!!");
return (-1);
}
}
else
{
end
This code and errors which I got, I made several changes to decrease errors but new ones occured while I was doing that. The main purpose of this code is serial port handling and write. I use it in Beagleboard xm with S-functions. I want my matlab algorithm to work in Beagleboard and use serial port. Thank you for your interest.
Kaustubha Govind
Kaustubha Govind le 7 Mar 2013
Ufuk: At this point, your code is not even in an S-function, but is a regular C-executable with a main(). You probably need to get help from a more C/C++ forum than a MATLAB/Simulink forum. It's hard for me to tell, because it's not clear where symbols like CLOCAL, and types like 'termios' are being defined. Please follow basic C rules and make sure that you can compile your code outside of MATLAB.

Connectez-vous pour commenter.


Dan
Dan le 9 Juil 2013
You declare to include some header files
#include fcntl.h #include errno.h #include signal.h #include stdlib.h #include termios.h #include unistd.h #include stdio.h #include string.h
But the Matlab/Simlink coder may fail to find these files. Consequently, when your codes refer to the variables predefined in these header, the compiler reports errors. Please see the Simulink 7 Writing S-Functions reference, you may need to define LIB_PATH, etc to have Matlab/Simulink to know where there header files are

Ahmet Tuna
Ahmet Tuna le 26 Mai 2017
Hey,
I have the same problem. I have C code, in this code i am communicating with serial port. The C code is working. I have made the changes for mexFunction. When i gave the command mex serialPort.c it also works and compiles. But when i use the function the serial does not open. Do you have a solution?
best regards

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by