win2000串口求助。(我在csdn上无人可以回答)高分200!(200分)

  • 主题发起人 主题发起人 ytdxl
  • 开始时间 开始时间
Y

ytdxl

Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi3.0在win98下写了一段串口通讯的程序,可以正常使用,可是同样的,<br>在win2000下,却不能正常运行,请各位高手指正:<br>打开串口:<br>function TOperateCOM.InitCOM :BooLean;<br>var<br>&nbsp; HandleID :Dword;<br>&nbsp; SFileName:String; &nbsp;iUseWhatCOM:integer;<br>&nbsp; tTIniFile:TIniFile;<br>begin<br>&nbsp; &nbsp; &nbsp;sFileName:='c:/'+S21Dir+'/ZdSystem.ini';<br>&nbsp; &nbsp; &nbsp;tTIniFile:=TIniFile.Create(sFileName);<br>&nbsp; &nbsp; &nbsp;iUseWhatCOM:=tTIniFile.Readinteger('COM','COM',1 );<br>&nbsp; &nbsp; &nbsp;HandleID:=-1;<br>&nbsp; &nbsp; &nbsp;CASE &nbsp; iUseWhatCOM Of<br>&nbsp; &nbsp; &nbsp; &nbsp; 1: &nbsp; &nbsp;HandleID &nbsp; &nbsp; :=createfile('COM1',generic_read or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;generic_write,0,nil,open_existing,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;file_attribute_normal or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;file_flag_overlapped,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; 2: &nbsp; &nbsp;HandleID :=CreateFile('COM2', GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, Nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATE_ALWAYS,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0);<br>&nbsp; &nbsp; &nbsp;END;<br>&nbsp; &nbsp; &nbsp;tTIniFile.Free;<br>&nbsp; &nbsp; &nbsp;If HandleID =-1<br>&nbsp; &nbsp; &nbsp;Then &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowCOMError;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result :=FALSE;<br>&nbsp; &nbsp; &nbsp;End<br>&nbsp; &nbsp; &nbsp;Else &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCommMask( HandleID, EV_RXCHAR) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetupComm(HandleID, 4096, 4096 ) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PurgeComm(HandleID, PURGE_TXABORT or PURGE_RXABORT or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PURGE_TXCLEAR or PURGE_RXCLEAR );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCommTimeouts(HandleId,COMMTIMEOUTS);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COMMTIMEOUTS.ReadIntervalTimeout:=10;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COMMTIMEOUTS.ReadTotalTimeoutMultiplier:=12;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COMMTIMEOUTS.ReadTotalTimeoutConstant:=10;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCommTimeouts(HandleId,COMMTIMEOUTS);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCommState(HandleID, dcb );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dcb.DCBlength :=sizeof( DCB );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dcb.BaudRate :=CBR_4800;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dcb.ByteSize :=8;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dcb.Parity :=2; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dcb.Stopbits :=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetCommState(HandleID, dcb);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OperateCOM.COMHandle :=HandleID;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result :=TRUE;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ComBusy :=False;<br>&nbsp; &nbsp; &nbsp;End;<br>end;<br><br>写出数据:<br>function TOperateCOM.WriteBytesToCom( ByteNum :Dword; buf :array of byte) :BooLean;<br>var<br>&nbsp; &nbsp; dwBytesWritten, i :Dword ;<br>&nbsp; &nbsp; overlp :POverLapped; //OverLapped<br>&nbsp; &nbsp; Label EXCEPTIONEND;<br>begin<br>&nbsp; &nbsp; &nbsp;While ComBusy=True do;<br>&nbsp; &nbsp; &nbsp;ComBusy :=True;<br>&nbsp; &nbsp; &nbsp;Result:=TRUE;<br>&nbsp; &nbsp; &nbsp;dwBytesWritten:=0;<br>&nbsp; &nbsp; &nbsp;For i :=0 to ByteNum-1 do<br>&nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, NIL);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCOMError;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;End;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If Result=False<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Then &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCOMError;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Goto EXCEPTIONEND;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;End;<br>&nbsp; &nbsp; &nbsp;End;<br>&nbsp; &nbsp; &nbsp;EXCEPTIONEND:<br>&nbsp; &nbsp; &nbsp;ComBusy :=False;<br>end;<br><br>总是<br>Result=False<br>我用<br>&nbsp; &nbsp; result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);<br>lpol是我定义的全局变量 POverLapped; <br>&nbsp; &nbsp; 也是Result=false;<br>用com1和com2两种打开方式,结果是一样的,但二者的句柄不一样。<br>&nbsp; &nbsp; 有什么问题,请告诉指正。ytdxl@21cn.com &nbsp;谢谢!<br><br><br>
 
还是没人回答。
 
你用spcomm看可以吗?
 
用微软的 Mscomm 控件试试,我用过,很方便,好运!
 
是呀,为什么不用MSCOMM控件呢?在2000下对安全性的要求比98高的多,可能是某些东西被禁用了把
 
我有比大家提到到的更好的东西,是Pcomm.dll,在win98,win2000下都很好用。<br>我现在是比较疑惑,我想知道在win2000下不行的原因。(大家如果对Pcomm.dll有兴趣,<br>我可以共享给大家,是台湾摩砂的,做串口的老大)。我同意 sweetychild 的意见,<br>可能是某些东西被禁用了。
 
to:ytdx1<br>给我一个Pcomm.dll参考一下!<br>fengzi110@163.com
 
我的问题还有人关注吗?
 
我的程序用汇编做的也存在这个问题,总之win2000下的东西好多都不好用<br>,我建议微软能够给予解释,如打印,拨号程序,串口程序等等。<br>
 
create_always为什么这么用
 
我也在用PCOMM。DLL,但是一点也不觉得好用,(因为项目组用,我不得不用),还是<br>SPCOMM感觉N爽<br>至于MSCOMM, 有次差点儿给它害死,再也不敢用了。。。
 
When you specify FILE_FLAG_OVERLAPPED, the ReadFile and WriteFile functions must specify an OVERLAPPED structure. <br>可是你的程序中并没有指定一个overlapped结构呀(只指定了一个overlapped指针,而且未分配内存也没赋值),而且最后也没调用,当然写不进去啦
 
又是微软使得绊子,你还是从了微软吧
 
我刚把ComPortLib 2.63上传到www.playicq.com,支持win2000。有源码、例子。
 
在2000下面用原来在98下面将串口作为文件处理的方法是有问题,我原来用SPCOMM<br>和一个Asysns的一个控件好像都不是工作的很好,后来我换成用Pcomm.dll发现一切<br>问题都解决了,所以我现在一直用pcomm.dll。不过我不知道问题所在,那位老大知道<br>就说一下吧:)
 
我认为主要是对串口初始化的问题,在windows2000下对串口的初始化好像需要符合2000的自身规范
 
spcomm在使用的时候,最好把几个类似Xoff 什么的属性设置为False,不然的话,他会<br>把一些值屏蔽掉的。
 
后退
顶部