How to read from a serial port in Matlab R2018a

26 views (last 30 days)
SHS
SHS on 9 Oct 2020
Answered: jq rq on 12 Nov 2020
I need to read and save the input data from a USB port. As I searched, first the serial port, as an object, should be defined:
- serialport (MatLab 2019 and later)
- serial (MatLab 2018 and older)
Then, using the "read" function, the port can be read. Although, the "read" function does not work for "serial":
- Undefined function 'read' for input arguments of type 'serial'.
How could I read the input data from a USB port in MatLab 2018a?

Answers (1)

jq rq
jq rq on 12 Nov 2020
Hi Shahin,
I've found something that works for me using older commands in R2019a.
Let's assume device like multimeter where we know port (or we can detect it), baudrate and we're periodically sending there command like tell me value, output is string (char).
clc; clear; close all
s = serial('COM3','BaudRate',230400);
fopen(s)
for i = 1:11
fprintf(s,"VAL?")
pause(1)
fgets(s) % <- output
end
fclose(s)
Hope this helped you!

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!