奇怪!WIN95和WINNT下同样的串口操作为什么结果会不同?(100分)

  • 主题发起人 主题发起人 dongym
  • 开始时间 开始时间
D

dongym

Unregistered / Unconfirmed
GUEST, unregistred user!
D5中一个在Win95下运行十分成功的串口操作,为什么在WinNT会出差错呢?
难道在CreateFile或WriteFile时函数参数要做一点变更吗?请各位大侠帮助
分析一下,不胜感谢!


//*******************变量定义**************************
hcom : THandle; //hFile
pdcb : TDcb; //_DCB;
pcomstat : TComstat; //_ComStat//PComstat
lpErrors : Cardinal
p : POverlapped;
dwBytesSent : DWORD; //Caidinal

TransBuf : array[0..15] of Byte //发送缓冲区
//*******************过程体内容************************//
hcom := CreateFile( 'COM1',
GENERIC_READ or GENERIC_WRITE,
0, // exclusive access
nil, // no security attrs
CREATE_NEW, //OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, // overlapped I/O
0);

if hCom = INVALID_HANDLE_VALUE then
begin
ShowMessage('串口未能正确打开');
close;
end;

GetCommState(hCom, pdcb);
//然后对pdcb的各参数赋值

SetCommState(hCom,pdcb);

SetCommMask(hCom ,EV_RXCHAR);

//ClearCommError(hCom,lpErrors ,PComStat(pcomstat));
SetupComm(hCom, 4096, 4096);
PurgeComm(hCom,PURGE_TXABORT or PURGE_RXABORT or
PURGE_TXCLEAR or PURGE_RXCLEAR );

//以上未检测到错误
//*******************写串口***************************
//对TransBuf赋值后

WriteFile(hCom,TransBuf,16,dwBytesSent,p); //p=nil,dwBytesSent=0

//此处用GetLastError()检测到WriteFile操作有参数错误(错误代码为87)
//以下其他操作无法正常进行
//******************过程体内容结束********************

疑问一:
ClearCommError(hCom,pdwErrorFlags,pcomstat)无论如何也不能用,本函数
要求第三个参数为PComStat类型,但pcomstat置成PComStat类型、TComstat
类型或_ComStat类型时,编译时均提示参数类型不匹配(95和NT下均如此)

疑问二:
WriteFile(hCom,TransBuf,16,dwBytesSent,p)要求第四个参数为Caidinal
类型,实际上为一个指针值,但第四个参数若取为@dwBytesSent或
Addr(dwBytesSent),编译不通过,取为dwBytesSent时,Win95下正常实现。
 
是不是security attrs这个参数的问题呢?
在95下可以省略,但在NT下就很重要,不信你试试EXITWINDOWSEX,在98下只要一句就可以
关闭计算机了,在NT下要写一串程序才行。
 
好象不是的,如果对于串口通信,NT下WriteFile中的安全属性应该没有什么作用,对此,我已经用VC验证过了.
 
佩服!
你为什么不用已有的控件写程序,而要自己写代码呢?
现在有很多串口通信控件!
 
多人接受答案了。
 
dongym:你好。我现在也碰到了同样的串口通信问题,在win98下正常,在win2000下运行不
正常。请不吝赐教。
我的问题在:http://www.delphibbs.com/delphibbs/dispq.asp?lid=1154498
 
当然不能用了,因为你打开串口时,指定了FILE_FLAG_OVERLAPPED(重叠I/O读写),而写
串口时却将OVERLAPPED设为NIL,肯定会出现错误,请看WRITEFILE中关于OVERLAPPED参数的
描述(大意是指如果打开串口时使用了I/O重叠操作,则READFILE和WRITEFILE时,必须指定
一个有效的OVERLAPPED参数,否则将会报告错误):
lpOverlapped
Points to an OVERLAPPED structure. This structure is required if hFile was opened
with FILE_FLAG_OVERLAPPED.
If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must
not be NULL. It must point to a valid OVERLAPPED structure. If hFile was opened
with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly
report that the write operation is complete.
If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the
write operation starts at the offset specified in the OVERLAPPED structure and
WriteFile may return before the write operation has been completed. In this case,
WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING.
This allows the calling process to continue processing while the write operation
is being completed. The event specified in the OVERLAPPED structure is set to the
signaled state upon completion of the write operation.

If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the
write operation starts at the current file position and WriteFile does not return
until the operation has been completed.
If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL,
the write operation starts at the offset specified in the OVERLAPPED structure
and WriteFile does not return until the write operation has been completed.
另,打开一个串口不能用 CREATE_NEW,因用OPEN——EXSITS
 
后退
顶部