今天因为工作需要,原先调用动态库的方法已经写死了,只有两个字符串参数,现在想把串口对象传到动态库里面去,在动态库里面直接收发数据,经过百度一番,重要出了下面的解决方案,不多说 上源码
Q群 Delphi Home 235236282,欢迎delphi 爱好者加入,一起学习、进步。
unit umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
cm1: TComm;
btn1: TButton;
btn2: TButton;
btn3: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
lsHandle: string;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
lsHandle := IntToStr(THandle(cm1));
end;
procedure TForm1.btn2Click(Sender: TObject);
var
cm: TComm;
lihandle: Integer;
begin
lihandle := StrToInt(lsHandle);
cm := TComm(lihandle);
ShowMessage(inttostr(cm.BaudRate));
cm.BaudRate:=7200;
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
ShowMessage(inttostr(cm1.BaudRate));
end;
end.
object Form1: TForm1
Left = 192
Top = 130
Width = 272
Height = 170
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object btn1: TButton
Left = 56
Top = 8
Width = 75
Height = 25
Caption = '保存句柄'
TabOrder = 0
OnClick = btn1Click
end
object btn2: TButton
Left = 56
Top = 40
Width = 75
Height = 25
Caption = '还原对象'
TabOrder = 1
OnClick = btn2Click
end
object btn3: TButton
Left = 56
Top = 72
Width = 169
Height = 25
Caption = '验证是否同一个对象'
TabOrder = 2
OnClick = btn3Click
end
object cm1: TComm
CommName = 'COM2'
BaudRate = 4800
ParityCheck = False
Outx_CtsFlow = False
Outx_DsrFlow = False
DtrControl = DtrEnable
DsrSensitivity = False
TxContinueOnXoff = True
Outx_XonXoffFlow = True
Inx_XonXoffFlow = True
ReplaceWhenParityError = False
IgnoreNullChar = False
RtsControl = RtsEnable
XonLimit = 500
XoffLimit = 500
ByteSize = _8
Parity = None
StopBits = _1
XonChar = #17
XoffChar = #19
ReplacedChar = #0
ReadIntervalTimeout = 100
ReadTotalTimeoutMultiplier = 0
ReadTotalTimeoutConstant = 0
WriteTotalTimeoutMultiplier = 0
WriteTotalTimeoutConstant = 0
Left = 16
Top = 8
end
end
Q群 Delphi Home 235236282,欢迎delphi 爱好者加入,一起学习、进步。