通过Matlab将任意波形上传到DS345

时间:2019-07-18 11:24:41

标签: matlab device

我很难通过Matlab中的Instrument Control Toolbox将任意波形上传到DS345函数发生器。

我已使用here提供的手册(请参阅第3-8页)尝试实现此目的。我使用的代码是:

%% Instrument Control of DS345 Signal Generator
clear all
clc
COMtype = 'COM4';
% Find the a serial port object
obj1 = instrfind('Type', 'serial', 'Port', COMtype , 'Tag', '');

% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
   obj1 = serial(COMtype);
else
   fclose(obj1);
   obj1 = obj1(1);
end

N = 512;
Sum = 0;
number = 10;
data = zeros(number,1);
X = zeros(number,1);
Y = zeros(number,1);

obj1.OutputBufferSize = N;

fopen(obj1);

checkConnection =strcat( query(obj1,'*IDN?') );

if strcmp(checkConnection,'StanfordResearchSystems,DS345,43862,1.04') ~=1
   disp(['Device Connection not working... Query msg ' ...
           checkConnection])
else
   disp(['Connection to ' checkConnection(25:29) ' established.'])
end


% Generating Function

for i = 1:number
   X(i) = 1*i;
   Y(i) = 0;
   data(2*i-1) = i;
   data(2*i) = 0; %8*i*(-1 + 2*(mod(i,2)));
   Sum = Sum + (data(2*i-1) + data(2*i));
end


figure(1)
   plot(X,Y)
   grid minor
checksumdata = sum(data);

data(2*number+1) = checksumdata;
% size(data)
% convert into 16-bit binary array
data = int16(data);
dataBin = dec2bin(data,16);


checkLDWF = strcat(query(obj1,['LDWF? 1,' num2str(2*number)]));
if checkLDWF =='1'
   disp('Ready to Download...')
end
pause(1)

disp('downloading...')
disp('sending DataBin...')
fwrite(obj1,dataBin,'short','async')
stopasync(obj1)

fclose(obj1);

在代码中,我生成了一个直线数组,并且想要上传任意矢量波形,这意味着使用 LDWF 命令。 该手册建议我应该发送16位二进制数据,然后发送校验和,该校验和只是数据数组的总和。

我得到的错误是“ LOADING ERROR ...”(加载错误...),这表明它一直在等待10秒钟的数据流,但什么也没有到达。因此它会因“加载错误”而超时。将不胜感激任何帮助/指导。

1 个答案:

答案 0 :(得分:0)

我没有发现您的代码有任何问题。

很遗憾,我目前无法访问Matlab。

同时,您可能想看看我提出的here的方法。

当您将数据从Matlab发送到函数生成器时,它将使您看到端口上的实际内容。这样,您可以确定要发送的数据正确无误,尤其是在发送正确数量的数据时。