Using UDP matlab to transmit data with VS real-time communication

Source: Internet
Author: User

My first blog, in this has been the help of everyone, but also hope that they can learn to pass on to those in need.

Recently in doing will vs and MATLAB for real-time transmission of data, mainly using UDP. Why UDP, and the benefits of UDP are no longer in the details here, you want to see the following references to the blog. The following is a separate vs. C # program and MATLAB program.

(1) Implementation of C # UDP communication

Main reference http://blog.csdn.net/ecocn/article/details/8278926

There is a very detailed, comprehensive introduction of UDP, as well as a C # form function of the source code, I ran, no errors. Interested in the words can be down to run a, of course, this time need a udpclient end, recommend the use of a sockettool software, you can refer to Socckettool use method, create a UDP client can communicate with the C # program. Here on the interface of the form function of VS, where 127.0.0.1 represents the IP address of this machine, and 172.19.3.15 is the IPv4 address of this machine, so it can be used as the local address, where the default port is 8888, you can modify it by clicking the Modify button.


(2) Implementation of UDP communication in MATLAB (GUI)

Considering that the operation will be more convenient, so the GUI in MATLAB to achieve this part of the function. The GUI interface looks like this:

Because I need real-time communication, that is, vs and MATLAB real-time transmission of data, so in the MATLAB UDP part of the need to monitor the other side of the real-time data sent, you can use two programs:

First, use the timer function. Second, the use of UDP callback function. Both of these methods I have tried, can be used, but given that my current project needs to be relatively strong real-time, and the timer function is not appropriate, so here the use of UDP callback function. The UDP callback function has two BYTEAVAILABLEFCN and DATAGRAMRECEIVEDFCN, the former is suitable for serial, TCP/IP, UDP and so on, the latter is only suitable for UDP

, the use of the same method is the same, the latter used here.

Main code snippet:

The OPENINGFCN part of the GUI:

function UDPCALL_OPENINGFCN (hobject, EventData, handles, varargin)
handles.output = hobject;
% send data at the end of the parameter set
ipmat= ' 127.0.0.1 ';% Local data end IP and port number, local
portmat=20000; 
ipvs= ' 127.0.0.1 '; % receive data end of IP and port number, remote
portvs=30000;
Set (Handles.localip, ' string ', Num2str (Ipmat));
Set (Handles.remoteip, ' string ', Num2str (Ipvs));
Set (Handles.remoteport, ' string ', Num2str (Portvs));
Set (Handles.localport, ' string ', Num2str (Portmat));
Set (Handles.receive_edit1, ' string ', ');
Global Count Num1 abnorm;
HANDLES.UDPTRANSMIT=UDP (Ipvs,portvs, ' LocalPort ', portmat);%mat sent to vs
fopen (handles.udptransmit);% open UDP
Handles.udpTransmit.ReadAsyncMode = ' continuous ';% asynchronous communication, sequential read serial data
set (Handles.udptransmit, ' Datagramterminatemode ', ' on ');
handles.udptransmit.datagramreceivedfcn={@updateUDP, handles};
Guidata (hobject, handles);


Udpcallback function Section:

function updateudp (obj,event,handles)%UDP callback functions
T1=clock;
Global Total  count  num2 num1;
If handles.udpTransmit.BytesAvailable = = 0 return end
%%%%%%%%%%%%%%%%% read display part%%%%%%%%%%%%%%%%%%
STRDATA=FSCANF (handles.udptransmit);% text to read vs sent data
Strtime=datestr (now, ' HH:MM:SS ');% Display time seconds
strtime=[ strtime,10];% Add newline character ten
strdata1=[strdata,10];
DISPDATA=[STRTIME,STRDATA1];
Set (Handles.receive_edit1, ' string ', dispdata);% Displays time and data
datanum=str2num (strdata);% converts text type to double type
%%% Add the action you want%%%


etime (clock,t1)% to view the time cost of each data transfer

Turn off the callback function of a function

function Closebutton_callback (hobject, EventData, handles)
fclose (handles.udptransmit);
Delete (handles.udptransmit);
Clear Ipmat Portmat Ipvs Portvs;
Close ();% closes GUI window

With this change can be used for the communication between MATLAB and VS, the interface is as follows:




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.