山
山药蛋
Unregistered / Unconfirmed
GUEST, unregistred user!
要写个简单的串口通讯的程序,已经在这两个控件上调了一天了,用的底子是网上找来的程序,但都没成功,真头痛,下面是我改动的程序,请各位帮忙看看哪里有错:
1、SPCOMM
//===============
procedure TForm1.FormShow(Sender: TObject);
begin
Comm1.StartComm;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Comm1.StopComm;
end;
procedure TForm1.SendData;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=0 to 1 do
begin
if not comm1.writecommdata(@sbuf,1) then//就是writecommdata一直都False
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
if not commflg then messagedlg('发送失败 !',mterror,[mbyes],0);
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
sbuf[0]:=100;
sbuf[1]:=10;
SendData;
end;
//接收的事件ReceiveData根本不触发,不写了。
//--------Comm1的属性如下-------
object Comm1: TComm
CommName = 'COM2'//我用的是COM2口
BaudRate = 9600
ParityCheck = False
Outx_CtsFlow = False
Outx_DsrFlow = False
DtrControl = DtrEnable
DsrSensitivity = False
TxContinueOnXoff = True
Outx_XonXoffFlow = True
Inx_XonXoffFlow = True
ReplaceWhenParityError = False
IgnoreNullChar = False
RtsControl = RtsEnable
XonLimit = 500
XoffLimit = 500
ByteSize = _8
Parity = None
StopBits = _1
XonChar = #17
XoffChar = #19
ReplacedChar = #0
ReadIntervalTimeout = 100
ReadTotalTimeoutMultiplier = 0
ReadTotalTimeoutConstant = 0
WriteTotalTimeoutMultiplier = 0
WriteTotalTimeoutConstant = 0
Left = 64
Top = 108
end
//===============
2、MSCOMM
//==============
procedure TForm1.FormShow(Sender: TObject);
begin
mscomm1.portopen:=True;
mscomm1.inbuffercount:=0;
mscomm1.Input;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
mscomm1.portopen:=False;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
RX:OleVariant;
SS,SReceive:String;
i,j:Integer;
begin
case mscomm1.commEvent of
comEvSend:
begin
//发送数据正确,内容同SPCOMM
end;
comEvReceive:
begin
j:=mscomm1.inbuffercount;
RX:=mscomm1.Input;
SS:=RX;
SReceive:='';
for i:=1 to j do
begin
SReceive:=SReceive+InttoHex(Byte(SS),2);
end;
Memo1.Lines.Add(SReceive);//根据协议,应收到"65 F0 0C FF"
mscomm1.inbuffercount:=0;//可是实际只收到"65 3F FF 00"
end;
end;//case
end;
//------------
MSComm属性只改动了如下的,其他保持默认值:
RThreshold:1
//===========
补充说明:
1、调试的时候都用“串口调试助手V2.1”验证过,串口设备的响应没有问题,即用“助手”能完全正常的收发数据,而用自己的程序则出现以上的问题。
2、我的编辑环境:WindowsXP_SP1、Delphi7、SPComm 2.5。
3、MSComm在VC6上作了测试,编出的程序没有问题,即没有收数据的错误。
1、SPCOMM
//===============
procedure TForm1.FormShow(Sender: TObject);
begin
Comm1.StartComm;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Comm1.StopComm;
end;
procedure TForm1.SendData;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=0 to 1 do
begin
if not comm1.writecommdata(@sbuf,1) then//就是writecommdata一直都False
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
if not commflg then messagedlg('发送失败 !',mterror,[mbyes],0);
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
sbuf[0]:=100;
sbuf[1]:=10;
SendData;
end;
//接收的事件ReceiveData根本不触发,不写了。
//--------Comm1的属性如下-------
object Comm1: TComm
CommName = 'COM2'//我用的是COM2口
BaudRate = 9600
ParityCheck = False
Outx_CtsFlow = False
Outx_DsrFlow = False
DtrControl = DtrEnable
DsrSensitivity = False
TxContinueOnXoff = True
Outx_XonXoffFlow = True
Inx_XonXoffFlow = True
ReplaceWhenParityError = False
IgnoreNullChar = False
RtsControl = RtsEnable
XonLimit = 500
XoffLimit = 500
ByteSize = _8
Parity = None
StopBits = _1
XonChar = #17
XoffChar = #19
ReplacedChar = #0
ReadIntervalTimeout = 100
ReadTotalTimeoutMultiplier = 0
ReadTotalTimeoutConstant = 0
WriteTotalTimeoutMultiplier = 0
WriteTotalTimeoutConstant = 0
Left = 64
Top = 108
end
//===============
2、MSCOMM
//==============
procedure TForm1.FormShow(Sender: TObject);
begin
mscomm1.portopen:=True;
mscomm1.inbuffercount:=0;
mscomm1.Input;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
mscomm1.portopen:=False;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
RX:OleVariant;
SS,SReceive:String;
i,j:Integer;
begin
case mscomm1.commEvent of
comEvSend:
begin
//发送数据正确,内容同SPCOMM
end;
comEvReceive:
begin
j:=mscomm1.inbuffercount;
RX:=mscomm1.Input;
SS:=RX;
SReceive:='';
for i:=1 to j do
begin
SReceive:=SReceive+InttoHex(Byte(SS),2);
end;
Memo1.Lines.Add(SReceive);//根据协议,应收到"65 F0 0C FF"
mscomm1.inbuffercount:=0;//可是实际只收到"65 3F FF 00"
end;
end;//case
end;
//------------
MSComm属性只改动了如下的,其他保持默认值:
RThreshold:1
//===========
补充说明:
1、调试的时候都用“串口调试助手V2.1”验证过,串口设备的响应没有问题,即用“助手”能完全正常的收发数据,而用自己的程序则出现以上的问题。
2、我的编辑环境:WindowsXP_SP1、Delphi7、SPComm 2.5。
3、MSComm在VC6上作了测试,编出的程序没有问题,即没有收数据的错误。