基于matlab的以太网通信-韦志恒20120911
--------------------------------------------------------
matlab的instrument control toolbox里面具有用于串口、以太网通信的函数。
在iCT的user guide里面有tcpip的详细说明。
TCP/IP Object
You create a TCP/IP object with the tcpipfunction. tcpiprequires
the name
of the remote host as an input argument. In most cases, you need to
specify
the remote port value. If you do not specify the remote port, then
80 is used.
As described in “Configuring Properties During Object Creation” on
page 3-3,
you can also configure property values during object
creation.
Each TCP/IP object is associated with one instrument. For example,
to create
a TCP/IP object for a Sony/Tektronix AWG520 Arbitrary Waveform
Generator,
//似乎matlab的tcpip只能用作client,而外设必须设作server。
tcpip这个函数的参数就是要连接的server的ip和端口值。
t = tcpip('sonytekawg.yourdomain.com',4000);
Note that the port number is fixedand is found in the
instrument’s
documentation.
=========================================================
下面是一个完整的历程:
1 Create and configure an
instrument object— First you create a TCP/IP
object in the MATLAB workspace. Port 80is the standard port for
Web
servers.
t = tcpip('www.rfc-editor.org', 80);
By default, the TCP/IP object has an InputBufferSizeof 512
,whichmeansit
can only read 512 bytes at a time. The MathWorks Web page data is
much
greater than 512 bytes, so you need to set a larger value for this
property.
set(t, 'InputBufferSize', 30000);
2 Connect the object — Next,
you open the connection to the server. If the
server is not present or is not accepting connections you would get
an error
here.
fopen(t);
3 Write and read data— You can
now communicate with the server using
the functions fprintf, fscanf , fwrite ,and fread.
To ask a Web server to send a Web page, you use the GET
command.
You can ask for a text file from the RFC Editor Web site using
'GET
(path/filename)' .
fprintf(t, 'GET /rfc/rfc793.txt');
The server receives the command and sends back the Web page. You
can
see if any data was sent back by looking at the BytesAvailable
property
of the object.
get(t, 'BytesAvailable')
Now you can start to read the Web page data. By default,fscanf
reads one
line at a time. You can read lines of data until the BytesAvailable
value is
0 . Note that you will not see a rendered web page; the HTML file
data will
scroll by on the screen.
while (get(t, 'BytesAvailable') > 0)
A = fscanf(t),
end
4 Disconnect and clean up — If
you want to do more communication, you
can continue to read and write data here. If you are done with the
object,
close it and delete it.
fclose(t);
delete(t);
clear t
==========================================================================
20120911 今后有时间再添加详细的使用说明。韦志恒。