使用 MATLAB 与外部设备的 GPIB 连接
有没有办法使用 MATLAB 建立 GPIB 连接,而无需仪器控制工具箱? (我没有)。还有一种方法可以让 MATLAB 了解外部设备的 RS232 参数值(波特率、停止位等)。对于 RS232 连接,我有以下代码:
% This function is meant to send commands to Potentiostat Model 263A.
% A run includes turning the cell on, reading current for time t1, turning
% the cell off, waiting for time t2.
% t1 is the duration [secs] for which the Potentiostat must run (cell is on)
% t2 is the duration [secs] to on after off
% n is the number of runs
% port is the serial port name such as COM1
function [s] = Potentiostat_control(t1,t2,n)
port = input('type port name such as COM1', 's')
s = serial(port);
set(s,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'even', 'StopBits', 2 ,'Terminator', 'CR/LF');
fopen(s)
%fprintf(s,'RS232?')
disp(['Total runs requested = ' num2str(n)])
disp('i denotes number of runs executed so far..');
for i=1:n
i
%data1 = query(s, '*IDN?')
fprintf(s,'%s','CELL 1'); % sends the command 'CELL 1'
%fprintf(s,'%s','READI');
pause(t1);
fprintf(s,'%s','CELL 0');
%fprintf(s,'%s','CLEAR');
pause(t2);
end
fclose(s)
Is there a way to establish a GPIB connection using MATLAB without the instrument control Tool box? (I don't have it). Also is there a way for MATLAB to know what the external device's RS232 parameter values are ( Baud rate, stop bit etc..). For the RS232 connection I have the following code:
% This function is meant to send commands to Potentiostat Model 263A.
% A run includes turning the cell on, reading current for time t1, turning
% the cell off, waiting for time t2.
% t1 is the duration [secs] for which the Potentiostat must run (cell is on)
% t2 is the duration [secs] to on after off
% n is the number of runs
% port is the serial port name such as COM1
function [s] = Potentiostat_control(t1,t2,n)
port = input('type port name such as COM1', 's')
s = serial(port);
set(s,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'even', 'StopBits', 2 ,'Terminator', 'CR/LF');
fopen(s)
%fprintf(s,'RS232?')
disp(['Total runs requested = ' num2str(n)])
disp('i denotes number of runs executed so far..');
for i=1:n
i
%data1 = query(s, '*IDN?')
fprintf(s,'%s','CELL 1'); % sends the command 'CELL 1'
%fprintf(s,'%s','READI');
pause(t1);
fprintf(s,'%s','CELL 0');
%fprintf(s,'%s','CLEAR');
pause(t2);
end
fclose(s)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于您的 GPIB 问题,GPIB 卡是否附带可调用库(如果您使用的是 Windows,则为 DLL)? Matlab 有一个调用外部库的接口。基本过程是让 Matlab 使用
LOADLIBRARY
解析头文件,然后使用LIBFUNCTIONS
查看可用函数并使用CALLLIB
调用函数。对于您的RS232问题,我认为在没有外部文档的情况下,主机端没有任何方法可以了解设备端的参数。
For your GPIB question, does the GPIB card come with a callable library (DLL if you're on Windows)? Matlab has an interface for calling external libraries. The basic procedure is to have Matlab parse the header file using
LOADLIBRARY
, then view the available functions usingLIBFUNCTIONS
and call functions usingCALLLIB
.For your RS232 question, I don't think there's any way for the host side to know the device side's parameters without external documentation.
我使用的是 National Instruments VISA 和 NI 488.2。
首先确保您在 NI-VISA 设置中检查了
VisaNS.NET API
,请参见下图:我正在使用
NationalInstruments.VisaNS。 MessageBasedSession
通过 MATLAB 的 .NET 接口。我编写了以下 MATLAB 类,将 NI VISA 包装到 MATLAB:
我还添加了一些方法来处理 SRQ 请求。
例如,您可以使用以下代码控制 GPIB 仪器:
MessageBasedSession
可用于通过 GPIB、以太网或 USB 与您的仪器进行通信。另请参阅https://stackoverflow.com/a/49388678/7556646。
I'm using National Instruments VISA and NI 488.2.
First make sure that you checked the
VisaNS.NET API
in the NI-VISA Setup, see the following figure:I'm using the
NationalInstruments.VisaNS.MessageBasedSession
over the .NET interface from MATLAB.I've written the following MATLAB class that wraps NI VISA to MATLAB:
I've added as well some methods to handle the SRQ request.
With the following code you can control GPIB instrument for example:
A
MessageBasedSession
can be used to communicate with your instrument over GPIB, Ethernet or USB.See as well https://stackoverflow.com/a/49388678/7556646.
我不知道RS232参数,但是对于带有tcpip的仪器,您可以非常轻松地发送SCPI命令。
以下是我向罗德与施瓦茨仪器发送 SCPI 命令的示例。无需 VISA 或 IVI。使用端口 5025。
完成后关闭连接:
I don't know about the RS232 parameters, but for an instrument with tcpip, you can send SCPI commands really easy.
Here is an example where I send a SCPI command to a Rohde&Schwarz instuments. No VISA or IVI needed. Use port 5025.
Then close the connection when you are done: