D
DODER
Unregistered / Unconfirmed
GUEST, unregistred user!
我要赶制一个串口接收程序,接收256KB的数据。
按照例程改了个程序,可是一接收就报错,各位FW帮我看看,怎么改,代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Grids, DirOutln, ShellAPI, SPComm;
type
TForm1 = class(TForm)
GroupBox3: TGroupBox;
GroupBox1: TGroupBox;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
ComboBox6: TComboBox;
ComboBox7: TComboBox;
ComboBox8: TComboBox;
ComboBox9: TComboBox;
ComboBox10: TComboBox;
Button2: TButton;
StatusBar1: TStatusBar;
Button3: TButton;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
Button4: TButton;
Button5: TButton;
Label1: TLabel;
GroupBox2: TGroupBox;
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Comm1: TComm;
Button6: TButton;
RadioButton1: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ComboBox6Change(Sender: TObject);
procedure ComboBox7Change(Sender: TObject);
procedure ComboBox8Change(Sender: TObject);
procedure ComboBox9Change(Sender: TObject);
procedure ComboBox10Change(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
viewstring:string;
i:integer;
rbuf:array[1..255] of string;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);{清空显示区}
begin
memo1.Text:='';
edit1.Text:='';
end;
procedure TForm1.Button3Click(Sender: TObject);{清空计数器}
begin
statusbar1.Panels[0].Text:='收到的字节数:0';
end;
procedure TForm1.ComboBox6Change(Sender: TObject);{参数选择}
begin
comm1.CommName:=combobox6.Text;
end;
procedure TForm1.ComboBox7Change(Sender: TObject);{参数选择}
begin
if combobox7.text='9600' then
comm1.BaudRate:=9600;
if combobox7.text='4800' then
comm1.BaudRate:=4800;
if combobox7.text='19200' then
comm1.BaudRate:=19200;
if combobox7.text='57600' then
comm1.BaudRate:=57600;
if combobox7.text='115200' then
comm1.BaudRate:=115200;
end;
procedure TForm1.ComboBox8Change(Sender: TObject);{参数选择}
begin
comm1.ParityCheck:=true;
if combobox8.Text='无NO' then
comm1.Parity:=none;
if combobox8.Text='偶EVEN' then
comm1.Parity:=even;
if combobox8.Text='奇ODD' then
comm1.Parity:=odd;
end;
procedure TForm1.ComboBox9Change(Sender: TObject);{参数选择}
begin
if combobox9.Text='8' then
comm1.ByteSize:=_8;
if combobox9.Text='7' then
comm1.ByteSize:=_7;
if combobox9.Text='6' then
comm1.ByteSize:=_6;
end;
procedure TForm1.ComboBox10Change(Sender: TObject);{参数选择}
begin
if combobox10.Text='1' then
comm1.StopBits:=_1;
if combobox10.Text='2' then
comm1.StopBits:=_2;
end;
procedure TForm1.Button4Click(Sender: TObject);{保存数据}
begin
if memo1.text='' then
showmessage('文件内容为空,还是不存了吧?')
else
with savedialog1 do
if execute then
begin
edit1.Text:=filename;
memo1.Lines.SaveToFile(savedialog1.FileName);
end;
end;
procedure TForm1.Button5Click(Sender: TObject);{打开数据}
begin
opendialog1.FileName:='*.txt';
with opendialog1 do
if execute then
begin
edit1.Text:=filename;
memo1.Lines.LoadFromFile(opendialog1.FileName);
OpenDialog1.HistoryList.Insert(0,OpenDialog1.FileName);
end;
end;
procedure TForm1.Button6Click(Sender: TObject);{ABOUT}
begin
ShellAbout(Application.MainForm.Handle,
'串口接收工具',
'Copyright (C) 2003-2008'#13'Email:tnt_505_cn@qianlong.com',
Application.Icon.Handle);
end;
procedure TForm1.Button2Click(Sender: TObject);{打开和关闭串口}
begin
if button2.Caption='打开串口' then
begin
button2.Caption:='关闭串口';
comm1.StartComm;
radiobutton1.Checked:=true;
end
else
begin
comm1.StopComm;
button2.Caption:='打开串口';
radiobutton1.Checked:=false;
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);{接收数据}
var
i:integer;
begin
viewstring :='';
move(buffer^,rbuf,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+rbuf+'';
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
end.
按照例程改了个程序,可是一接收就报错,各位FW帮我看看,怎么改,代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Grids, DirOutln, ShellAPI, SPComm;
type
TForm1 = class(TForm)
GroupBox3: TGroupBox;
GroupBox1: TGroupBox;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
ComboBox6: TComboBox;
ComboBox7: TComboBox;
ComboBox8: TComboBox;
ComboBox9: TComboBox;
ComboBox10: TComboBox;
Button2: TButton;
StatusBar1: TStatusBar;
Button3: TButton;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
Button4: TButton;
Button5: TButton;
Label1: TLabel;
GroupBox2: TGroupBox;
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Comm1: TComm;
Button6: TButton;
RadioButton1: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ComboBox6Change(Sender: TObject);
procedure ComboBox7Change(Sender: TObject);
procedure ComboBox8Change(Sender: TObject);
procedure ComboBox9Change(Sender: TObject);
procedure ComboBox10Change(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
viewstring:string;
i:integer;
rbuf:array[1..255] of string;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);{清空显示区}
begin
memo1.Text:='';
edit1.Text:='';
end;
procedure TForm1.Button3Click(Sender: TObject);{清空计数器}
begin
statusbar1.Panels[0].Text:='收到的字节数:0';
end;
procedure TForm1.ComboBox6Change(Sender: TObject);{参数选择}
begin
comm1.CommName:=combobox6.Text;
end;
procedure TForm1.ComboBox7Change(Sender: TObject);{参数选择}
begin
if combobox7.text='9600' then
comm1.BaudRate:=9600;
if combobox7.text='4800' then
comm1.BaudRate:=4800;
if combobox7.text='19200' then
comm1.BaudRate:=19200;
if combobox7.text='57600' then
comm1.BaudRate:=57600;
if combobox7.text='115200' then
comm1.BaudRate:=115200;
end;
procedure TForm1.ComboBox8Change(Sender: TObject);{参数选择}
begin
comm1.ParityCheck:=true;
if combobox8.Text='无NO' then
comm1.Parity:=none;
if combobox8.Text='偶EVEN' then
comm1.Parity:=even;
if combobox8.Text='奇ODD' then
comm1.Parity:=odd;
end;
procedure TForm1.ComboBox9Change(Sender: TObject);{参数选择}
begin
if combobox9.Text='8' then
comm1.ByteSize:=_8;
if combobox9.Text='7' then
comm1.ByteSize:=_7;
if combobox9.Text='6' then
comm1.ByteSize:=_6;
end;
procedure TForm1.ComboBox10Change(Sender: TObject);{参数选择}
begin
if combobox10.Text='1' then
comm1.StopBits:=_1;
if combobox10.Text='2' then
comm1.StopBits:=_2;
end;
procedure TForm1.Button4Click(Sender: TObject);{保存数据}
begin
if memo1.text='' then
showmessage('文件内容为空,还是不存了吧?')
else
with savedialog1 do
if execute then
begin
edit1.Text:=filename;
memo1.Lines.SaveToFile(savedialog1.FileName);
end;
end;
procedure TForm1.Button5Click(Sender: TObject);{打开数据}
begin
opendialog1.FileName:='*.txt';
with opendialog1 do
if execute then
begin
edit1.Text:=filename;
memo1.Lines.LoadFromFile(opendialog1.FileName);
OpenDialog1.HistoryList.Insert(0,OpenDialog1.FileName);
end;
end;
procedure TForm1.Button6Click(Sender: TObject);{ABOUT}
begin
ShellAbout(Application.MainForm.Handle,
'串口接收工具',
'Copyright (C) 2003-2008'#13'Email:tnt_505_cn@qianlong.com',
Application.Icon.Handle);
end;
procedure TForm1.Button2Click(Sender: TObject);{打开和关闭串口}
begin
if button2.Caption='打开串口' then
begin
button2.Caption:='关闭串口';
comm1.StartComm;
radiobutton1.Checked:=true;
end
else
begin
comm1.StopComm;
button2.Caption:='打开串口';
radiobutton1.Checked:=false;
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);{接收数据}
var
i:integer;
begin
viewstring :='';
move(buffer^,rbuf,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+rbuf+'';
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
end.