GUI Serial Com interrupt

2 visualizaciones (últimos 30 días)
Daniel
Daniel el 18 de Abr. de 2016
Comentada: Daniel el 18 de Abr. de 2016
i have a GUI and a state machine to read the data i receive but at the moment it works when i press a button. i want it to work continuously using interrupt how can i do it? i can receive a data of 40 bytes and need to save it through the iterations how can i do it
to be more specific: here is my code.only the relevant part: where i set the serial port and the funcion i try to call when the INTERRUPT occurs
the error i see is:
Error using feval
Invalid function name '@intcon1'.
Error in instrcb (line 36)
feval(val{1}, obj, eventStruct, val{2:end});
Warning: The BytesAvailableFcn is being disabled. To enable the callback property
either connect to the hardware with FOPEN or set the BytesAvailableFcn property.
* _THE CODE:_*
function SELECT_COM_BUTTON_Callback(hObject, eventdata, handles)
%deactivate actual button
set(hObject,'Enable','off');pause(0.1)
serPortn = get(handles.port_list, 'Value');
if serPortn == 1
errordlg('Select valid COM port');
else
serList = get(handles.port_list,'String');
com = serList{serPortn};
end
contents = get(handles.baud_list,'String');
vel=str2double(contents{get(handles.baud_list,'Value')});
% serial port configuration
handles.SerPIC = serial(com);%choose serail COM
set(handles.SerPIC,'BaudRate',vel);%speed
set(handles.SerPIC,'DataBits',8);%8 bits of data
set(handles.SerPIC,'Parity','none');%no parity bits
set(handles.SerPIC,'StopBits',1);%one stop bit
set(handles.SerPIC,'FlowControl','none');%no flow control
handles.SerPIC.BytesAvailableFcn = {'@intcon1',handles};
set(handles.SerPIC,'BytesAvailableFcnMode','byte');
set(handles.SerPIC,'BytesAvailableFcnCount', 1);
fopen(handles.SerPIC);
%opening message of serial port (user needs to press ok)
warndlg(['Port ',com,' OPEN'],'RS_232')
% updates the handles structure (example: added pattern_1 to struct)
guidata(hObject,handles)
end
function intcon1(hObject, eventdata, handles)
persistent current_state
persistent L;
persistent i;
persistent DATA;
persistent recieved_checksum;
% current_state = 0;
% while (handles.SerPIC.BytesAvailable >= 1)
this_input = fread(handles.SerPIC,1);
switch current_state
case 0 % state 0 wait for HEADER_1
L=0;
i=0;
DATA=0;
recieved_checksum=0;
if (this_input == 85)
current_state = 1;
else
current_state = 0;
end
case 1 % state 1 wait for HEADER_2
if (this_input == 51) %was 170
current_state = 2;
recieved_checksum = 136;
elseif (this_input == 85)
current_state = 1;
else
current_state = 0;
end
case 2 % state 2 wait for message length
MESSAGE_LENGTH = this_input;
L=MESSAGE_LENGTH;
i=1;
current_state = 3;
recieved_checksum = mod(recieved_checksum + this_input,255);
case 3
% if (s.BytesAvailable == MESSAGE_LENGTH)
DATA(i) = this_input;
recieved_checksum = mod(recieved_checksum + this_input,255);
i=i+1;
L = L - 1;
if L > 0
current_state = 3;
else
current_state = 4;
end
case 4
recieved_checksum = recieved_checksum - this_input;
if (recieved_checksum ==0)
set(handles.temp_read,'string',[num2str(DATA(1))])
set(handles.voltage_read,'string',[num2str(DATA(2))])
set(handles.current_read,'string',[num2str(DATA(3))])
current_state = 0;
else
current_state = 0;
end
end
guidata(hObject,handles)
end
  1 comentario
Daniel
Daniel el 18 de Abr. de 2016
is it too messy? i can clean it up a bit if you need. to sum up my question is this: i want to read serial communication continuously but :handles.SerPIC.BytesAvailableFcn = {'@intcon1',handles}; does not seem to work and reference the interrupt to the matching funcion

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by