Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2005.01.16;
Скачать: [xml.tar.bz2];

Вниз

Как проверить истек ли TimeOut при работе с портом   Найти похожие ветки 

 
Kolan ©   (2004-12-31 00:08) [0]

Здравствуйте,
 При работе с Com портом мне надо проверить, если прошло какое-то время, то значит устройство не отвечеет. Не пойму как это сделать(понятно что с помошью таймера).


 
Unleashed   (2004-12-31 02:53) [1]

В данном фрагменте проверяется состояние порта. проверка состояния порта  проводится 3-х секундным прослушиванием.
function commdialog(port: pchar): integer;
var
h: thandle;
com: _commconfig;
err: integer;
begin
h:= createfile(port,GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,
File_attribute_normal and FILE_FLAG_OVERLAPPED,0);
result:=0;
if h = INVALID_HANDLE_VALUE then begin
result:=1;
application.MessageBox("Íåò äîñòóïà ê ïîðòó ",port,mb_ok);
end else begin
getcommstate(h,ip);
com.dcb:=ip;
if not commconfigdialog(port,application.MainForm.Handle,com) then
begin if getlasterror <> ERROR_CANCELLED then
application.MessageBox(pchar("&#206;&#248;&#232;&#225;&#234;&#224; &#241;&#225;&#238;&#240;&#224; &#241;&#226;&#229;&#228;&#229;&#237;&#232;&#233; " + inttostr(getlasterror)),
"&#206;&#248;&#232;&#225;&#234;&#224; &#226;&#226;&#238;&#228;&#224;-&#226;&#251;&#226;&#238;&#228;&#224;",mb_ok); result:=-1 end else
begin
ip:= com.dcb;
setcommstate(h,ip);
end;
end;
closehandle(h);
end;
var
ov: toverlapped;
i,i1,err: integer;
port,rbuf: pchar;
buf: char;
ss,ss1: string;
succ: boolean;
wr,wro,rd,rdo,ch: cardinal;
begin
clear.Enabled:=false;
snd.Enabled:=false;
can.Enabled:= true;
buf:=" ";
fillchar(ov,sizeof(ov),0);
ov.Offset:=0;
ov.hEvent:= createevent(nil,true,false,nil);
port:= pchar("COM" + pchar(m.Lines.strings[0])[3]);
err:= commdialog(port);
g.Show;
st.SimpleText:="";
if err <>-1 then
for i:=err to m.Lines.Count-1 do if not br then begin
ch:=0;
wr:=0;
rd:=0;
rdo:=0;
wro:=0;
ss:="";
g.Progress:=0;
update;
port:= pchar("COM" + pchar(m.Lines.strings[i])[3]);
h:= createfile(port,GENERIC_READ or GENERIC_WRITE,0,
nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
if h = INVALID_HANDLE_VALUE then begin
st.SimpleText:=port +": &#205;&#229;&#242; &#228;&#238;&#241;&#242;&#243;&#239;&#224;";
end;
setcommstate(h,ip);
update;
if h <> INVALID_HANDLE_VALUE then
g.MaxValue:=strlen(pchar(m.Lines.strings[i]))-6 else g.MaxValue:=0;
getmem(rbuf,g.MaxValue);
for i1:=7 to strlen(pchar(m.Lines.strings[i])) do if not br then begin
if h<>invalid_handle_value then
st.SimpleText:=port+ ": &#206;&#242;&#239;&#240;&#224;&#226;&#234;&#224; &#228;&#224;&#237;&#237;&#251;&#245;...";
st.Update;
buf:= m.Lines.strings[i][i1];
if writefileex(h,@buf,1,ov,nil) then begin
application.ProcessMessages;
if getoverlappedresult(h,ov,wr,true) then inc(ch);
end;
rdo:=0;
application.ProcessMessages;
succ:= readfile(h,rbuf^,1,rdo,@ov);
if not succ then
if waitforsingleobject(ov.hEvent,3000)=wait_object_0 then
GetOverlappedResult(h,Ov,rdo,true)
else begin
ch:=0;
st.SimpleText:=port+ ": &#205;&#229;&#242; &#241;&#232;&#227;&#237;&#224;&#235;&#224;";
break;
end;
g.Progress:= g.Progress+1;
setstring(ss1,rbuf,1);
inc(wro);
ss:= ss+ss1;
end else break;
if (wro >0) and (ch>0) and (h <> INVALID_HANDLE_VALUE) then
st.SimpleText:=
port + ": &#209;&#238;&#238;&#225;&#249;&#229;&#237;&#232;&#229; &#239;&#238;&#235;&#243;&#247;&#229;&#237;&#238; "" + ss +
"". &#193;&#224;&#233;&#242; &#238;&#242;&#239;&#240;&#224;&#226;&#235;&#229;&#237;&#238;: " + inttostr(ch) + ". &#193;&#224;&#233;&#242; &#239;&#238;&#235;&#243;&#247;&#229;&#237;&#238;: "
+ inttostr(wro) + ". &#207;&#238;&#242;&#229;&#240;&#232; &#239;&#240;&#232; &#242;&#240;&#224;&#237;&#241;&#236;&#232;&#241;&#241;&#232;&#232; "+floattostr((ch -wro)/ch*100)+"%";
view.info.Items.Add(st.SimpleText);
application.ProcessMessages;
closehandle(h);
end
else begin
br:= false;
break;
end;
freemem(rbuf);
st.Hint:= st.SimpleText;
closehandle(ov.hEvent);
snd.Enabled:=true;
can.Enabled:=false;
clear.Enabled:=true;
g.Hide;
if view.info.Count >1 then
with view do begin
ClientHeight:=info.Items.Count*view.info.ItemHeight-info.Font.Height;
ClientWidth:= info.Canvas.TextWidth(info.Items.Strings[0])+info.Font.Size;
ShowModal;
info.Clear;
end else view.info.Clear;
end;
procedure Tport.CanClick(Sender: TObject);
begin
br:= true;
can.Enabled:= false;
snd.Enabled:=true;


 
GanibalLector ©   (2004-12-31 02:58) [2]

http://www.delphikingdom.com/asp/viewitem.asp?UrlItem=/mastering/ports4.htm


 
DiamondShark ©   (2004-12-31 10:53) [3]


> Unleashed   (31.12.04 02:53) [1]

Мощно задвинул...



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2005.01.16;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.034 c
6-1098668733
Dvm_Home
2004-10-25 05:45
2005.01.16
IdFTP


14-1104150956
Ilya___
2004-12-27 15:35
2005.01.16
Подскажите пожалуйта! Как создать файл таблицу Excel


1-1104141631
Aleksandr.
2004-12-27 13:00
2005.01.16
Как у главного меню поменять цвет?


3-1102684136
Rule
2004-12-10 16:08
2005.01.16
Вопрос простой, аж смешно по DBComboBox


14-1104220075
Cosinus
2004-12-28 10:47
2005.01.16
Помогите перевести с С++...





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский