串口createfile函数---返回值问题(100分)

  • 主题发起人 主题发起人 crtd02433
  • 开始时间 开始时间
C

crtd02433

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:<br>&nbsp; &nbsp; &nbsp; &nbsp;createfile这个函数的最后一个参数是不是就是这个函数的返回值呀,<br>&nbsp; 还有:假设hcomm:=createfile(。。。。。。);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 那么:hcomm=0,和hcomm=invalid_handle_value<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 这两个表达式有什么区别呀,???<br>&nbsp; 详细解释一下句柄,好吗??? 在线等待,满意马上给分!!!!!!
 
invalid_handle_value==0定义的常量,表示无效的句柄<br>if(hcomm=invalid_handle_value)then 出错了
 
yanghai0437这位朋友,你是说invalid_handle_value和0是同一个意思了,那么这段程序<br>我又糊涂了!!能否QQ聊,我的:22330699<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, ExtCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; RadioGroup1: TRadioGroup;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Memo2: TMemo;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure OpenComm; &nbsp;//打开通信端口<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; hComm: THandle;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp;opencomm;<br>end;<br><br>//以下是打开通信端口的程序<br>procedure TForm1.OpenComm;<br>var<br>&nbsp; cc:TCOMMCONFIG;<br>&nbsp; Temp:string;<br><br>begin<br>&nbsp; Temp:='COM'+inttostr(radiogroup1.ItemIndex+1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 选择所要打开的COM<br>&nbsp; memo1.Text:=temp;<br>&nbsp; hComm:=CreateFile(PChar(Temp), GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp;0, nil, OPEN_EXISTING, 0, 10); // 打开COM<br>&nbsp; if (hComm =invalid_handle_value) then begin &nbsp;// =================&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;和下面对应解释一下<br>&nbsp; &nbsp; MessageBox (0, '打开通信端口错误!!','',MB_OK);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; GetCommState(hComm,cc.dcb); // 得知目前COM 的状态<br>&nbsp; cc.dcb.BaudRate:=CBR_9600; // 设置波特率为9600<br>&nbsp; cc.dcb.ByteSize:=8; &nbsp;// 字节为 8 bit<br>&nbsp; cc.dcb.Parity:=NOPARITY; // Parity 为 None<br>&nbsp; cc.dcb.StopBits:=ONESTOPBIT; // 1 个Stop bit<br><br>&nbsp; if not SetCommState(hComm, cc.dcb) then begin// 设置COM 的状态<br>&nbsp; &nbsp; MessageBox (0, '通信端口设置错误!!!','',MB_OK);<br>&nbsp; &nbsp; CloseHandle(hComm);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp; SetCommMask(hcomm,$0);<br>&nbsp; &nbsp;CloseHandle(hComm);<br>&nbsp; &nbsp;close;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>var<br>&nbsp; Temp : string;<br>&nbsp; inbuff: array[0..2047] of Char;<br>&nbsp; nBytesRead, dwEvent, dwError:LongWORD ;<br>&nbsp; cs:TCOMSTAT;<br>begin<br>&nbsp; &nbsp;ClearCommError(hComm,dwError,@CS); &nbsp;//取得状态<br>&nbsp; &nbsp; &nbsp; &nbsp;// 数据是否大于我们所准备的Buffer<br>&nbsp; &nbsp;if cs.cbInQue &gt; sizeof(inbuff) then begin<br>&nbsp; &nbsp; &nbsp;PurgeComm(hComm, PURGE_RXCLEAR); &nbsp;// 清除COM 数据<br>&nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;ReadFile(hComm, inbuff,cs.cbInQue,nBytesRead,nil); // 接收COM 的数据<br>&nbsp; &nbsp;//转移数据到变量中<br>&nbsp; &nbsp;Temp:=Copy(inbuff,1,cs.cbInQue);<br>&nbsp; &nbsp;memo2.Text :=Temp; &nbsp; // 将数据显示于Memo1 上<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp; Temp:string;<br>&nbsp; lrc:LongWord;<br>begin<br>&nbsp; if (hComm=0) then exit; //检查Handle值=================================&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>&nbsp; Temp:=memo1.Text ;//取得传送的字符串<br>&nbsp; //实际的传送动作<br>&nbsp; WriteFile(hComm,PChar(Temp)^,Length(Temp), lrc, nil); // 送出数据<br><br>end;<br><br>end.<br><br>======================》》》》》》》所在行怎么解释呀??
 
在场的留下QQ号码,好吗???我的22330699
 
&nbsp; invalid_handle_value=0还是==1呀,<br>&nbsp;不可能等于两个值吧?
 
接受答案了.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
916
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部