procedure TForm1.Button1Click(Sender: TObject);
var
cc:TCommConfig;
Temp:string;
begin
Temp:='COM' + inttostr(rdcom.ItemIndex + 1); //选择要打开的通信端口
hComm:=CreateFile(PChar(Temp),GENERIC_READ OR GENERIC_WRITE,0,nil,OPEN_EXISTING,0,0); //打开通信端口
if (hComm = INVALID_HANDLE_VALUE) then
begin
messagebox(0,'打开通信端口错误!!','',MB_OK); //如果通信端口未打开
exit;
end
else messagebox(0,'通信端口已打开!!','',MB_OK);
GetCommState(hComm,cc.dcb); //得知目前通信端口的状态
cc.dcb.BaudRate:=CBR_9600; //设置波特率为9600
cc.dcb.ByteSize:=8; //字节为8位
cc.dcb.Parity:=NOPARITY; //parity为none
cc.dcb.StopBits:=onestopbit; //1个停止位
if not SetCommState(hComm,cc.dcb) then //设置通信端口的状态
begin
messagebox(0,'通信端口设置错误!!','',MB_OK);
CloseHandle(hComm);
exit;
end
else messagebox(0,'通信端口设置正确无误!!','',MB_OK);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//建立线程对象,并且激活执行
ReadThread:=Num_receive.Create(False);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
Temp:string;
lrc:LongWord;
begin
if (hComm=0) then exit; //检查handle的值
Temp:=msend.Text; //取得传送的字符串
//实际传送的动作
if WriteFile(hComm,PChar(Temp)^,Length(Temp),lrc,nil) then //送出数据
messagebox(0,'数据已送出!!','',MB_OK)
else messagebox(0,'数据未送出!!','',MB_OK);
end;