(31)串口通讯都快把我折磨疯了:(-----给我解释段程序吧(100分)

  • 主题发起人 主题发起人 千中元
  • 开始时间 开始时间

千中元

Unregistered / Unconfirmed
GUEST, unregistred user!
来自:hubdog 时间:99-10-17 15:41:43 ID:142866 <br>&nbsp; var<br>1) &nbsp;hcom:thandle;<br>2) &nbsp;timeout:TCommTimeouts;<br>3) &nbsp;dataout,datain:pchar;<br>4) &nbsp;overlapped:POverlapped;<br>5) &nbsp;buffersize:dword;<br>6) &nbsp;begin<br>7) &nbsp; hcom:=createfile('com1',generic_read or &nbsp; &nbsp;generic_write,0,nil,open_existing,<br>8) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;file_flag_overlapped,0);<br>9) &nbsp;timeout.readintervaltimeout:=10;<br>10) timeout.readtotaltimeoutmultiplier:=20;<br>11) timeout.readtotaltimeoutconstant:=30;<br>12) setcommtimeouts(hcom,timeout);<br>13) setevent(hcom);<br>14) dataout:='S';<br>15) getmem(overlapped,sizeof(tOverlapped));<br>16) buffersize:=4096;<br>17) getmem(datain,4096);<br>18) if writefile(hcom,dataout,1,buffersize,overlapped) then<br>19) &nbsp; showmessage('write right');<br>20) if readfile(hcom,datain,1,buffersize,overlapped) then<br>21) &nbsp; showmessage('read right');<br>22) freemem(datain);<br>23) freemem(overlapped);<br>24) closehandle(hcom);<br>25) &nbsp; &nbsp; &nbsp;end;<br>26)@type表示一个地址(如0012:0034),若overlapped:poverlapped不应27)用@<br>28)若overlapped:toverlapped then use @overlapped<br>&nbsp;问题一:<br>&nbsp; &nbsp; &nbsp; Poverlapped和toverlapped有什么区别?
 
POverlapped = ^TOverlapped
 
kang说的没错啊,<br>Toverlapped是一个record<br>poverlapped是Toverlapped类型的指针
 
从内存来看:Poverlapped和toverlapped有什么区别<br><br>00130100: 12 34 56 78 90 12 34 56 78 99 //这里放的是 TOverlapped的数据<br><br><br>00151234: 00 01 13 00 //这里放的是POverLapped变量,其值00130100指向上面那个地址<br><br>
 
第二个问题:<br>&nbsp; &nbsp; 怎么setevent前没有createvent?<br>&nbsp; &nbsp; 如果仅仅读串口用不用setevent?
 
程序好象没完。大概还有一个线程,在用waitForsingleSubject什么的等着<br>这个事件的置位(用setevent).<br><br>做串口的毕业设计?就把spcomm的源码打出来交上去!没一百页,也有<br>八十页!还一调就通 :-)<br><br>
 
呵呵。。我希望代码不超过两页,读着也省力气
 
太懒,这样不好
 
LPDWORD lpNumberOfBytesRead, // address of number of bytes read <br>readfile的第三个参数,怎么翻译不顺当?
 
由于delphi写成: var lpNumberOfBytesRead: DWORD<br>你就:<br>var <br>&nbsp; NumberOfBytesRead:DWORD;<br><br>ReadFile(xxx,xxx,NumberOfBytesRead,...)
 
在Delphi里,所有的对象都传指针,比如<br>procedure F (Buffer: Pointer);<br><br>定义一个Buffer: Pointer;<br><br>这时候调用F的时候应该写成<br>F(Buffer^)<br>不能写成F(Buffer)<br><br>因为编译器会把对象转换为指针,如果直接用Buffer做参数,一转换就成了<br>指针的指针了<br>
 
