serial port event in MATLAB GUI.

5 views (last 30 days)
Vincent
Vincent on 19 May 2013
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)

Walter Roberson
Walter Roberson on 19 May 2013
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()

Categories

Find more on Migrate GUIDE Apps 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!