matlab tcpip代码

%创建
t = tcpip('sonytekawg.yourdomain.com',4000);



%创建缓存区
set(t,'OutputBufferSize',3000)



%连接
fopen(t)



%设置读写
set(t,'ByteOrder','littleEndian')



%生成发送数据
x = (0:499).*8*pi/500;
data = sin(x);
marker = zeros(length(data),1);
marker(1) = 3;


%告诉对方发送格式
fprintf(t,'%s',['MMEMORY:DATA "sin.wfm",#42544MAGIC 1000' 13 10])
fprintf(t,'%s','#42500')




%发送数据
for (i = 1:length(data)),
fwrite(t,data(i),'float32');
fwrite(t,marker(i));
end


%设置频率
fprintf(t,'%s',['CLOCK 1.0000000000e+008' 13 10 10])


%关闭
fclose(t)
set(t,'InputBufferSize',3000)




%重新打开
fopen(t)


%读取数据
fprintf(t,'MMEMORY:DATA? "sin.wfm" ')
data = fread(t,t.BytesAvailable);


%The next set of commands reads the same waveform as a float32 array. To begin, write the waveform to the AWG.
fprintf(t,'MMEMORY:DATA? "sin.wfm" ')


%Read the file header as ASCII characters.
header1 = fscanf(t)
% header1 =
%42544MAGIC 1000


%Read the next six bytes, which specify the length of data.
header2 = fscanf(t,'%s',6)
% header2 =
% #42500


%Read the waveform using float32 precision and read the markers using uint8 precision. Note that one float32 value consists of four bytes. Therefore, the following commands read 2500 bytes.
data = zeros(500,1);
marker = zeros(500,1);
for i = 1:500,
data(i) = fread(t,1,'float32');
marker(i) = fread(t,1,'uint8');
end


%Read the remaining data, which consists of clock information and termination characters.
clock = fscanf(t);
cleanup = fread(t,2);


%Disconnect and clean up — When you no longer need t, you should disconnect it from the host, and remove it from memory and from the MATLAB workspace.
fclose(t)
delete(t)
clear t
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值