L
Langrenxilin
Unregistered / Unconfirmed
GUEST, unregistred user!
一个串口通讯的dll,使用SPCOMM控件,打开串口没问题,发送数据时总是不行,哪位指点一下,
源代码:
DLL:
library Project1;
uses
ShareMem,
SysUtils,
Classes,
Windows,
Messages,
spcomm;
{$R *.RES}
var
comm:tcomm;
sbuf,rbuf:array[1..10] of byte;
function opencom(comno:integer):boolean;stdcall;
var
commname:string;
i:integer;
begin
comm := tcomm.create(nil);
result:=false;
commname := 'COM'+inttostr(comno);
comm.CommName := commname;
if comm.handle = 0 then
comm.StartComm;
result:=true;
end;
function send(n:integer):boolean;
begin
sbuf[1]:=byte($00+n);
if not comm.writecommdata(@sbuf[1],1) then
result:=false
else
result:=true;
end;
exports
opencom index 1,
send index 2;
begin
end.
主程序:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function opencom(comno:integer):boolean;stdcall;external 'E:/like/串口/new dllspcom/project1.dll';
function send(n:integer):boolean;stdcall;external 'E:/like/串口/new dllspcom/project1.dll';
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if opencom(1) then
label1.caption:='open com1';
end;
procedure TForm1.Button2Click(Sender: TObject);
var
num:integer;
begin
num:=strtoint(edit1.text);
if send(num) then
label1.caption:='send';
end;
end.
源代码:
DLL:
library Project1;
uses
ShareMem,
SysUtils,
Classes,
Windows,
Messages,
spcomm;
{$R *.RES}
var
comm:tcomm;
sbuf,rbuf:array[1..10] of byte;
function opencom(comno:integer):boolean;stdcall;
var
commname:string;
i:integer;
begin
comm := tcomm.create(nil);
result:=false;
commname := 'COM'+inttostr(comno);
comm.CommName := commname;
if comm.handle = 0 then
comm.StartComm;
result:=true;
end;
function send(n:integer):boolean;
begin
sbuf[1]:=byte($00+n);
if not comm.writecommdata(@sbuf[1],1) then
result:=false
else
result:=true;
end;
exports
opencom index 1,
send index 2;
begin
end.
主程序:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function opencom(comno:integer):boolean;stdcall;external 'E:/like/串口/new dllspcom/project1.dll';
function send(n:integer):boolean;stdcall;external 'E:/like/串口/new dllspcom/project1.dll';
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if opencom(1) then
label1.caption:='open com1';
end;
procedure TForm1.Button2Click(Sender: TObject);
var
num:integer;
begin
num:=strtoint(edit1.text);
if send(num) then
label1.caption:='send';
end;
end.