serial port event in MATLAB GUI.

I want to update a textbox everytime i receive data from the serial port.
here's my code.
function btnConnect_Callback(hObject, eventdata, handles)
global serialConnection;
portNum = get(handles.lstSerialPorts, 'Value');
portList = get(handles.lstSerialPorts, 'String');
serialConnection = serial(portList{portNum});
set(serialConnection, 'BytesAvailableFcnMode', 'byte');
set(serialConnection, 'BytesAvailableFcnCount', 28);
serialConnection.BytesAvailableFcn = {@serialEventHandler,hObject,handles,28};
fopen(serialConnection);
function serialEventHandler(hObject, handles, bytesToRead)
global serialConnection;
data = fread(serialConnection, bytesToRead);
set(handles.txtTRY, 'String', data);
guidata(hObject,handles);
However, it always gives an error:
??? Error using ==> MAINMENU>serialEventHandler Too many input arguments.
Error in ==> instrcb at 36 feval(val{1}, obj, eventStruct, val{2:end});
What should i do? thanks in advance.

Answers (1)

Event handlers automatically have the object and event added to the beginning of the arguments used to call. So use
serialConnection.BytesAvailableFcn = {@serialEventHandler,handles,28};
and
function serialEventHandler(hObject, event, handles, bytesToRead)
Note: since you are not modifying what is stored in "handles" itself, only using set() to modify something that "handles" refers to, you do not need to guidata(hObject, handles) after the set()

Asked:

on 19 May 2013

Community Treasure Hunt

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

Start Hunting!