下面这个程序是我用在监控程序里的。 用些地方是多余的可以不看。 function OpenCommPort(port:string; Baud ,_tOut: integer):Thandle; var tOut : _COMMTIMEOUTS; DCB : _DCB; hCom : Thandle; begin hCom := CreateFile(pchar(Port),GENERIC_WRITE or GENERIC_READ,0,NiL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0); if (hCom <> INVALID_HANDLE_VALUE) then begin GetCommState(hCom,DCB); with DCB do begin BaudRate := Baud; ByteSize :=8; // number of bits/byte, 4-8 Parity := NOPARITY; // 0-4=no,odd,even,,space StopBits := ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 end; SetCommState(hCom,DCB); GetCommTimeouts(hCom,tOut); with tOut do begin ReadTotalTimeoutMultiplier := 5; ReadTotalTimeoutConstant := _tOut; WriteTotalTimeoutMultiplier := 5; WriteTotalTimeoutConstant := _tOut; end; SetCommTimeouts(hCom,tOut); result := hCom; end else Result := 0; end;
to ActiveLF(程序人生): 我按照呢说的方法做了,可是还是收不到数据。 能不能给点另外的帮助呢? 谢谢!!!!!!
现在我将程序改为下面: 初始化过程: procedure TForm1.FormCreate(Sender: TObject); begin Form1.Caption:='串口通讯'; MSComm1.InBufferCount:=0; MSComm1.InputLen:=0; MSComm1.RThreshold:=1; MSComm1.CommPort:=1; MSComm1.DTREnable:=true; MSComm1.RTSEnable:=true; MSComm1.PortOpen:=true; MSComm1.InputMode:=1; MSComm1.Settings:='9600,n,8,1'; Mscomm2.Settings:='9600,n,8,1'; MSComm2.InputMode:=1; MSComm2.InBufferCount:=0; MSComm2.InputLen:=0; MSComm2.RThreshold:=1; MSComm2.CommPort:=2; MSComm2.DTREnable:=true; MSComm2.RTSEnable:=true; MSComm2.PortOpen:=true; Memo2.text:=''; Memo1.text:=''; end; 发送过程: procedure TForm1.Button1Click(Sender: TObject); var i:integer; s,datastr:string; begin s:=memo1.text; datastr:=''; if MSComm1.PortOpen=false then showmessage('error!') else begin showmessage('ok!'); for i:=0 to (length(s) div 2)-1 do begin datastr:=datastr+chr(strtoint('$'+ copy(s,2*i+1,2))); end; begin MSComm1.output:=datastr; end; end; end; 怎样证明发送成功? 接收过程: procedure TForm1.Button3Click(Sender: TObject); var recstr:string; strtemp:string; i:integer; begin if Mscomm2.PortOpen=false then showmessage('error') else showmessage('ok'); if Mscomm2.CommEvent=comEvReceive then begin sleep(350); recstr:=Mscomm1.input; strtemp:=''; for i:=0 to length(recstr) do strtemp:=strtemp+inttohex(ord(recstr[i]),2); memo2.text:=memo2.text+strtemp; end; end; memo2框中看不到数据,单步执行时发现程序在接收过程中运行到: if Mscomm2.CommEvent=comEvReceive then就步往下执行了,为什么呢? 希望大家能给我找到错误,再一次感谢大家的光临。
if Mscomm2.CommEvent=comEvReceive then begin sleep(350); recstr:=Mscomm1.input; strtemp:=''; for i:=0 to length(recstr) do strtemp:=strtemp+inttohex(ord(recstr[i]),2); memo2.text:=memo2.text+strtemp; end; end; 把这段代码放到onComm事件中去看看。
to deathcat(从死亡边缘站起来的患了狂猫病的猫): 现在可以肯定的是:串口线的连接没有问题. 下面这段代码放在onComm事件中根本就不会执行。 if Mscomm2.CommEvent=comEvReceive then begin sleep(350); recstr:=Mscomm1.input; strtemp:=''; for i:=0 to length(recstr) do strtemp:=strtemp+inttohex(ord(recstr[i]),2); memo2.text:=memo2.text+strtemp; end; end; 我将其放在一个按钮事件中,点击按钮表示接收。
用些地方是多余的可以不看。
function OpenCommPort(port:string; Baud ,_tOut: integer):Thandle;
var
tOut : _COMMTIMEOUTS;
DCB : _DCB;
hCom : Thandle;
begin
hCom := CreateFile(pchar(Port),GENERIC_WRITE or GENERIC_READ,0,NiL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
if (hCom <> INVALID_HANDLE_VALUE) then
begin
GetCommState(hCom,DCB);
with DCB do begin
BaudRate := Baud;
ByteSize :=8; // number of bits/byte, 4-8
Parity := NOPARITY; // 0-4=no,odd,even,,space
StopBits := ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
end;
SetCommState(hCom,DCB);
GetCommTimeouts(hCom,tOut);
with tOut do begin
ReadTotalTimeoutMultiplier := 5;
ReadTotalTimeoutConstant := _tOut;
WriteTotalTimeoutMultiplier := 5;
WriteTotalTimeoutConstant := _tOut;
end;
SetCommTimeouts(hCom,tOut);
result := hCom;
end else Result := 0;
end;
你和我想的一样呀。
好。
去试试。
我按照呢说的方法做了,可是还是收不到数据。
能不能给点另外的帮助呢?
谢谢!!!!!!
初始化过程:
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Caption:='串口通讯';
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.RThreshold:=1;
MSComm1.CommPort:=1;
MSComm1.DTREnable:=true;
MSComm1.RTSEnable:=true;
MSComm1.PortOpen:=true;
MSComm1.InputMode:=1;
MSComm1.Settings:='9600,n,8,1';
Mscomm2.Settings:='9600,n,8,1';
MSComm2.InputMode:=1;
MSComm2.InBufferCount:=0;
MSComm2.InputLen:=0;
MSComm2.RThreshold:=1;
MSComm2.CommPort:=2;
MSComm2.DTREnable:=true;
MSComm2.RTSEnable:=true;
MSComm2.PortOpen:=true;
Memo2.text:='';
Memo1.text:='';
end;
发送过程:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
s,datastr:string;
begin
s:=memo1.text;
datastr:='';
if MSComm1.PortOpen=false then
showmessage('error!')
else
begin
showmessage('ok!');
for i:=0 to (length(s) div 2)-1 do
begin
datastr:=datastr+chr(strtoint('$'+ copy(s,2*i+1,2)));
end;
begin
MSComm1.output:=datastr;
end;
end;
end;
怎样证明发送成功?
接收过程:
procedure TForm1.Button3Click(Sender: TObject);
var
recstr:string;
strtemp:string;
i:integer;
begin
if Mscomm2.PortOpen=false then
showmessage('error')
else
showmessage('ok');
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=0 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
end;
memo2框中看不到数据,单步执行时发现程序在接收过程中运行到:
if Mscomm2.CommEvent=comEvReceive then就步往下执行了,为什么呢?
希望大家能给我找到错误,再一次感谢大家的光临。
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=0 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
end;
把这段代码放到onComm事件中去看看。
现在可以肯定的是:串口线的连接没有问题.
下面这段代码放在onComm事件中根本就不会执行。
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=0 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
end;
我将其放在一个按钮事件中,点击按钮表示接收。
谢谢你啦。。
如果你愿意我贴代码上来。。