Q
qikefei
Unregistered / Unconfirmed
GUEST, unregistred user!
本人用一台计算机通过RS232/RS485转接口连接一台智能电表(MODBUS协议),结果向电表发送一个读寄存器的字符串却没有反映。但是我用一个串口测试软件Bitboy向串口发送相同的字符串就有返回值。想来想去也不知道个所以然,请各位大侠指教。谢谢!程序源代码如下:
unit Mainfrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, SPComm, ExtCtrls, ComCtrls;
type
TfrmComm = class(TForm)
Comm: TComm;
StatusBar1: TStatusBar;
Panel1: TPanel;
Panel2: TPanel;
mmoComm: TMemo;
btnOpen: TBitBtn;
btnClose: TBitBtn;
btnSend: TBitBtn;
Panel3: TPanel;
edtSendText: TEdit;
Label6: TLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
cmbByteSize: TComboBox;
cmbStopBits: TComboBox;
cmbParity: TComboBox;
edtCommName: TEdit;
edtBaudRate: TEdit;
procedure btnOpenClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure CommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure edtBaudRateExit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure cmbByteSizeChange(Sender: TObject);
procedure cmbStopBitsChange(Sender: TObject);
procedure cmbParityChange(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmComm: TfrmComm;
implementation
{$R *.dfm}
//当窗体创建时,初始化参数
procedure TfrmComm.FormCreate(Sender: TObject);
begin
edtCommName.Text := 'COM1';
edtBaudRate.Text := '9600';
cmbByteSize.ItemIndex := 3; //8位
cmbStopbits.ItemIndex := 0; //1位
cmbParity.ItemIndex := 1; //奇校验
end;
//打开端口
procedure TfrmComm.btnOpenClick(Sender: TObject);
begin
//设置串口参数
comm.CommName := edtCommName.Text;
comm.BaudRate := strToInt(edtBaudRate.Text);
comm.ByteSize := TByteSize(cmbByteSize.ItemIndex);
comm.StopBits := TStopBits(cmbStopBits.ItemIndex);
comm.Parity := TParity(cmbParity.ItemIndex);
//打开串口
try
Comm.StartComm;
except
raise Exception.Create('打开串口错误');
end;
statusbar1.Panels[0].Text := Comm.CommName + '已打开';
statusbar1.Refresh;
btnOpen.Enabled := False;
btnSend.Enabled := True;
btnClose.Enabled := True;
end;
//关闭端口
procedure TfrmComm.btnCloseClick(Sender: TObject);
begin
Comm.StopComm;
statusbar1.Panels[0].Text := Comm.CommName + '已关闭';
statusbar1.Refresh;
end;
//向端口发送数据
procedure TfrmComm.btnSendClick(Sender: TObject);
var
str: Pchar;
Count: integer;
begin
str := Pchar(edtSendText.Text);
Count := Length(str);
if Comm.WriteCommData(str, Count) then
mmoComm.Lines.Add('已发送' + intTostr(Count) + '个字符')
else
raise exception.Create('发送错误');
end;
//从端口接收数据
procedure TfrmComm.CommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
strRecv : string;
begin
setLength(strRecv,BufferLength);
Move(Buffer^,pchar(strRecv)^,BufferLength);
mmoComm.Lines.Add('已收到:'+intTostr(BufferLength)+'字节的数据');
mmoComm.Lines.Add(strRecv);
mmoComm.Invalidate ;
end;
//对波特率进行有效性检验
procedure TfrmComm.edtBaudRateExit(Sender: TObject);
var
i: integer;
begin
try
i := strToInt(edtBaudRate.Text)
except
edtBaudRate.setfocus;
raise Exception.Create('波特率设置错误');
end;
//改变串口的波特率
comm.BaudRate := StrToInt(edtBaudRate.Text);
end;
procedure TfrmComm.cmbByteSizeChange(Sender: TObject);
begin
//改变串口的数据位
comm.ByteSize := TByteSize(cmbByteSize.ItemIndex);
end;
procedure TfrmComm.cmbStopBitsChange(Sender: TObject);
begin
//改变串口的停止位
comm.StopBits := TStopBits(cmbStopBits.ItemIndex);
end;
procedure TfrmComm.cmbParityChange(Sender: TObject);
begin
//设置串口的奇偶校验位
comm.Parity := TParity(cmbParity.ItemIndex);
end;
end.
unit Mainfrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, SPComm, ExtCtrls, ComCtrls;
type
TfrmComm = class(TForm)
Comm: TComm;
StatusBar1: TStatusBar;
Panel1: TPanel;
Panel2: TPanel;
mmoComm: TMemo;
btnOpen: TBitBtn;
btnClose: TBitBtn;
btnSend: TBitBtn;
Panel3: TPanel;
edtSendText: TEdit;
Label6: TLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
cmbByteSize: TComboBox;
cmbStopBits: TComboBox;
cmbParity: TComboBox;
edtCommName: TEdit;
edtBaudRate: TEdit;
procedure btnOpenClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure CommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure edtBaudRateExit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure cmbByteSizeChange(Sender: TObject);
procedure cmbStopBitsChange(Sender: TObject);
procedure cmbParityChange(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmComm: TfrmComm;
implementation
{$R *.dfm}
//当窗体创建时,初始化参数
procedure TfrmComm.FormCreate(Sender: TObject);
begin
edtCommName.Text := 'COM1';
edtBaudRate.Text := '9600';
cmbByteSize.ItemIndex := 3; //8位
cmbStopbits.ItemIndex := 0; //1位
cmbParity.ItemIndex := 1; //奇校验
end;
//打开端口
procedure TfrmComm.btnOpenClick(Sender: TObject);
begin
//设置串口参数
comm.CommName := edtCommName.Text;
comm.BaudRate := strToInt(edtBaudRate.Text);
comm.ByteSize := TByteSize(cmbByteSize.ItemIndex);
comm.StopBits := TStopBits(cmbStopBits.ItemIndex);
comm.Parity := TParity(cmbParity.ItemIndex);
//打开串口
try
Comm.StartComm;
except
raise Exception.Create('打开串口错误');
end;
statusbar1.Panels[0].Text := Comm.CommName + '已打开';
statusbar1.Refresh;
btnOpen.Enabled := False;
btnSend.Enabled := True;
btnClose.Enabled := True;
end;
//关闭端口
procedure TfrmComm.btnCloseClick(Sender: TObject);
begin
Comm.StopComm;
statusbar1.Panels[0].Text := Comm.CommName + '已关闭';
statusbar1.Refresh;
end;
//向端口发送数据
procedure TfrmComm.btnSendClick(Sender: TObject);
var
str: Pchar;
Count: integer;
begin
str := Pchar(edtSendText.Text);
Count := Length(str);
if Comm.WriteCommData(str, Count) then
mmoComm.Lines.Add('已发送' + intTostr(Count) + '个字符')
else
raise exception.Create('发送错误');
end;
//从端口接收数据
procedure TfrmComm.CommReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
strRecv : string;
begin
setLength(strRecv,BufferLength);
Move(Buffer^,pchar(strRecv)^,BufferLength);
mmoComm.Lines.Add('已收到:'+intTostr(BufferLength)+'字节的数据');
mmoComm.Lines.Add(strRecv);
mmoComm.Invalidate ;
end;
//对波特率进行有效性检验
procedure TfrmComm.edtBaudRateExit(Sender: TObject);
var
i: integer;
begin
try
i := strToInt(edtBaudRate.Text)
except
edtBaudRate.setfocus;
raise Exception.Create('波特率设置错误');
end;
//改变串口的波特率
comm.BaudRate := StrToInt(edtBaudRate.Text);
end;
procedure TfrmComm.cmbByteSizeChange(Sender: TObject);
begin
//改变串口的数据位
comm.ByteSize := TByteSize(cmbByteSize.ItemIndex);
end;
procedure TfrmComm.cmbStopBitsChange(Sender: TObject);
begin
//改变串口的停止位
comm.StopBits := TStopBits(cmbStopBits.ItemIndex);
end;
procedure TfrmComm.cmbParityChange(Sender: TObject);
begin
//设置串口的奇偶校验位
comm.Parity := TParity(cmbParity.ItemIndex);
end;
end.