Question about serialport() vs serial() on different versions on Matlab

6 views (last 30 days)
I am trying to communicate with a HIOKI LCR device. I have a sample program from HIOKI a part of the code is attached.
Now this program is written in Matlab version 2019a, I have version 2023b.
It works fine on older Matlab version(I tested it on matlab 2019b), but there is no communication with the HIOKI on the newest version of Matlab. I know that from version 2019b serial is being replaced with serialport. So i have tried altering the code.
% (0)Class variable
properties (Access = public)
SerialPort = ''; % Serial Port Device
MsgBuf = ''; % Received Data
ReceiveTimeout = 0; % Receive-Timeout [ms]
end
properties (Access = private)
ofs = 0; %Description
end
methods (Access = public)
% (1)Connect
function ret = OpenInterface(app,port,speed,timeout)
ret = false;
try
clear app.SerialPort
app.ReceiveTimeout = timeout;
app.SerialPort = serial(port); % Create a serial port object/Set the COM port
app.SerialPort.timeout = timeout;
set(app.SerialPort,'BaudRate',speed); % Set communication speed
set(app.SerialPort,'Terminator','CR/LF'); % Set the terminator
app.SerialPort.InputBufferSize = 1E6;
fopen(app.SerialPort); % Open the serial port
app.SendButton.Enable = true; % Enable the Transmit button
app.Timeout.Editable = false; % Uneditable after the connection is established
app.COM.Editable = false;
app.Speed.Editable = false;
ret = true;
catch
msgbox ("Err_OpenInterface")
ret = false;
end
end
% (2) Disconnect
function ret = CloseInterface(app)
ret = false;
try
fclose(app.SerialPort); % Close the serial port
delete(app.SerialPort) % Dump the serial port object
clear app.SerialPort
app.SendButton.Enable = false; % Disable the Transmit button
app.Timeout.Editable = true; % Editable after disconnection
app.COM.Editable = true;
app.Speed.Editable = true;
ret = true;
catch
msgbox ("Err_CloseInterface")
ret = false;
end
end
% (3)Send commands
function ret = SendMsg(app, strMsg)
ret = false;
try
strMsg = (strMsg); % Add a terminator, CR+LR, to transmitted commands: Additional settings are not required since terminator characters have already been set
fprintf(app.SerialPort, strMsg); % Write data in the transmit buffer
ret = true;
catch
msgbox ("Err_SendMsg")
ret = false;
end
end
% (4)Receive
function ret = ReceiveMsg(app, timeout)
ret = false;
app.MsgBuf = ""; % Clear received data
tic; % Start a stopwatch
try
% Continue the loop until LF is received
while(1)
rcv = fscanf(app.SerialPort); % Read data from the receive buffer
if ~isempty(rcv)
rcv = strrep(char(rcv),char(uint8(13)),""); % Delete CR in received data
if strfind(rcv,uint8(10)) > 0 % End the loop when LF is received
buf = char(rcv);
rcv = buf(1:strfind(rcv,uint8(10)) - 1); % Extract data without LF and the following from the original received data
app.MsgBuf = strcat(app.MsgBuf,rcv); % Save the data
break
else
app.MsgBuf = [app.MsgBuf rcv]; % Save the data
end
end
ew = toc; % Get elapsed time
if ew > timeout
app.MsgBuf = "Timeout";
MsgBox (app.MsgBuf)
return
end
end
ret = true;
catch
msgbox ("Err_ReceiveMsg")
ret = false;
end
end
% (5)Transmit and receive commands
function ret = SendQueryMsg(app, strMsg, timeout)
ret = false;
ret = SendMsg(app, strMsg); % Transmit commands
if ret
ret = ReceiveMsg(app, timeout); % Receive response when command transmission is succeeded
end
end
end
I changed the orignal code that uses the old serial() functions to this (i used this: https://nl.mathworks.com/help/matlab/import_export/transition-your-code-to-serialport-interface.html#mw_61890a86-a984-4f4d-9800-edb569a585b8 to help me):
function ret = OpenInterface(app,port,speed,timeout)
ret = false;
try
app.ReceiveTimeout = timeout;
app.SerialPort = serialport(port,speed); % Create a serial port object/Set the COM port
configureTerminator(app.SerialPort,"CR/LF") % Set communication speed
% Set the terminator
app.SendButton.Enable = true; % Enable the Transmit button
app.Timeout.Editable = false; % Uneditable after the connection is established
app.COM.Editable = false;
app.Speed.Editable = false;
ret = true;
catch
msgbox ("Err_OpenInterface")
ret = false;
end
end
......
% Continue the loop until LF is received
while(1)
rcv = readline(app.SerialPort);
if ~isempty(rcv)
rcv = strrep(char(rcv),char(uint8(13)),""); % Delete CR in received data
if strfind(rcv,uint8(10)) > 0
I have 2 questions:
1)Why does the original code not work in newer versions of Matlab?
2)Are the alterations I did correct to be compatible with serialport() instead of serial()?

Answers (1)

Shivam Lahoti
Shivam Lahoti on 10 Mar 2025
Hi Sebastiaan,
I can see the 'serial' object is not recomended as mentioned in the following documentation:
The steps taken be you are logical. Below is a detailed answer for your queries:
1) Why does the original code not work in newer versions of MATLAB?
Although the `serial` object was not deprecated in MATLAB R2019b, it was marked as not recommended for use, with the introduction of the `serialport` object offering a more modern and efficient interface for serial communication. The `serialport` object provides enhanced performance and additional features, necessitating modifications to existing code that uses the older `serial` object. While `serial` is still available in versions after R2019b, transitioning to `serialport` is advisable for improved functionality and future compatibility.
2) Are the alterations I did correct to be compatible with serialport() instead of serial()?
Your modifications are mostly correct, but a few adjustments can enhance compatibility and performance. When using the 'serialport' object, it's important to note that the 'Timeout' property is specified in seconds, unlike the previous serial object, which used milliseconds, so you should adjust the timeout value accordingly. Additionally, the 'readline' function in 'serialport' simplifies data reading by automatically reading until it encounters a line terminator, eliminating the need for manual terminator handling as required in older implementations. Furthermore, implementing proper error handling is essential to catch any issues during serial communication, ensuring the robustness of your application.
I hope this helps clarify your query.
Regards,
Shivam.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!