UDP Receive and Send Matlab

前端 未结 1 1613
醉酒成梦
醉酒成梦 2021-01-26 06:26

I am currently working on receiving packets of data from an external device and then ill be sending data to another device. I having a working Simulink model however i don\'t kn

相关标签:
1条回答
  • 2021-01-26 06:41

    This should work:

    clc();
    
    echoudp('on',25000);
    
    u = udp('127.0.0.1',25000);
    u.InputBufferSize = 10000;
    u.OutputBufferSize = 10000;
    u.ReadAsyncMode = 'continuous';
    u.BytesAvailableFcn = @BytesAvailable_Callback;
    u.BytesAvailableFcnMode = 'terminator';
    
    fopen(u);
    
    if (~strcmp(u.Status,'open'))
        NetworkError(u,'Connection failed!');
    end
    
    try
        for i = 1:10
            fprintf(u,'Hey!');
        end
    catch e
        NetworkError(u,['Communication failed! (' e.message ')']);
    end
    
    pause(1);
    
    NetworkDispose(u);
    
    function BytesAvailable_Callback(u,evt) %#ok<INUSD>
        data = fscanf(u);
        disp('Data Received!');
        disp(data);
    end
    
    function NetworkDispose(u)  
        fclose(u);
        delete(u);
        clear u;
    
        echoudp('off');
    end
    
    function NetworkError(u,msg)
        NetworkDispose(u);
        error(msg);
    end
    

    This sets up a continuous (and asynchronous) listening over UDP. For more information read this.

    0 讨论(0)
提交回复
热议问题