Error when trying to read data from serialport
Show older comments
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 Comments
Sebastian Bomberg
on 7 Jan 2021
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
on 7 Jan 2021
Edited: Javier Mateo
on 7 Jan 2021
Sebastian Bomberg
on 8 Jan 2021
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
Javier Mateo
on 8 Jan 2021
Samuel Olufemi Taiwo
on 21 Mar 2021
you have to create whatver you want to plot within the function.
Answers (0)
Categories
Find more on Serial and USB Communication in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!