Question #5873   Submitted by Answiki on 02/12/2022 at 07:36:22 AM UTC

How can I read the serial port continuously with Matlab?

Answer   Submitted by Answiki on 02/12/2022 at 07:33:27 AM UTC

To continuously read data on serial port in Matlab, the best option is to use the serialport object. Once the port is opened, read the data in a loop. In the following example, we'll assume data on serial port is a series of 3 integers separated by a space. Each series is separated a line feed (LF):

80 7 160
82 8 156
83 9 162
85 8 168
84 8 164
79 7 159
... 


The following Matlab code open the serial device, read each line and convert to matrix:

close all;
clear all;

% Open the serial device and set LF as terminal character
device =  serialport('COM1', 9600);
configureTerminator(device,"LF");

% Main loop
while (1)

    % Get data line by line
    line = readline(device);   
    data = sscanf(line, '%d')
end;


8 events in history
Question by Answiki 02/12/2022 at 07:36:22 AM
How can I read the serial port continuously with Matlab?
Question by Answiki 02/12/2022 at 07:35:43 AM
In Matlab, Is it possible to continuously read data from the serial port?
Question by Answiki 02/12/2022 at 07:35:04 AM
How to read COM port data continuously in Matlab?
Answer by Answiki on 02/12/2022 at 07:33:27 AM

To continuously read data on serial port in Matlab, the best option is to use the serialport object. Once the port is opened, read the data in a loop. In the following example, we'll assume data on serial port is a series of 3 integers separated by a space. Each series is separated a line feed (LF):

80 7 160
82 8 156
83 9 162
85 8 168
84 8 164
79 7 159
... 


The following Matlab code open the serial device, read each line and convert to matrix:

close all;
clear all;

% Open the serial device and set LF as terminal character
device =  serialport('COM1', 9600);
configureTerminator(device,"LF");

% Main loop
while (1)

    % Get data line by line
    line = readline(device);   
    data = sscanf(line, '%d')
end;


Answer by Answiki on 02/12/2022 at 07:32:30 AM

To continuously read data on serial port in Matlab, the best option is to use the serialport object. Once the port is opened, read the data in a loop. In the following example, we'll assume data on serial port is a series of 3 integers separated by a space. Each series is separated a line feed (LF):

80 7 160
82 8 156
83 9 162
85 8 168
84 8 164
79 7 159
... 

The following Matlab code open the serial device, read each line and convert to matrix:

close all;
clear all;

% Open the serial device and set LF as terminal character
device =  serialport('COM1', 9600);
configureTerminator(device,"LF");

% Main loop
while (1)

    % Get data line by line
    line = readline(device);   
    data = sscanf(line, '%d')
end;


Answer by Imabot on 02/11/2022 at 05:32:39 PM

In the following we'll assume data on serial port is a series of 3 integers separated by a space. Each series is separated a line feed (LF):

80 7 160
82 8 156
83 9 162
85 8 168
84 8 164
79 7 159
... 

The following Matlab code open the serial device, read each line and convert to matrix:

close all;
clear all;

device =  serialport('COM1', 9600);
configureTerminator(device,"LF");

while (1)
    line = readline(device);    
    data = sscanf(line, '%d')
end;


Answer by Imabot on 02/11/2022 at 05:31:40 PM

In the following we'll assume data on serial port is a series of 3 integers separated by a line feed (LF) :

80 7 160
82 8 156
83 9 162
85 8 168
84 8 164
79 7 159
... 

The following Matlab code open the serial device, read each line and convert to matrix:

close all;
clear all;

device =  serialport('COM1', 9600);
configureTerminator(device,"LF");

while (1)
    line = readline(device);    
    data = sscanf(line, '%d')
end;


Question by Imabot 02/11/2022 at 04:37:07 PM
How to continuously read data on serial port in Matlab?
# ID Query URL Count

Icons proudly provided by Friconix.