0
0738
Unregistered / Unconfirmed
GUEST, unregistred user!
注意:是并口,不是串口,代码如下
procedure TfrmMain.btnSendDataClick(Sender: TObject);
var
NumOfSended,NumOfSend,NumOfSending: DWord;
Err: DWord;
SendData: PChar;
Send_Os: TOverlapped;
hSend: THandle;
begin
NumOfSending := 0;
NumOfSended := 0;
SendData := 'aaaa';
NumOfSend := Length(SendData);
hSend := CreateFile('LPT1', GENERIC_WRITE, 0,nil, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, 0);
FillChar(Send_Os, SizeOf(Send_Os), 0);
Send_Os.hEvent := CreateEvent(nil, True, False, nil);
SetCommMask(hSend, EV_TXEMPTY); //并口好像不支持此过程????
repeat
// 发送通讯数据
if not WriteFile(hSend,SendDat[NumOfSending],NumOfSend,
NumOfSended,@Send_Os) then begin
Err := GetLastError;
if Err <> 0 then begin
if Err = ERROR_IO_PENDING then begin
WaitForSingleObject(Send_Os.hEvent, INFINITE);
GetOverlappedResult(hSend, Send_Os, NumOfSended, False);
end else
raise Exception.Create('发送数据失败');
end;
end;
Dec(NumOfSend, NumOfSended);
Inc(NumOfSending, NumOfSended);
until (NumOfSend <= 0);
end;
因为并口不支持SetCommMask,导致WaitForSingleObject无法返回,如果设置了
WaitForSingleObject的超时参数的话,结果一个字符都没有发送出去
procedure TfrmMain.btnSendDataClick(Sender: TObject);
var
NumOfSended,NumOfSend,NumOfSending: DWord;
Err: DWord;
SendData: PChar;
Send_Os: TOverlapped;
hSend: THandle;
begin
NumOfSending := 0;
NumOfSended := 0;
SendData := 'aaaa';
NumOfSend := Length(SendData);
hSend := CreateFile('LPT1', GENERIC_WRITE, 0,nil, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, 0);
FillChar(Send_Os, SizeOf(Send_Os), 0);
Send_Os.hEvent := CreateEvent(nil, True, False, nil);
SetCommMask(hSend, EV_TXEMPTY); //并口好像不支持此过程????
repeat
// 发送通讯数据
if not WriteFile(hSend,SendDat[NumOfSending],NumOfSend,
NumOfSended,@Send_Os) then begin
Err := GetLastError;
if Err <> 0 then begin
if Err = ERROR_IO_PENDING then begin
WaitForSingleObject(Send_Os.hEvent, INFINITE);
GetOverlappedResult(hSend, Send_Os, NumOfSended, False);
end else
raise Exception.Create('发送数据失败');
end;
end;
Dec(NumOfSend, NumOfSended);
Inc(NumOfSending, NumOfSended);
until (NumOfSend <= 0);
end;
因为并口不支持SetCommMask,导致WaitForSingleObject无法返回,如果设置了
WaitForSingleObject的超时参数的话,结果一个字符都没有发送出去