Error when trying to interface Raspberry Pi 2 and serial devices: The 'serial' class does not support code generation.

2 views (last 30 days)
Hello,
I am having trouble creating code in Simulink to handle interfacing between my Raspberry Pi 2 and a serial device connected to pins 8 and 10. I have had no problem interfacing with this setup using the serialDev class. My goal is to build a MatLAB function block in Simulink that initializes the Raspberry Pi 2 serial port and read/write to it. The serialDev class doesn't seem like it works in Simulink so I attempted to use the serial class. Here is my code:
function y = GetParallaxParam()
%serial port parameters for Parallax
port = '/dev/ttyAMA0';
baudRate = 115200; %fastest allowed
dataBits = 8;
parity = 'none';
stopBits = 1;
%connect to serial port
Parallax = serial(port); %error Here!
Parallax.BaudRate = baudRate;
Parallax.DataBits = dataBits;
Parallax.StopBits = stopBits;
%default parity is none
fopen(Parallax);
y = Parallax;
Here is the error message:
The 'serial' class does not support code generation.
Function 'MATLAB Function' (#225.209.221), line 10, column 12:
"serial(port)"
Is my implementation of the serial class wrong? Or is there another strategy I need to follow?
Thank you,
Will

Answers (2)

Walter Roberson
Walter Roberson on 23 Sep 2015
serial class has a lot of implications about callbacks and background processing and buffering and so on, which mostly cannot be expected to be supported on a generic system; remember that code generation has to target something that barely has a Standard C library.
Is there a reason to not use the Serial Configuration Block ?
  3 Comments
Will Schoenhals
Will Schoenhals on 24 Sep 2015
I don't have trouble communicating between Raspberry Pi 2 and my serial device with normal matlab.m files. My difficulty lies with Simulink. I am unable to find a way to facilitate communication between my Raspberry Pi 2 and serial device with MatLAB function blocks, Level-2 MatLAB functions in an S-block, or any other S-block.

Sign in to comment.


Will Schoenhals
Will Schoenhals on 7 Oct 2015
For anyone else with the same problem: This isn't much of an answer but I think it's a step in the right direction. http://www.mathworks.com/matlabcentral/answers/114683-simulink-read-a-file-on-raspberrypi-in-external-mode-error-unknown-type-name-a-filea If this code can read/write to an external I2C device there is a way to read/write serial.

Categories

Find more on Raspberry Pi Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!