Help with read/readline with serialport object for NanoVNA V2_2

8 views (last 30 days)
Dear everyone,
I found it to be very confusing to make the NanoVNA V2_2 to work with MATLAB.
On Matlab, a serialport object is to be used for serial port communication.
I am quite certain that I can send commands to the NanoVNA V2_2 like sweepStartHz, sweepStepHz, and swepPoints followed by the respective number, and the device responds to these commands by going into USB Mode.
The problem comes when I try to read the data from the device. I am just not able to do so. The MATLAB has commands like read and readline but how to use them with the NanoVNA V2_2? Is it the limited Matlab functionality of something else? I just cannot understand.
The code that works so far in my opinion is as follows:
port = 'COM4';
BaudRate = 9600;
device = serialport(port, BaudRate);
start = 100e6;
stop = 2e9;
points = 101;
step = (stop-start)/(points);
writeline(device,"sweepPoints"+ num2str(typecast(points,'uint16')));
writeline(device,"sweepStartHz"+ num2str(typecast(step,'uint64')));
writeline(device,"sweepStepHz"+ num2str(typecast(step,'uint64')));

Answers (1)

Walter Roberson
Walter Roberson on 13 Jul 2022
readline(device) possibly followed by using sscanf() to convert text into double.
  2 Comments
Amrit Zoad
Amrit Zoad on 14 Jul 2022
Edited: Amrit Zoad on 14 Jul 2022
I should have added this in the question itself.
readline gives a timeout error no matter how longer timeout you set.
readline(device)
Warning: The specified amount of data was not returned within the Timeout period for 'readline'.
'serialport' unable to read any data. For more information on possible reasons, see serialport Read Warnings.
ans =
[]
To further add. If I add the configureTerminator after defining the serialport object.
configureTerminator(device,"CR/LF");
The the NumBytesAvailable in serialport object shows to be 1.
Then if I use the following read command, the answer shows to be 50 and the NumBytesAvailable turns to 0.
read(device,1,'uint8')
ans =
50
Walter Roberson
Walter Roberson on 14 Jul 2022
Edited: Walter Roberson on 13 Feb 2024
Host to device command list shows that the commands are binary, not text, and are sent without separation. The command 0x0d is INDICATE (basically, test the link) and the response is always 0x32. Translating... when you send carriage return (0x0d) the device responds with the character '2' (0x32) which is decimal 50

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!