Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在GUI | MATLAB中运行无限循环收发数据_Matlab_User Interface_Parallel Processing_Matlab Guide_Xbee - Fatal编程技术网

在GUI | MATLAB中运行无限循环收发数据

在GUI | MATLAB中运行无限循环收发数据,matlab,user-interface,parallel-processing,matlab-guide,xbee,Matlab,User Interface,Parallel Processing,Matlab Guide,Xbee,我正在做一个项目,通过一台笔记本电脑向另一台笔记本电脑传输数据。我已经完成了GUI界面,但是接收部分有问题。由于接收者不知道接收文件的确切时间,我写了一个无限循环,它是: recv=[]; while (1) while s.BytesAvailable==0 end a=fscanf(s); recv=[recv a] end 我怎样才能一直运行这个for循环,从程序的最开始直到用户关闭程序,并且用

我正在做一个项目,通过一台笔记本电脑向另一台笔记本电脑传输数据。我已经完成了GUI界面,但是接收部分有问题。由于接收者不知道接收文件的确切时间,我写了一个无限循环,它是:

    recv=[];
    while (1)

        while s.BytesAvailable==0
        end

        a=fscanf(s);
        recv=[recv a]

    end
我怎样才能一直运行这个
for
循环,从程序的最开始直到用户关闭程序,并且用户仍然能够选择不同的数据来传输它


换言之;将任务分为两部分:始终运行的接收部分;虽然传输部分仅在用户希望传输数据时工作…

我不熟悉MATLAB,但在大多数编程环境中,您会让无限循环检查串行端口上的输入,然后处理GUI中的任何事件。如果更改内部
while
循环,则可以在
while(1)
中执行其他操作:


Matlab支持异步读取操作,在端口接收数据时触发。触发器可以是最小字节数或特殊字符

您必须使用回调属性

您可以使用
setappdata
getappdata
存储太大而无法放入端口输入缓冲区的数组。 假设您将主图形的句柄保存到名为
hfig
的变量中:

在主gui代码中:

s.BytesAvailableFcnCount = 1 ;                           %// number of byte to receive before the callback is triggered
s.BytesAvailableFcnMode = 'byte' ;
s.BytesAvailableFcn = {@myCustomReceiveFunction,hfig} ;  %// function to execute when the 'ByteAvailable' event is triggered.

recv = [] ;                                              %// initialize the variable
setappdata( hfig , 'LargeReceivedPacket' , recv )        %// store it into APPDATA
作为一项单独的职能:

function myCustomReceiveFunction(hobj,evt,hfig)
    %// retrieve the variable in case you want to keep history data 
    %// (otherwise just initialize that to recv = [])
    recv = getappdata( hfig , 'LargeReceivedPacket' )

    %// now read all the input buffer until empty
    while hobj.BytesAvailable>0
        a=fscanf(hobj);
        recv=[recv a]
    end
    %// now do what you want with your received data
    %// this function will execute and return control to the main gui
    %// when terminated (when the input buffer is all read).
    %// it will be executed again each time the 'ByteAvailable' event is fired.
    %//
    %// if you did something with "recv" and want to save it, use setappdata again:
    setappdata( hfig , 'LargeReceivedPacket' , recv )        %// store it into APPDATA

您可以查看这篇奇特的文章:当您处理少量数据时,这可能会起作用。但是在我的例子中,我正在模拟Aloha协议,因此;我实际上在处理数千个字节。即使是InputBufferSize和OutputBufferSize也无法获取这些数据。。。因此,我必须先将其码垛,然后开始传输。@AbdulmalekNaes,您可以调整输入和输出缓冲区大小,还可以在触发
BytesAvailable
事件之前调整要接收的字节数(或特殊字符)。当然,如果流很长(或是永久性的),则必须在多个过程中进行处理。我不确定我是否理解你的“码垛”概念。谢谢。。。我的意思是“打包”;例如,将900万字节划分为数据包。每一个都有5万字节。。。是的,我知道我可以调整缓冲区大小,但你放的越多,出现的错误和超时的次数就越多。如果你的输入是~9Mb,你可以在chuncks中读取它,我更新了代码以向你展示如何操作。Hoki。。。我不知道如何感谢你。。但我有一个问题;s.BytesAvailableFcn={@myCustomReceiveFunction,hfig}中的“hfig”是什么意思??
function myCustomReceiveFunction(hobj,evt,hfig)
    %// retrieve the variable in case you want to keep history data 
    %// (otherwise just initialize that to recv = [])
    recv = getappdata( hfig , 'LargeReceivedPacket' )

    %// now read all the input buffer until empty
    while hobj.BytesAvailable>0
        a=fscanf(hobj);
        recv=[recv a]
    end
    %// now do what you want with your received data
    %// this function will execute and return control to the main gui
    %// when terminated (when the input buffer is all read).
    %// it will be executed again each time the 'ByteAvailable' event is fired.
    %//
    %// if you did something with "recv" and want to save it, use setappdata again:
    setappdata( hfig , 'LargeReceivedPacket' , recv )        %// store it into APPDATA