How to set Query Instrument Block parameters in proper format?

6 views (last 30 days)
I'm trying to remote control Keysight DC Electronic Load N3300A with Simulink via GPIB interface, and use GPIB-USB-HS+ board from NI to connect the instrument with my PC. I want to set the instrument to be in CC mode and sink 1.25A of current, and then send the measured current data back to my Simulink model. There is no problem that I control it with sending command through Test & Measuring Tool or run in Matlab script, Here are the commands in Matlab scrip:
C1 = gpib('ni',0,3); % Board index=0, Address=3
C1.EOSMode = 'read&write';
C1.EOSCharCode = 'LF';
fprintf(C1,'CHAN 2'); % Using channel 2
fprintf(C1,'INPUT OFF');
fprintf(C1,'FUNC CURR'); % Set CC mode
fprintf(C1,'CURR:RANG MIN'); % Set low current range
fprintf(C1,'CURR 1.25'); % Set current sink to be 1.25A
fprintf(C1,'INPUT ON');
fprintf(C1,'MEAS:CURR?'); % mearsure the input current
CurData = fscanf(C1)
clear C1;
CurVal = str2double(CurData);
And thus I use the same settings and commands in Query Instrument Block in Simulink, but there is no response from the instrument. I am not sure the parameters I set in the block are in correct format, here are my settings:
 Block Sample Time: 1
Hardware Configuration
 Timeout: 10
 Buffer Size: 512
 Interface: GPIB
 Board Vendor: National Instruments
 Board index: 0
 Primary Address: 3
 Secondary Address: 0
Instrument Initialization
 Send string: 'CHAN 2 ; INPUT OFF ; FUNC CURR ; CURR:RANG MIN ; CURR 1.25 ; :INPUT ON'
 *I had also tried selecting Execute function, but not sure if I have to type fprintf or fprintf('CHAN 2 ; INPUT OFF ; FUNC CURR ; CURR:RANG MIN ; CURR 1.25 ; :INPUT ON').
 Query command: 'MEAS:CURR?'
 Data format: ASCII
 ASCII format string: '%c' (I'd also tried '%1.3f')
 Byte order: Little Endian
Checked "Remove any additional bytes from input buffer"
After initial response: Repeat query for new data
In addition, since I can obtain the instrument data via Matlab script and store it in workspace, is there any method to run a m-file in each time step in Simulink? I would like to import the measured data into my Simulink model and apply Simulink Destop Real-Time to do the real-time processing. However, I can't write the commands into Matlab Function Block because it exists an error "The 'gpib' class does not support code generation." and also don't know if callbacks can run the script automatically in every time step.
Any tips about setting Query Instrument Block or importing data from m-file would be greatly appreciated!

Answers (2)

Jan Houska
Jan Houska on 18 Apr 2021
Hi Joanne,
you cannot use gpib object with SImulink Desktop Real-Time in External mode because it does not support code generation. However, you can still use the object with SImulink Desktop Real-Time in Normal mode. To do that, please put the Real-Time Sync block into your model and run the model from the SIMULATION toolstrip tab (as opposed to the DESKTOP REAL-TIME tab).
Good Luck, Jan
  1 Comment
Pin An Lin
Pin An Lin on 19 Apr 2021
Hi Jan,
Thank you for the reply. I know the gpib object can be only used in normal and accelerator mode. And I haven't applied Real-Time Sync block because I would like to first comfirm my PC can obtain the correct response via Query Instrument block. However, I didn't get any response from the instrument and always get 0A even there is no real-time processing.

Sign in to comment.

Andrei Ursache
Andrei Ursache on 29 Apr 2021
If you're using the gpib code within a MATLAB Function block, since it does not support code generation you need to declare gpib (and the gpib related functions you're using, i.e. fopen, etc.) with coder.extrinsic ( ). Within a MATLAB Function block for setting/getting gbib property values you would also need to use the set and get functions rather than dot notation.
Another option would be to call your MATLAB function via an Interpreted MATLAB Function block ( ).
For troubleshooting the behavior you are seeing with the Query Instrument block, consider contacting MathWorks support.


Find more on Direct Interface Communication in Simulink in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!