Y
ytdxl
Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi3.0在win98下写了一段串口通讯的程序,可以正常使用,可是同样的,<br>在win2000下,却不能正常运行,请各位高手指正:<br>打开串口:<br>function TOperateCOM.InitCOM :BooLean;<br>var<br> HandleID word;<br> SFileName:String; iUseWhatCOM:integer;<br> tTIniFile:TIniFile;<br>begin<br> sFileName:='c:/'+S21Dir+'/ZdSystem.ini';<br> tTIniFile:=TIniFile.Create(sFileName);<br> iUseWhatCOM:=tTIniFile.Readinteger('COM','COM',1 );<br> HandleID:=-1;<br> CASE iUseWhatCOM Of<br> 1: HandleID :=createfile('COM1',generic_read or<br> generic_write,0,nil,open_existing,<br> file_attribute_normal or<br> file_flag_overlapped,0);<br> 2: HandleID :=CreateFile('COM2', GENERIC_READ or GENERIC_WRITE,<br> 0, Nil,<br> CREATE_ALWAYS,<br> FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,<br> 0);<br> END;<br> tTIniFile.Free;<br> If HandleID =-1<br> Then begin<br> ShowCOMError;<br> result :=FALSE;<br> End<br> Else begin<br> SetCommMask( HandleID, EV_RXCHAR) ;<br> SetupComm(HandleID, 4096, 4096 ) ;<br> PurgeComm(HandleID, PURGE_TXABORT or PURGE_RXABORT or<br> PURGE_TXCLEAR or PURGE_RXCLEAR );<br><br> GetCommTimeouts(HandleId,COMMTIMEOUTS);<br> COMMTIMEOUTS.ReadIntervalTimeout:=10;<br> COMMTIMEOUTS.ReadTotalTimeoutMultiplier:=12;<br> COMMTIMEOUTS.ReadTotalTimeoutConstant:=10;<br> SetCommTimeouts(HandleId,COMMTIMEOUTS);<br><br> GetCommState(HandleID, dcb );<br> dcb.DCBlength :=sizeof( DCB );<br> Dcb.BaudRate :=CBR_4800;<br> Dcb.ByteSize :=8;<br> Dcb.Parity :=2; <br> Dcb.Stopbits :=0;<br> SetCommState(HandleID, dcb);<br> OperateCOM.COMHandle :=HandleID;<br> result :=TRUE;<br> ComBusy :=False;<br> End;<br>end;<br><br>写出数据:<br>function TOperateCOM.WriteBytesToCom( ByteNum word; buf :array of byte) :BooLean;<br>var<br> dwBytesWritten, i word ;<br> overlp OverLapped; //OverLapped<br> Label EXCEPTIONEND;<br>begin<br> While ComBusy=True do;<br> ComBusy :=True;<br> Result:=TRUE;<br> dwBytesWritten:=0;<br> For i :=0 to ByteNum-1 do<br> Begin<br> Try<br> //result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);<br><br> result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, NIL);<br> Except<br> ShowCOMError;<br> End;<br> If Result=False<br> Then Begin<br> ShowCOMError;<br> Goto EXCEPTIONEND;<br> End;<br> End;<br> EXCEPTIONEND:<br> ComBusy :=False;<br>end;<br><br>总是<br>Result=False<br>我用<br> result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);<br>lpol是我定义的全局变量 POverLapped; <br> 也是Result=false;<br>用com1和com2两种打开方式,结果是一样的,但二者的句柄不一样。<br> 有什么问题,请告诉指正。ytdxl@21cn.com 谢谢!<br><br><br>