About API read/write COM code
解决方案 »
- 数据库登录问题,突然出错求解决!!!
- Form Dock 时候 Handle 的问题
- 请高手解决:创建Word时报“执行搜索操作时失败”问题(50分奉送)
- 谁看得懂下面的代码?能解释清楚的,可以再加100分!
- dbgrid的值如何顯示顔色
- 求贪吃蛇的源代码(加注释的)
- 动态生成数据库表?@@@ select?
- 关于Delphi中执行insert语句
- 谁有<<delphi 5企业级解决方案及应用剖析>>随书光盘的源代码?
- 编制多线程程序的大致步骤
- 解决问题就给分:关于AdvImage控件(播放jpg和gif等图像的控件)在D6,D7中引用出错的问题,哪位高手过来帮我看看?或者帮我找到D5的下载地
- 一个简单的问题(字符串处理),在线等
var
dcb:tdcb;
tmpHandle:thandle;
timeouts:commtimeouts;
begin
tmphandle:=0;
timeouts.ReadIntervalTimeout:=maxword;
timeouts.ReadTotalTimeoutMultiplier:=0;
timeouts.ReadTotalTimeoutConstant:=0;
timeouts.WriteTotalTimeoutMultiplier:=50;
timeouts.WriteTotalTimeoutConstant:=1000;
case access of
0:tmpHandle:=createfile(pchar(comName),GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,0);
//1:tmpHandle:=createfile(pchar(comName),GENERIC_READ,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
//2:tmpHandle:=createfile(pchar(comName),GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
end;
if tmpHandle=invalid_handle_value then
begin
raise exception.Create('Can not OPEN '+comName);
closehandle(tmphandle);
exit;
end
else
if not getcommstate(tmpHandle,dcb) then
begin
raise exception.Create('Can not Get '+comname+' Status');
closehandle(tmphandle);
exit;
end
else
begin
dcb.BaudRate:=9600;
dcb.ByteSize:=8;
dcb.StopBits:=twostopbits;
dcb.Parity:=noparity;
if not setcommstate(tmphandle,dcb) then
begin
raise exception.Create('Can not SET '+comname+' Status');
closehandle(tmphandle);
exit;
end
else
if not setupcomm(tmphandle,4096,4096) then
begin
raise exception.Create('Can not SET the BUFFER of '+comname);
closehandle(tmphandle);
exit;
end
else
if not setcommtimeouts(tmphandle,timeouts) then
begin
raise exception.Create('Can not SET the TIMEOUT of '+comname);
closehandle(tmphandle);
exit;
end;
end;
result:=tmphandle;
end;procedure Senddata(comname:string;memo:tmemo);
var
i:integer;
buf:pointer;
n{//,lperror}:longword;
//commstatus:tcomstat;
begin
n:=0;
for i:=0 to memo.Lines.Count-1 do
begin
handle_write:=initcom(comname,0);
buf:=pointer(memo.lines.strings[i]);
//clearcommerror(handle_write,lperror,@commstatus);
writefile(handle_write,buf^,length(memo.lines.strings[i]),n,nil);
//writethread:=twritethread.create(handle_write,buf,length(memo.lines.strings[i]));
closehandle(handle_write);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo2.Lines.Clear;
senddata('COM1',memo1);
readthread:=treadthread.create;
end;{ tReadThread }constructor tReadThread.create;
begin
FreeOnTerminate:=true;
inherited create(false);
end;procedure tReadThread.execute;
begin
inherited;
synchronize(readcom);
end;procedure tReadThread.readcom;
var
buf:array [0..maxword] of char;
commstatus:tcomstat;
n:longword;
dwError:LongWORD ;
messageRec:string;
begin
n:=0;
clearcommerror(handle_read,dwerror,@commstatus);
try
readfile(handle_read,buf,commstatus.cbInQue,n,nil)
except
exit;
end;
messagerec:=copy(buf,1,commstatus.cbInQue);
form1.memo2.Lines.Add(messagerec);
end;