串口问题!(50分)

  • 主题发起人 主题发起人 lzmling
  • 开始时间 开始时间
L

lzmling

Unregistered / Unconfirmed
GUEST, unregistred user!
我在dll中这样写的:
function TComm.InitCommdial(ThisCommName: string; BaudRate,DataByte,StopByte,ParityByte: integer): Boolean;
var
dcb: Tdcb;
begin
//Are we already doing comm?
if hCommFile<>0 then
begin
InitCommdial := False;
exit;
end;

hCommFile := CreateFile( PChar(ThisCommName),
GENERIC_READ or GENERIC_WRITE,
0, //not shared
nil, //no security ??
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED,
0);

if hCommFile=INVALID_HANDLE_VALUE then
begin
hCommFile := 0;
InitCommdial := False;
exit;
end;

//Is this a valid comm handle?
if GetFileType(hCommFile)<>FILE_TYPE_CHAR then
begin
CloseHandle(hCommFile);
hCommFile := 0;
InitCommdial := False;
exit;
end;

if not SetupComm(hCommFile, INQUESIZE, OUTQUESIZE) then
begin
CloseHandle(hCommFile);
hCommFile := 0;
InitCommdial := False;
exit;
end;

//Querying then setting the comm port configurations.
GetCommState(hCommFile, dcb);

dcb.BaudRate := BaudRate;
dcb.ByteSize := DataByte; //number of bits/byte, 4-8
dcb.Parity := ParityByte; //0-4=no,odd,even,mark,space
dcb.StopBits := StopByte; //0,1,2 = 1,1.5,2

if not SetCommState(hCommFile, dcb) then
begin
CloseHandle(hCommFile);
hCommFile := 0;
InitCommdial := False;
exit;
end;

//purge any information in the buffer
PurgeComm(hCommFile, PURGE_TXABORT or PURGE_RXABORT or
PURGE_TXCLEAR or PURGE_RXCLEAR);

InitCommdial := True;
end;

总是在SetCommState(hCommFile, dcb)时返回false,为什么呢?
 
有谁能看一下吗?谢谢[:(]
 
我在普通程序中这样写是正确的:
当端口没有被占有时SetCommState返回ture,被占有时返回false
没有在dll中测试,应该是一样的吧。
procedure TForm1.Button1Click(Sender: TObject);
var
dcb: Tdcb;
hCommFile:hwnd;
begin
dcb.BaudRate :=9600;//BaudRate;
dcb.ByteSize := 8;//DataByte; //number of bits/byte, 4-8
dcb.Parity := 0;//ParityByte; //0-4=no,odd,even,mark,space
dcb.StopBits := 0;//StopByte; //0,1,2 = 1,1.5,2
hCommFile := CreateFile( '1',//PChar(ThisCommName),
GENERIC_READ or GENERIC_WRITE,
0, //not shared
nil, //no security ??
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED,
0);


if not SetCommState(hCommFile, dcb) then
begin
CloseHandle(hCommFile);
hCommFile := 0;
// InitCommdial := False;
exit;
end;

end;
 
接受答案了.
 
后退
顶部