MATLAB Answers

Vincent

serial port event in MATLAB GUI.

Asked by 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.

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by 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()

  0 Comments


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today