关于串口接收的问题。 ( 积分: 100 )

  • 主题发起人 主题发起人 DODER
  • 开始时间 开始时间
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.
 
我要赶制一个串口接收程序,接收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.
 
//用数组接收或字串接收
//串口接收
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var i:integer;
rxbuf:array of byte;
s,rxinfo:string;
begin
//setlength(rxbuf,bufferlength);
setlength(s,bufferlength);
//move(buffer^,rxbuf[0],bufferlength);
move(buffer^,s[1],bufferlength);
//s:='';
//for i:=0 to bufferlength-1 do s:=s+Char(rxbuf);
Memo2.Lines.Add(s);
Comm1.WriteCommData(@rxbuf[0],bufferlength);
end;
 
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var i:integer;
s,rxinfo:string;
begin
setlength(s,bufferlength);
move(buffer^,s,bufferlength);
Memo2.Lines.Add(s);
// Comm1.WriteCommData(@rxbuf[0],bufferlength);
end;
楼主最好把出错信息贴上来,
 
2位FW的也是报错,报内存不可读,我更正一下,我要接收一个10MB的数据。
是不是我的数组太小了呢?
 
串口 115200 的速度1s是多少数据?10M是不是有点大压,而且不是文本吧,我[blue]抄[/blue]的这段代码读取文本没有什么问题的
 
chenybin,可我这还是报错,我把源代码给你,你帮我看看能行么?我周围都没有人用DELPHI,或者你有什么例子给我看看好么?你的QQ是?
 
我没有测试环境,委屈一下,留下你的QQ吧
 
我的QQ,21581461
 
最好 把错误也贴出来
 

Similar threads

I
回复
0
查看
686
import
I
I
回复
0
查看
533
import
I
I
回复
0
查看
686
import
I
后退
顶部