谁帮我解决一下spcomm的问题?(100分)

F

flexsys

Unregistered / Unconfirmed
GUEST, unregistred user!
我用spcomm来处理于单片机的串口通讯,想把spcomm控件和我的一下通讯处理封装在我自己的类里,让多个控制窗口都能共用。单是搞了几天都不行。问题出在TForm1.FormCreate中的MyComm.Create(Self)这一句上,老是出现错误“Access violation at address 00454c8f in module 'Project1.exe'. Read of address 0000000”.下面是我的部分程序,大虾们帮我看看吧
unit COMM;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, SPComm;
type
TOurComm = class (TWinControl)
private
Comm:TComm;
procedure ReceiveCommand(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
public
Constructor Create(AOwner:TComponent);override;
Procedure OpenComm;
Procedure CloseComm;
//procedure SendCommand (command:byte;
datasize:byte;
str:array of byte);
end;
var
MyComm:TOurComm;
implementation
procedure TOurComm.OpenComm;
begin
comm.StartComm;
end;

procedure TOurComm.CloseComm;
begin
Comm.StopComm;
Comm.free;
end;

procedure TOurComm.ReceiveCommand(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
//i:integer;
rbuf:array [0..8] of byte;
begin
//.......
move(buffer^,rbuf,bufferlength);
//.........
end;

constructor TOurComm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
comm:=TComm.Create(Self);
comm.BaudRate:=115200;
comm.ByteSize:=_8;
comm.CommName:='COM1';
comm.Parity:=None;
comm.StopBits:=_1;
comm.OnReceiveData:=ReceiveCommand;
end;
end.

主窗口
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, comm;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
MyComm.Create(Self);
//就是这句出错“Access violation at address......”
MyComm.OpenComm;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
MyComm.closecomm;
MyComm.Destroy;
end;
 
MyComm := TOurComm.create(self)
 
MyComm := TOurComm.create(self)
TOurComm = class (TWinControl)
又不是可视的控件,干吗要从TWinControl继承。。。
从TComponent就行了吧
 
谢谢johui,问题解决。
 

Similar threads

I
回复
0
查看
505
import
I
I
回复
0
查看
480
import
I
I
回复
0
查看
651
import
I
I
回复
0
查看
697
import
I
顶部