一些帮助编写S函数以从串行端口读取数据 [英] Some help writing a s-function to read data from serial port

查看:276
本文介绍了一些帮助编写S函数以从串行端口读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现问题后,我在这里,我需要一些帮助用MATLAB Function模块编写一个函数.我在以下链接中看到,有些人使用该块或s函数解决了该问题: http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/317910

After problems I had here, I need some help to write a function with MATLAB Function block. I saw in following links that some people solved it with that block or s-function: http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/317910

http://www.physicsforums.com/showthread.php?t=595813

http://www.mathworks.de/matlabcentral/newsreader/view_thread/250266

所以我尝试了这个:

function y = fcn(u)
coder.extrinsic('only3')
coder.extrinsic('serial', 'fopen','fread')
coder.extrinsic('set')

persistent s a 
y = (zeros(2,1));
  s = serial('COM12');

set(s,'Terminator','', 'InputBufferSize', 1024);
a = char('000');        % a-initialization for mxArray problems

a = only3(get(s,'status'));   %to check if port is already opened calling custom function 
if strncmp(a,'clo',3)==true
    fopen(s)
else
    fclose(s)
end

   y = fread(s,[2 1],'uint8'); % I have to read some data from serial. This command works fine in the matlab command window.

其中only3是我创建的函数.它从字符串中获取第一个3个字符,我需要它仅比较'status'答案的三个字符:

Where only3 is a function that I created. It takes first 3 char from a string, and I need it to compare only three char of the 'status' answer:

function let = only3(string)

let = string(1:3);

我这样做是为了知道通讯是否已经打开.但是simulink会以窗口形式返回错误:

I did it to know if communication is already opened. But simulink returns me an error as window:

Call to MATLAB function aborted: Open failed: Port: COM12 is not available. No ports are available.

我认为它会在第一次迭代中打开端口后尝试打开端口.

I think it tries to open the port after port opening in the first iteration.

编辑:我以此更改代码:

function y = fcn(u)

coder.extrinsic('only3')
coder.extrinsic('strncmp') %like Phil say
coder.extrinsic('serial', 'fopen','fread')
coder.extrinsic('get')
persistent s a b
y = (zeros(2,1));

%%部分摘自Phil的建议:

%%Part taken from Phil suggestion:

 if isempty(s)
   % only do this the first time
    s = serial('COM12','Terminator','', 'InputBufferSize', 1024);
    a = '000';
    b = false; %without this returns mxArray error.
end 


a = only3(get(s,'status'));
b = strncmp(a,'clo',3);
if b == true
    fopen(s)
else
    fclose(s)
end


y = fread(s,[2 1],'uint8'); 

它返回错误:

Unsuccessful read: OBJ must be connected to the hardware with FOPEN. Block MATLAB Function (#24) While executing: State During Action 

突出显示y表达式.

更新 我用以下代码解决了该问题:

UPDATE I solved it with following code:

function y = fcn(u)
coder.extrinsic('only3')
coder.extrinsic('strncmp')
coder.extrinsic('serial', 'fopen','fread')
coder.extrinsic('get')
persistent s a b
y = uint8(zeros(2,1));  %signal is an uint8

 if isempty(s)
   % only do this the first time
    s = serial('COM12','Terminator','', 'InputBufferSize', 1024);
    a = '000';
    b = false;
    a = only3(get(s,'status'));
b = strncmp(a,'clo',3);

    switch double(b)
    case 1
        fopen(s);
    otherwise
        fclose(s);
    end
end

   y = uint8(fread(s,[2 1],'uint8')); 

但是,正如我在下面评论的那样,每次停止仿真时,我都必须重新启动Matlab,因为它不会关闭通讯.我之所以这样说是因为,如果我重试开始仿真,它将返回我的第一个错误":

But, as I commented below, every time that I stop simulation I have to restart Matlab because it does not close communication. I say this because if I re-try to start simulation it returns "my first error":

Call to MATLAB function aborted: Open failed: Port: COM12 is not available. No ports are available.

我不知道为什么.每次模拟停止时都会有运行fclose(s)的东西,就像M代码1级S函数中的mdlTerminate函数一样.有什么建议吗?

I do not know why. There would be something that runs fclose(s) every simulation stopping like mdlTerminate function in M-code Level-1 S-functions. Some suggestion ?

推荐答案

您对持久变量的初始化是错误的.想必您真正想要的是

Your initialization of the persistent variables is wrong. Presumably what you really want is

persistent s a
if isempty(s)
   % only do this the first time
   s = serial('COM12');
   a = '000';
end

这篇关于一些帮助编写S函数以从串行端口读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