matlab code to receive data from serial port

Matlab Code to Receive Data from a Serial Port

Spread the love

 

Matlab Code to Receive Data from a Serial Port

This tutorial explains you to receive data from serial port. Serial port communication is used in most of the embedded system to send and receive data. It plays a major role in Internt of Things and other embedded applications. This part of the code can be used to interface your PC running Matlab and it can be connected to other microcontroller,DSP, and FPGA through a serial port.

Step 1:To reset all the instrument objects

% Instrreset Disconnect and delete all instrument objects.
instrreset;

Step 2:To Construct a serial port objects

%Construct serial port object.
CreateSerialPortObject = serial('COM1');

Step 3:To set the serial port setting

In this step you have to configure the baudrate and other serail port settings like flow control,parity etc.

%Set Serial Port Settings
set(CreateSerialPortObject,'BaudRate',115200,'StopBits',1,'FlowControl','none','Parity','none',
'DataBits',8,'OutputBufferSize',50000);

Step 4:To set the Input Buffer size

%Create Buffer Size
CreateSerialPortObject.InputBufferSize=100;

Step 5:To set timeout period

%Time out, Wait till this time
CreateSerialPortObject.TimeOut=60;
CreateSerialPortObject.Terminator = '';

Step 6:To open the serial port which was created

%Open Serial Port Object
fopen(CreateSerialPortObject);

Step 7:To read the serial port which was opened

%Read theĀ  Serial Port Object
te = fread(CreateSerialPortObject,100);

Step 8:To Close the serial port

%Close Serial Port Object
fclose(instrfind);

Step 9:To Clear the Instrument objects

%Clear Instrument Objects
clear CreateSerialPortObject;