首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中断matlab中TCP-IP回调函数

中断matlab中TCP-IP回调函数
EN

Stack Overflow用户
提问于 2018-11-13 01:14:33
回答 1查看 196关注 0票数 0

最近编写了在matlab的两个实例之间建立连接的代码。我可以通过执行代码的TCP-IP连接发送消息。现在,我尝试将代码设置为可中断,因为我希望通过TCP-IP启动/停止函数。但问题是,在函数完成之前,发送第二个命令什么也不做。有没有办法中断TCP-IP回调函数?

代码:

代码语言:javascript
复制
classdef connectcompstogether<handle
    properties
        serverIP
        clientIP
        tcpipServer
        tcpipClient
        Port = 4000;
        bsize = 8;

        earlystop
    end

    methods
        function gh = connectcompstogether(~)
            %           gh.serverIP = '127.0.0.1';
            gh.serverIP = 'localhost';
            gh.clientIP = '0.0.0.0';
        end

        function SetupServer(gh)
            gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
            set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
            fopen(gh.tcpipServer);
            display('Established Connection')
        end

        function SetupClient(gh)
            gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
            set(gh.tcpipClient, 'InputBufferSize',gh.bsize);
            set(gh.tcpipClient, 'BytesAvailableFcnCount',8);
            set(gh.tcpipClient, 'BytesAvailableFcnMode','byte');
            set(gh.tcpipClient, 'BytesAvailableFcn', @(h,e)gh.recmessage(h,e));
            fopen(gh.tcpipClient);
            display('Established Connection')
        end
        function CloseClient(gh)
            fclose(gh.tcpipClient);
            gh.tcpipClient = [];
        end
    end
    methods
        function sendmessage(gh,message)
            fwrite(gh.tcpipServer,message,'double');
        end

        function recmessage(gh,h,e)
            Message = fread(gh.tcpipClient,gh.bsize/8,'double');

            if Message == 444
                gh.Funwithnumbers();
            elseif Message == 777
                gh.earlystop = 1;
            end
        end

        function Funwithnumbers(gh)
            x=1;
            while true
                if x > 5000, break;end
                if gh.earlystop == 1,break;end
                x = x+1;
                display(x)
            end

        end
    end
end

为了便于理解代码。

服务器

代码语言:javascript
复制
Ser = connectcompstogether;
ser.SetupServer();
ser.sendmessage(333);

客户端

代码语言:javascript
复制
cli = connectcompstogether;
cli.SetupClient();

更新:所以在浏览网页后,我发现基于这个post,tcpip回调是不能被中断的。这篇文章是在2017年发布的,这意味着我的2016a版本绝对不能中断回调。

所以更新我的问题,有没有可能在matlab中启动一个子进程来运行函数。我只想使用回调来启动代码。如果我可以从回调中启动子进程。然后,我应该能够释放主进程,并使用tcpip在不同的计算机上启动/停止函数。

更新2:所以我尝试使用'spmd‘命令进行并行处理,但问题仍然存在。

代码语言:javascript
复制
    function recmessage(gh,h,e)
        Message = fread(gh.tcpipClient,gh.bsize/8,'double');

        spmd
            switch labindex
                  case 1
                        if Message == 444
                             gh.Funwithnumbers();
                        elseif Message == 777
                             gh.earlystop = 1;
                       end
            end
       end
    end
EN

回答 1

Stack Overflow用户

发布于 2018-11-14 19:35:05

您可以使用timer对象,它可以方便地延迟某些函数的执行。

代码语言:javascript
复制
t=timer('ExecutionMode','singleShot', 'StartDelay',0, 'TimerFcn',@myCallback);
start(t);

在本例中,StartDelay为0,因此myCallback几乎会立即添加到任务队列中,等待Matlab处理。但是,只有在完成对tcpip对象的回调之后,才会开始执行。然而,一旦启动,它将阻塞队列。

您可以尝试如下所示:

代码语言:javascript
复制
properties
    t=timer('ExecutionMode','singleShot', 'StartDelay',0, 'TimerFcn',@myCallback);
end

function tcpipCallback(gh,tcpObj,~)
message=fread(tcpObj,1,'double');
if message==444
   if strcmp(get(t,'Running'),'on')
     error('The function is running already');
   else
     set(gh.t,'UserData',false);
     start(gh.t);
   end
elseif message==777
   set(gh.t,'UserData',true);
end

function myCallback(tObj,~)
   ii=0;
   while ii<5000
     if get(tObj,'UserData'),break,end
     ii=ii+1;
     pause(.0001); %Pause to interrupt the callback; drawnnow might work too; or perhaps this is not needed at all.
   end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53267028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档