unit comdemou;<br>interface<br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br>const<br>&nbsp; &nbsp; &nbsp;Wm_commNotify=Wm_User+12;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; Procedure comminitialize;<br>&nbsp; &nbsp; Procedure MsgcommProcess(Var Message:Tmessage); Message Wm_commnotify;<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; // 线 程 声 明<br>&nbsp; TComm=Class(TThread)<br>&nbsp; protected<br>&nbsp; &nbsp; &nbsp;procedure Execute;override;<br>&nbsp; end;<br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; hcom,Post_Event:Thandle;<br>&nbsp; lpol:Poverlapped;<br>implementation<br>{$R *.DFM}<br>Procedure TComm.Execute; &nbsp; &nbsp;// 线 程 执 行 过 程<br>var<br>dwEvtMask:Dword;<br>Wait:Boolean;<br>Begin<br>fillchar(lpol,sizeof(toverlapped),0);<br>While True do Begin<br>&nbsp; &nbsp; &nbsp; dwEvtMask:=0;<br>&nbsp; &nbsp; &nbsp; Wait:=WaitCommEvent(hcom,dwevtmask,lpol); &nbsp;// 等 待 串 行 口 事 件;<br>&nbsp; &nbsp; &nbsp; if Wait Then Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;waitforsingleobject(post_event,infinite); &nbsp; &nbsp; &nbsp;// 等 待 同 步 事 件 置 位;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resetevent(post_event); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 同 步 事 件 复 位;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PostMessage(Form1.Handle,WM_COMMNOTIFY,0,0);// 发 送 消 息;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br><br>procedure Tform1.comminitialize; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 串 行 口 初 始 化<br>var<br>lpdcb:Tdcb;<br>Begin<br>hcom:=createfile('com2',generic_read or generic_write,0,nil,open_existing,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_attribute_normal or file_flag_overlapped,0);// 打 开 串 行 口<br>&nbsp; &nbsp; if hcom=invalid_handle_value then<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp;setupcomm(hcom,4096,4096); // 设 置 输 入, 输 出 缓 冲 区 皆 为4096 字 节<br>&nbsp; &nbsp; &nbsp; &nbsp; getcommstate(hcom,lpdcb); &nbsp;// 获 取 串 行 口 当 前 默 认 设 置<br>&nbsp; &nbsp; &nbsp; &nbsp; lpdcb.baudrate:=2400;<br>&nbsp; &nbsp; &nbsp; &nbsp; lpdcb.StopBits:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; lpdcb.ByteSize:=8;<br>&nbsp; &nbsp; &nbsp; &nbsp; lpdcb.Parity:=EvenParity; &nbsp; &nbsp;// 偶 校 验<br>&nbsp; &nbsp; &nbsp; &nbsp; Setcommstate(hcom,lpdcb);<br>&nbsp; &nbsp; &nbsp; &nbsp; setcommMask(hcom,ev_rxchar); <br>&nbsp; &nbsp; &nbsp;// 指 定 串 行 口 事 件 为 接 收 到 字 符;<br>end;<br>Procedure TForm1.MsgcommProcess(Var Message:Tmessage);<br>var<br>Clear:Boolean;<br>Coms:Tcomstat;<br>cbNum,ReadNumber,lpErrors:Integer;<br>Read_Buffer:array[1..100]of char;<br>Begin<br>Clear:=Clearcommerror(hcom,lpErrors,@Coms);<br>if Clear Then Begin<br>&nbsp; &nbsp;cbNum:=Coms.cbInQue;<br>&nbsp; &nbsp;ReadFile(hCom,Read_Buffer,cbNum,ReadNumber,lpol);<br>&nbsp; &nbsp;// 处 理 接 收 数 据<br>&nbsp; &nbsp;SetEvent(Post_Event); &nbsp; &nbsp; &nbsp; &nbsp; // 同 步 事 件 置 位<br>&nbsp; &nbsp;end;<br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>comminitialize;<br>post_event:=CreateEvent(nil,true,true,nil); // 创 建 同 步 事 件;<br>Tcomm.Create(False); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 创 建 串 行 口 监 视 线 程;<br>end;<br>end.<br><br><br>这个程序我已经调式了一下,不过好象还是不怎么行,大家帮忙吧!<br>我也在做毕业设计啊,烦死了!<br>
 
hehe..me 2.<br>
 
后退
顶部