帮忙改错~串口接收问题。倾家荡产!!!! ( 积分: 200 )

  • 主题发起人 主题发起人 DODER
  • 开始时间 开始时间
D

DODER

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了个串口接收程序(SPCOM),打算接收单片机发送的数据,HEX文件。
1:但是程序在触发TForm1.Comm1ReceiveData时报错。
单步调试在viewstring:=viewstring+rbuf;这句报错,显示内存为只读,不可写。
2:如果我要接收一个20MB左右的文件,用SPCOM也可以么?115200的波特率
3:能否贴出你们的电邮,我把原代码发给你们,我这边催的好急啊~~~

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='' ;
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+rbuf;
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;

请各位帮忙看看~


下面是原代码:
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..9999] 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);
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^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+rbuf;
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
end.
 
我写了个串口接收程序(SPCOM),打算接收单片机发送的数据,HEX文件。
1:但是程序在触发TForm1.Comm1ReceiveData时报错。
单步调试在viewstring:=viewstring+rbuf;这句报错,显示内存为只读,不可写。
2:如果我要接收一个20MB左右的文件,用SPCOM也可以么?115200的波特率
3:能否贴出你们的电邮,我把原代码发给你们,我这边催的好急啊~~~

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='' ;
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+rbuf;
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;

请各位帮忙看看~


下面是原代码:
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..9999] 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);
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^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+rbuf;
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
end.
 
tseug@263.net
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1350928
标题: 使用Spcomm的Comm1.WriteCommByte,接受时怎么样才能转换为byte?程序(有关图象传送)贴出,帮忙解决问题的再加50分!!!

var
s : string;
begin
SetLength(s, bufferlength);
move(buffer^,pchar(S)^,bufferlength);
end;

---------------------------------

这样不会出错吧?没有处理任何东西呀
 
问题应该在rbuf上
正确的应该写成这样。
var
rbuf:string;
begin
setlength(rbuf,bufferlength);
....

end;
 
string rbuf
setLength(rbuf,bufflength);
CopyMemory(@rbuf[1],buffer,bufflength)
 
多人接受答案了。
 
后退
顶部