使用usb转串口的设备,用spcomm控件直接向串口发送、接收数据。
打开串口时出现错误“File handle is not a comm handle”;
我将spcomm源代码中的以下语句封掉
if GetFileType( hNewCommFile ) <> FILE_TYPE_CHAR then
begin
CloseHandle( hNewCommFile );
raise ECommsError.Create( 'File handle is not a comm handle ' )
end;此后,startcomm可以通过,使用WriteCommData发送数据也没有报错,但是却无法接收到数据。
请问是什么问题呀!
打开串口时出现错误“File handle is not a comm handle”;
我将spcomm源代码中的以下语句封掉
if GetFileType( hNewCommFile ) <> FILE_TYPE_CHAR then
begin
CloseHandle( hNewCommFile );
raise ECommsError.Create( 'File handle is not a comm handle ' )
end;此后,startcomm可以通过,使用WriteCommData发送数据也没有报错,但是却无法接收到数据。
请问是什么问题呀!
U口的COM,其实一样使用的
我觉得是你设备的问题
procedure TComm.StartComm;
var
hNewCommFile: THandle;
begin
// Are we already doing comm?
if (hCommFile <> 0) then
raise ECommsError.Create('This serial port already opened'); hNewCommFile :=
CreateFile(PChar('\\.\' +FCommName),
GENERIC_READ or GENERIC_WRITE,
0, {not shared}
nil, {no security ??}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,
0 {template});procedure TReadThread.Execute;
var
szInputBuffer: array[0..INPUTBUFFERSIZE - 1] of Char;
nNumberOfBytesRead: DWORD; HandlesToWaitFor: array[0..1] of THandle;
dwHandleSignaled: DWORD;
// Needed for overlapped I/O (ReadFile)
overlappedRead: TOverlapped;label
EndReadThread;
begin
FillChar(overlappedRead, Sizeof(overlappedRead), 0); // Lets put an event in the Read overlapped structure.
overlappedRead.hEvent := CreateEvent(nil, True, True, nil);
if overlappedRead.hEvent = 0 then
begin
PostHangupCall;
goto EndReadThread
end;
// We will be waiting on these objects.
HandlesToWaitFor[0] := hCloseEvent;
HandlesToWaitFor[1] := overlappedRead.hEvent; // Setup CommEvent handling. // Set the comm mask so we receive error signals.
if not SetCommMask(hCommFile, EV_ERR or EV_RLSD or EV_RING) then
begin
PostHangupCall;
goto EndReadThread
end; // Start waiting for Read events.
if not SetupReadEvent(@overlappedRead,
szInputBuffer, INPUTBUFFERSIZE,
nNumberOfBytesRead) then
goto EndReadThread; // Keep looping until we break out.
while True do
begin
// Wait until some event occurs (data to read; error; stopping).
dwHandleSignaled := WaitForMultipleObjects(2, @HandlesToWaitFor,
False, INFINITE); // Which event occured?
case dwHandleSignaled of
WAIT_OBJECT_0: // Signal to end the thread.
begin
// Time to exit.
goto EndReadThread
end; WAIT_OBJECT_0 + 1: // CommEvent signaled.
begin
// Get the new data!
if not HandleReadEvent(@overlappedRead,
szInputBuffer,
INPUTBUFFERSIZE,
nNumberOfBytesRead) then
goto EndReadThread; // Wait for more new data.
if not SetupReadEvent(@overlappedRead,
szInputBuffer, INPUTBUFFERSIZE,
nNumberOfBytesRead) then
goto EndReadThread
{break;} end; WAIT_FAILED: // Wait failed. Shouldn't happen.
begin
PostHangupCall;
goto EndReadThread
end
else // This case should never occur.
begin
PostHangupCall;
goto EndReadThread
end
end {case dwHandleSignaled}
end; {while True} // Time to clean up Read Thread.
EndReadThread: PurgeComm(hCommFile, PURGE_RXABORT + PURGE_RXCLEAR);
CloseHandle(overlappedRead.hEvent);
end; {TReadThread.Execute}
不知谁解决没,aiirii的方法确认能接收到吗?
我用的是台湾的USB->COM
没有问题