Why does this function time-out?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Although I clear COM4, it doesn't respond (although sometimes after reboot). I have included the code, and the output. What is my problem? (thanks)
=========== CODE ==========
function checkingCOM4_Callback(hObject, eventdata, handles)
pause(.1000) % <============================
disp('CLEAR COM4 PARAMETERS ')
pause(.1000) % <============================
clc
delete(instrfind);
pause(0.1);
s = serial('COM4');
set(s,'BaudRate',9600,'DataBits', 8,...
'Parity', 'none','StopBits', 1, ...
'FlowControl', 'none','Terminator','CR/LF');
fopen(s);
s
fprintf(s,'g');
fclose(s);
delete(s);
clear s;
pause(1) % <============================
disp('CHECK PORTS (INSTRFIND) ')
pause(.5) % <============================
com_check = instrfind ;
if isempty(com_check)
handles.com1 = serial('COM4') ;
set(handles.com1,'BaudRate',2400,'Terminator', ...
'','DataBits',8,'Parity','none','StopBits',...
1, 'Timeout',1) ;
pause(.1000) % <============================
disp('OPEN COMM PORT (FOPEN)')
pause(.1000) % <============================
fopen(handles.com1) ;
i=0 ;
while i<=5
i=i+1 ;
pause(.1000) % <============================
disp('PING DEVICE (FWRITE) ')
pause(.1000) % <============================
fwrite(handles.com1,[hex2dec('5A') hex2dec('A5')],'uint8') ;
pause(.1000) % <============================
disp('CHECK RESPONSE (FREAD) ')
pause(.1000) % <============================
BoxGoStr = fread(handles.com1,2) ;
pause(.1000) % <============================
disp('IF NO RESPONSE (ISEMPTY)')
pause(.1000) % <============================
if ~isempty(BoxGoStr)
pause(.1000) % <============================
disp('CHECK BOXGOSTR (88,77)')
pause(.1000) % <============================
if BoxGoStr(1) == hex2dec('88') && BoxGoStr(2) == hex2dec('77')
set(handles.ComAlarm, 'String', 'Coms Initiated', 'BackgroundColor', 'g');
set(handles.CorrAlarm, 'String', 'Running', 'BackgroundColor', 'g');
set(handles.CorAlarmTxt, 'ForegroundColor','k')
pause(.1000) % <============================
disp('COM4 OPEN')
pause(.1000) % <============================
GoodComs=1;
break
else
pause(.1000) % <============================
disp('COM4 FAILED ')
pause(.1000) % <============================
GoodComs =0;
end
else
pause(.1000) % <============================
disp('COM4 FAILED')
pause(.1000) % <============================
GoodComs =0;
break
end
end
else
pause(.1000) % <============================
disp('COM4 SUCCESSFUL')
pause(.1000) % <============================
GoodComs = 1;
end
========== OUTPUT =========
checkingCOM4 - output
Serial Port Object : Serial-COM4
Communication Settings
Port: COM4
BaudRate: 9600
Terminator: 'CR/LF'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
CHECK PORTS (INSTRFIND) OPEN COMM PORT (FOPEN) PING DEVICE (FWRITE) CHECK RESPONSE (FREAD)
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
IF NO RESPONSE (ISEMPTY) COM4 FAILED
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Mobile Fundamentals finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!