Error when trying to read data from serialport

Hello!
I have a serialport connected, like this:
s =
Serialport with properties:
Port: "COM9"
BaudRate: 115200
NumBytesAvailable: 950
The data I gather from here looks like this:
>> readline(s)
ans =
"*RRN:00000000C76D,00000000C6F1,0,000651,0004,-59"
This device is receiving that kind of data every second or so (it´s not fixed). So "NumBytesAvailable" increases every time (I believe it is 50 Bytes every line). I want to read every of these lines, every time there is a new one. But I don´t know how to use "Callback".
When Callback is on, I have these error (increasing errors every time there is new data in terminator)
>> configureCallback(s,"terminator",@callbackFcn)
Warning: Error occurred while executing the listener callback for event DataWritten defined for class asyncio.InputStream:
Undefined function 'callbackFcn' for input arguments of type 'internal.Serialport'.
Error in internal.Serialport/callbackFunction (line 1353)
obj.BytesAvailableFcn(obj, dataAvailableInfo);
Error in internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 958)
obj.StringClient.StringReadFcn = @obj.callbackFunction;
Error in matlabshared.transportclients.internal.StringClient.StringClient/DataAvailableCallback
Error in matlabshared.transportclients.internal.StringClient.StringClient>@(varargin)obj.DataAvailableCallback(varargin{:})
Error in matlabshared.seriallib.internal.Serial/onDataReceived
Error in matlabshared.seriallib.internal.Serial>@(varargin)obj.onDataReceived(varargin{:})
Error in asyncio.Channel/onDataReceived (line 487)
notify(obj.InputStream, 'DataWritten', ...
Error in asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)
@(source, data) obj.onDataReceived());
> In asyncio/Channel/onDataReceived (line 487)
In asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)
Warning: Error occurred while executing the listener callback for event DataWritten defined for class asyncio.InputStream:
Undefined function 'callbackFcn' for input arguments of type 'internal.Serialport'.
Error in internal.Serialport/callbackFunction (line 1353)
obj.BytesAvailableFcn(obj, dataAvailableInfo);
Error in internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 958)
obj.StringClient.StringReadFcn = @obj.callbackFunction;
Error in matlabshared.transportclients.internal.StringClient.StringClient/DataAvailableCallback
Error in matlabshared.transportclients.internal.StringClient.StringClient>@(varargin)obj.DataAvailableCallback(varargin{:})
Error in matlabshared.seriallib.internal.Serial/onDataReceived
Error in matlabshared.seriallib.internal.Serial>@(varargin)obj.onDataReceived(varargin{:})
Error in asyncio.Channel/onDataReceived (line 487)
notify(obj.InputStream, 'DataWritten', ...
Any help please? Thank you in advance

5 commentaires

Hi Javier,
it looks like the callback function "callbackFcn" does not exist or is not on the path. Perhaps you just copied parts of code from a generic example?
Have a look at this example in the documentation
You will find the actual body of a callback function that reads and displays one line of data whenever a terminator is read.
Hope this helps
Sebastian
Javier Mateo
Javier Mateo le 7 Jan 2021
Modifié(e) : Javier Mateo le 7 Jan 2021
Hi Sebastian, thank you very much again for your support.
I tried the function, but I am missing something or I do not know how to use it... I have never played with functions
(using "serialport" instead of src, does not work neither)
This is what I got:
Javier,
please save your above function to a file readSerialData.m and have a look at the rest of the steps in the example:
  • Create an instrument object — Create the serial port object s associated with serial port COM1.
s = serialport("COM1",9600);
  • Configure properties — Configure s to execute the callback function readSerialData when the terminator is read.
configureCallback(s,"terminator",@readSerialData)
  • Disconnect and clean up — Clear the objects from the MATLAB® workspace when you are done.
clear s
Yes, it worked flawlessly, thank you!
I can get the data in the command window. However, my goal is to play with this data and plot it in real time.
Could you please help me with that? I dont see the variable "data" in the workspace. I don´t know how to play with this data then
If I try to write any code below the readSerialData.m code, the callback terminator does not work anymore
you have to create whatver you want to plot within the function.

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by