MATLAB Answers

Petey
1

VISA Serial Read - Unsuccessful read: A timeout occurred before the Terminator was reached.

Asked by Petey
on 14 Mar 2017
Latest activity Commented on by Petey
on 30 May 2017

I am trying to interact with stepper motor controllers using VISA serial communications. I have NI-DAQmx and NI-VISA support packages installed within MATLAB R2016a. Serial writes seem to function perfectly; however, serial reads issue a timeout warning (“Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.”) and always take at least 1 second. Surprisingly, the serial read always retrieves the expected data. I have already have tried shortening (down to 1 ms) and lengthening (up to 10 s) the timeout, implementing a pause before reading the serial data (to give the controller time to process my request and put data onto the serial bus), and tried changing the read terminators to every possible ASCII character. The last characters in every data read are always (ASCII): EXT, CR and LF. Bottom line is that serial read kind of works, but always has a timeout warning and takes too long (at least 1 second). My code is attached. Any help or advice would be much appreciated!

% Serial settings
vs = visa('ni', 'ASRL3::INSTR'); % Create VISA serial object
vs.BaudRate = 9600; % Rate of transmission [bit/s]
vs.DataBits = 8; % Number of data bits
vs.Parity = 'none'; % No parity
vs.FlowControl = 'none'; % No flow control
vs.StopBits = 1; % Number of stop bits
vs.Terminator = {'LF', 'CR'}; % Read and write terminator
set(vs, 'Timeout', 0.05); % Set timeout [s]
query_cmds = '/1?8R'; % String to request encoder position
% Send serial commands to controllers
fopen(vs); % Open serial port
pause(.010) % Time delay [s]
disp('Query Commands Sent to Controllers')
disp(query_cmds)
fprintf(vs, query_cmds); % Ask controllers for encoder position
tic
query_resp = fscanf(vs);
toc
fclose(vs); % Close serial port

  0 Comments

Sign in to comment.

1 Answer

Answer by Vinod
on 20 Mar 2017
 Accepted Answer

What you are seeing is normal. Please check the Rules for completing a read operation with fscanf. You may also want to look at the documentation for FREAD if that is what you are after.

  1 Comment

The trick seemed to be replacing fscanf() with fread() and knowing the exact number of bytes expected. For example,
bytes_expect = 12; % Number of bytes expected
query_resp = fread(vs, bytes_expect, 'char')'; % Receive response from controller
Note that the transpose puts the output of fread() in the form of a character row vector. Now, serial communications occur on the order of 10 ms or so.

Sign in to comment.