J
jxcclij
Unregistered / Unconfirmed
GUEST, unregistred user!
请高手指点
动态连接库说明
HMCommDevDrv使用说明2004年10月3日一、返回码定义:
const int COMM_OK = 0; // 通讯收发正常
const int COMM_RECVERROR = 1; // 设备未响应
const int COMM_CRCERROR = 2; // CRC校验错(已收到设备应答帧)
const int COMM_TIMESTAMPERROR = 3; // 读历史数据时戳错
const int COMM_UNKNOWNPORTTYPE = 4; // 未知端口类型
const int COMM_PORTERROR = 5; // 端口异常
const int COMM_SENDERROR = 6; // 发送失败
const int COMM_PORTNOTOPEN = 7; // 端口未打开
const int COMM_PARATOOLONG = 8; // 读写参数超长(一次读写总长一般不超过32个字节)const int COMM_UNKNOWNPARATYPE = 9; // 未知参数类型
const int COMM_INVALIDPARATYPE = 10; // 非法参数类型(读历史数据时用了非历史参数类型)
const int COMM_INVALIDADDR = 11; // 非法设备地址(一般设备地址在6~254之间)
const int COMM_INVALIDACK = 12; // 无效的应答帧(当应答帧命令字与发送帧命令字不匹配时报该错)
const int COMM_INVALIDTIME = 13; // 时间参数非法(调用读写历史数据接口时)const int COMM_DATAERROR = 14; // 数据非法(调用读写历史数据接口时,一般为超大的异常数值)
const int COMM_PORTOPENED = 15; // 端口已打开
const int COMM_PORTCREATEERROR = 16; // 端口打开失败
const int COMM_PORTPARAERROR = 17;
// 端口初始化参数错误二、动态链接库函数定义:
/* 功能:打开一个串口,在读写这个串口上的设备时必须先打开它 参数: sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4
. . . . . .
iBaudIndex Baud Rate
0 1200
1 2400
2 4800
3 9600
4 19200
5 38400
6 57600
7 115200
dwReadTotalTimeout 读端口总超时时间(毫秒)
dwReadIntervalTimeout 读端口字符间隔超时时间(毫秒)
dwWriteTotalTimeout 写端口总超时时间(毫秒)
dwRetryIntervalTime 重发等待时间间隔(毫秒)
返回值: 0 正常
*/DWORD APIENTRY CreatePort(char *sPortName,
char *sPortPara,
UINT nBaudIndex,
DWORD dwReadTotalTimeout,
DWORD dwReadIntervalTimeout,
DWORD dwWriteTotalTimeout,
DWORD dwRetryIntervalTime);
/* 功能: 当一个串口不再使用时应该关闭它 参数:
sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4
. . . . . .
返回值: 0 正常
*/void APIENTRY ClosePort(char *sPortName);
/* 功能: 在一个已打开的串口上读一台设备的设备标识 参数:
sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4
. . . . . .
iDeviceAddr 现场设备的逻辑地址
sDeviceId 存放设备标识返回值的字符串 返回值:
0 正常 1 不正常
*/DWORD APIENTRY HMGetDeviceId(char *sPortName, int& iDeviceAddr, char *sDeviceId);
/* 功能: 在一个已打开的串口上读一台设备上的一个参数或多个地址连续的参数 参数:
iParaType para value type
0 float
1 double
2 byte
3 history struct “累积流量(double),瞬时流量(float),温度(float),压力(float),差压(float),密度(float),月(int),日(int),时(int),分(int)” 4 time "年,月,日,小时,分,秒" 5 bytes 6 realtime struct1 "累积流量(double), 瞬时流量(float), 温度(float), 压力(float), 密度(float), 差压(float)"
7 realtime struct2 "累积流量(double), 瞬时流量(float), 压力(float), 压力传感器温度(float), 差压(float), 差压传感器温度(float), 温度(float)" sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4 COM4
. . . . . .
iDeviceAddr 现场设备的逻辑地址
dwParaAddr 第一个参数的地址
iParaCount 参数的个数 float 0 ~ 8 double 0 ~ 4 byte 0 ~ 16 struct 1 time 1
sValues 存放参数返回值的字符串,值与值之间用逗号隔开
返回值: 0 正常 1 不正常*/
/*ReadDevicePara为同步读设备数据的函数,函数返回时设备中数据一起返回;ReadDeviceParaA,、ReadDeviceParaB为异步读设备数据的函数,ReadDeviceParaA发出读设备数据的请求后立即返回,等待一段时间后再使用ReadDeviceParaB函数读取设备返回的数据,这两个函数可以用来实现并行访问多个串口的设备。*/DWORD APIENTRY ReadDevicePara(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
DWORD APIENTRY ReadDeviceParaA(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
DWORD APIENTRY ReadDeviceParaB(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
/* 功能: 在一个已打开的串口上写一台设备上的一个参数或多个地址连续的参数 参数:
iParaType para value type
0 float
1 double
2 byte
3 history data struct “累积流量(double),瞬时流量(float),温度(float),压力(float),差压(float),密度(float),月(int),日(int),时(int),分(int)” 4 time "年,月,日,小时,分,秒" 5 bytes sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4 . . . . . . iDeviceAddr 现场设备的逻辑地址
dwParaAddr 第一个参数的地址
iParaCount 参数的个数 float 0 ~ 8 double 0 ~ 4 byte 0 ~ 16 sValues 存放参数设定值的字符串,值与值之间用逗号隔开
返回值: 0 正常 1 不正常*/
DWORD APIENTRY WriteDevicePara(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
delphi 7.0 程序源代码
unit Unit1;
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
HSMC: TLabel;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
HSFHZ: TLabel;
CSFHZ: TLabel;
Edtint1: TEdit;
Edtint2: TEdit;
Edtint3: TEdit;
Button8: TButton;
Edtmax: TEdit;
Edtmin: TEdit;
Label1: TLabel;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;implementationtype
//自己用c作的连接库
TMin1=function(x,y,z:Integer;out sschar):Integer; cdecl;
TMax1=function(x,y,z:Integer;out sschar):Integer; cdecl;
type
TCreateCommPort = function(sPortNamechar;
nBaudIndex:integer;
dwReadTotalTimeout: longword;
dwReadIntervalTimeout:longword;
dwWriteTotalTimeout:longword;
dwRetryIntervalTime:longword):longword; stdcall;
type TCloseCommPort = function(sPortNamechar): longword; stdcall; //在一个已打开的串口上读一台设备的设备标识
type
THMGetDeviceId=function( sPortNamechar;
iDeviceAddrint;
sDeviceIdchar)WORD;stdcall; //在一个已打开的串口上读一台设备上的一个参数或多个地址连续的参数
type
TReadDevicePara= function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
type
TReadDeviceParaA= function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
type TReadDeviceParaB= function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
//在一个已打开的串口上写一台设备上的一个参数或多个地址连续的参数
type TWriteDevicePara=function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
CreateCommPort:TCreateCommPort;
IWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
@CreateCommPort := GetProcAddress(OneHandle,'CreateCommPort');
if (@CreateCommPort <> nil) then
begin
I:=CreateCommPort('COM1',3,4000,500,500,1000);
if I=0 then
begin
showmessage('打开串口成功');
end
ELSE
BEGIN
showmessage('打开串口失败');
END;
HSMC.Caption := 'CreateCommPort';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:='';
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ClosePort:TCloseCommPort;
IWORD;
portnamechar;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
@ClosePort := GetProcAddress(OneHandle,'CloseCommPort');
if not (@ClosePort = nil) then
BEGIN
portname:=pchar('COM1');
I:=ClosePort('COM1');
HSMC.Caption := 'CloseCommPort';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:='';
END
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
HMGetDeviceId:THMGetDeviceId;
retstrchar; IWORD;
addrint;
count:integer;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
@HMGetDeviceId := GetProcAddress(OneHandle,'HMGetDeviceId');
if (@HMGetDeviceId <> nil) then
begin
count:=7;
addr:=@count;
I:=HMGetDeviceId(Pchar('COM1'),addr,retstr);
HSMC.Caption := 'HMGetDeviceId';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
WriteDevicePara:TFarProc;
retstrchar;
IWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
WriteDevicePara := GetProcAddress(OneHandle,'WriteDevicePara');
if (WriteDevicePara <> nil) then
begin
I:=TWriteDevicePara(WriteDevicePara)
(7,Pchar'COM1'),5,16711796,1,retstr);
HSMC.Caption := 'WriteDevicePara';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:='';
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ReadDevicePara:TFarProc;
retstrchar;
IWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
ReadDevicePara := GetProcAddress(OneHandle,'ReadDevicePara');
if not (ReadDevicePara = nil) then
begin
I:=TReadDevicePara(ReadDevicePara)(6,Pchar
('COM1'),10,32,1,retstr);
HSMC.Caption := 'ReadDevicePara';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ReadDeviceParaA:TFarProc;
retstrchar;
iWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
ReadDeviceParaA := GetProcAddress(OneHandle,'ReadDeviceParaA');
if not (ReadDeviceParaA = nil) then
begin
I:=TReadDeviceParaA(ReadDeviceParaA)(6,Pchar
('COM1'),10,32,1,retstr);
HSMC.Caption := 'ReadDeviceParaA';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button7Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ReadDeviceParaB:TFarProc;
retstrchar;
iWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
ReadDeviceParaB := GetProcAddress(OneHandle,'ReadDeviceParaB');
if not (ReadDeviceParaB = nil) then
begin
I:=TReadDeviceParaB(ReadDeviceParaB)(6,Pchar
('COM1'),1,32,1,retstr);
HSMC.Caption := 'ReadDeviceParaB';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button8Click(Sender: TObject);
var
Handle:THandle;
Min1:TFarProc;
Max1:TFarProc;
sschar;
begin
Handle:=LoadLibrary('MaxMin.dll'); //将"MaxMin.dll"的文件映象映射进调用进程的地址空间
if Handle<>0 then
begin
Min1:=GetProcAddress(Handle,'Min1'); //取得DLL中函数Min1( )的地址
Max1:=GetProcAddress(Handle,'Max1'); //取得DLL中函数Max1( )的地址
if (Min1<>nil) and (Min1<>nil) then
begin
edtMin.Text:=IntToStr(TMin1(Min1)(StrToInt(edtInt1.Text),
StrToInt(edtInt2.Text),StrToInt(edtInt3.Text),pchar(ss)));
//调用动态链接库中的函数Min1
label1.Caption:=pchar(ss);
edtMax.Text:=IntToStr(TMax1(Max1)(StrToInt(edtInt1.Text),
StrToInt(edtInt2.Text),StrToInt(edtInt3.Text),pchar(ss))); //调用动态链接库中的函数Max1
label5.Caption:=pchar(ss);
end
else
ShowMessage('调用函数"GetProcAddress"时出错!');
FreeLibrary(Handle); //从进程的地址空间中解除"MaxMin.dll"文件的映射
end;
end;
end.
打开串口后 不能关闭 读写设备的函数都不能使用 返回值为 1 而且串口根本没有数据
动态连接库不是我做的,是另外一个厂家给的。我也没有源代码;调用函数都是正常的就是实现不了描述的功能,我用的是stdcall char* 定义成了pchar 可是没有返回值的。
动态连接库说明
HMCommDevDrv使用说明2004年10月3日一、返回码定义:
const int COMM_OK = 0; // 通讯收发正常
const int COMM_RECVERROR = 1; // 设备未响应
const int COMM_CRCERROR = 2; // CRC校验错(已收到设备应答帧)
const int COMM_TIMESTAMPERROR = 3; // 读历史数据时戳错
const int COMM_UNKNOWNPORTTYPE = 4; // 未知端口类型
const int COMM_PORTERROR = 5; // 端口异常
const int COMM_SENDERROR = 6; // 发送失败
const int COMM_PORTNOTOPEN = 7; // 端口未打开
const int COMM_PARATOOLONG = 8; // 读写参数超长(一次读写总长一般不超过32个字节)const int COMM_UNKNOWNPARATYPE = 9; // 未知参数类型
const int COMM_INVALIDPARATYPE = 10; // 非法参数类型(读历史数据时用了非历史参数类型)
const int COMM_INVALIDADDR = 11; // 非法设备地址(一般设备地址在6~254之间)
const int COMM_INVALIDACK = 12; // 无效的应答帧(当应答帧命令字与发送帧命令字不匹配时报该错)
const int COMM_INVALIDTIME = 13; // 时间参数非法(调用读写历史数据接口时)const int COMM_DATAERROR = 14; // 数据非法(调用读写历史数据接口时,一般为超大的异常数值)
const int COMM_PORTOPENED = 15; // 端口已打开
const int COMM_PORTCREATEERROR = 16; // 端口打开失败
const int COMM_PORTPARAERROR = 17;
// 端口初始化参数错误二、动态链接库函数定义:
/* 功能:打开一个串口,在读写这个串口上的设备时必须先打开它 参数: sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4
. . . . . .
iBaudIndex Baud Rate
0 1200
1 2400
2 4800
3 9600
4 19200
5 38400
6 57600
7 115200
dwReadTotalTimeout 读端口总超时时间(毫秒)
dwReadIntervalTimeout 读端口字符间隔超时时间(毫秒)
dwWriteTotalTimeout 写端口总超时时间(毫秒)
dwRetryIntervalTime 重发等待时间间隔(毫秒)
返回值: 0 正常
*/DWORD APIENTRY CreatePort(char *sPortName,
char *sPortPara,
UINT nBaudIndex,
DWORD dwReadTotalTimeout,
DWORD dwReadIntervalTimeout,
DWORD dwWriteTotalTimeout,
DWORD dwRetryIntervalTime);
/* 功能: 当一个串口不再使用时应该关闭它 参数:
sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4
. . . . . .
返回值: 0 正常
*/void APIENTRY ClosePort(char *sPortName);
/* 功能: 在一个已打开的串口上读一台设备的设备标识 参数:
sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4
. . . . . .
iDeviceAddr 现场设备的逻辑地址
sDeviceId 存放设备标识返回值的字符串 返回值:
0 正常 1 不正常
*/DWORD APIENTRY HMGetDeviceId(char *sPortName, int& iDeviceAddr, char *sDeviceId);
/* 功能: 在一个已打开的串口上读一台设备上的一个参数或多个地址连续的参数 参数:
iParaType para value type
0 float
1 double
2 byte
3 history struct “累积流量(double),瞬时流量(float),温度(float),压力(float),差压(float),密度(float),月(int),日(int),时(int),分(int)” 4 time "年,月,日,小时,分,秒" 5 bytes 6 realtime struct1 "累积流量(double), 瞬时流量(float), 温度(float), 压力(float), 密度(float), 差压(float)"
7 realtime struct2 "累积流量(double), 瞬时流量(float), 压力(float), 压力传感器温度(float), 差压(float), 差压传感器温度(float), 温度(float)" sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4 COM4
. . . . . .
iDeviceAddr 现场设备的逻辑地址
dwParaAddr 第一个参数的地址
iParaCount 参数的个数 float 0 ~ 8 double 0 ~ 4 byte 0 ~ 16 struct 1 time 1
sValues 存放参数返回值的字符串,值与值之间用逗号隔开
返回值: 0 正常 1 不正常*/
/*ReadDevicePara为同步读设备数据的函数,函数返回时设备中数据一起返回;ReadDeviceParaA,、ReadDeviceParaB为异步读设备数据的函数,ReadDeviceParaA发出读设备数据的请求后立即返回,等待一段时间后再使用ReadDeviceParaB函数读取设备返回的数据,这两个函数可以用来实现并行访问多个串口的设备。*/DWORD APIENTRY ReadDevicePara(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
DWORD APIENTRY ReadDeviceParaA(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
DWORD APIENTRY ReadDeviceParaB(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
/* 功能: 在一个已打开的串口上写一台设备上的一个参数或多个地址连续的参数 参数:
iParaType para value type
0 float
1 double
2 byte
3 history data struct “累积流量(double),瞬时流量(float),温度(float),压力(float),差压(float),密度(float),月(int),日(int),时(int),分(int)” 4 time "年,月,日,小时,分,秒" 5 bytes sPortName Comm Port
COM1 COM1
COM2 COM2
COM3 COM3
COM4 . . . . . . iDeviceAddr 现场设备的逻辑地址
dwParaAddr 第一个参数的地址
iParaCount 参数的个数 float 0 ~ 8 double 0 ~ 4 byte 0 ~ 16 sValues 存放参数设定值的字符串,值与值之间用逗号隔开
返回值: 0 正常 1 不正常*/
DWORD APIENTRY WriteDevicePara(int iParaType, char *sPortName, int iDeviceAddr, DWORD dwParaAddr, int iParaCount, char *sValues);
delphi 7.0 程序源代码
unit Unit1;
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
HSMC: TLabel;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
HSFHZ: TLabel;
CSFHZ: TLabel;
Edtint1: TEdit;
Edtint2: TEdit;
Edtint3: TEdit;
Button8: TButton;
Edtmax: TEdit;
Edtmin: TEdit;
Label1: TLabel;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;implementationtype
//自己用c作的连接库
TMin1=function(x,y,z:Integer;out sschar):Integer; cdecl;
TMax1=function(x,y,z:Integer;out sschar):Integer; cdecl;
type
TCreateCommPort = function(sPortNamechar;
nBaudIndex:integer;
dwReadTotalTimeout: longword;
dwReadIntervalTimeout:longword;
dwWriteTotalTimeout:longword;
dwRetryIntervalTime:longword):longword; stdcall;
type TCloseCommPort = function(sPortNamechar): longword; stdcall; //在一个已打开的串口上读一台设备的设备标识
type
THMGetDeviceId=function( sPortNamechar;
iDeviceAddrint;
sDeviceIdchar)WORD;stdcall; //在一个已打开的串口上读一台设备上的一个参数或多个地址连续的参数
type
TReadDevicePara= function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
type
TReadDeviceParaA= function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
type TReadDeviceParaB= function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
//在一个已打开的串口上写一台设备上的一个参数或多个地址连续的参数
type TWriteDevicePara=function( iParaType:integer;
sPortNamechar;
iDeviceAddr:integer;
dwParaAddrWORD;
iParaCount:integer;
sValueschar)WORD; stdcall;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
CreateCommPort:TCreateCommPort;
IWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
@CreateCommPort := GetProcAddress(OneHandle,'CreateCommPort');
if (@CreateCommPort <> nil) then
begin
I:=CreateCommPort('COM1',3,4000,500,500,1000);
if I=0 then
begin
showmessage('打开串口成功');
end
ELSE
BEGIN
showmessage('打开串口失败');
END;
HSMC.Caption := 'CreateCommPort';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:='';
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ClosePort:TCloseCommPort;
IWORD;
portnamechar;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
@ClosePort := GetProcAddress(OneHandle,'CloseCommPort');
if not (@ClosePort = nil) then
BEGIN
portname:=pchar('COM1');
I:=ClosePort('COM1');
HSMC.Caption := 'CloseCommPort';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:='';
END
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
HMGetDeviceId:THMGetDeviceId;
retstrchar; IWORD;
addrint;
count:integer;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
@HMGetDeviceId := GetProcAddress(OneHandle,'HMGetDeviceId');
if (@HMGetDeviceId <> nil) then
begin
count:=7;
addr:=@count;
I:=HMGetDeviceId(Pchar('COM1'),addr,retstr);
HSMC.Caption := 'HMGetDeviceId';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
WriteDevicePara:TFarProc;
retstrchar;
IWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
WriteDevicePara := GetProcAddress(OneHandle,'WriteDevicePara');
if (WriteDevicePara <> nil) then
begin
I:=TWriteDevicePara(WriteDevicePara)
(7,Pchar'COM1'),5,16711796,1,retstr);
HSMC.Caption := 'WriteDevicePara';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:='';
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ReadDevicePara:TFarProc;
retstrchar;
IWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
ReadDevicePara := GetProcAddress(OneHandle,'ReadDevicePara');
if not (ReadDevicePara = nil) then
begin
I:=TReadDevicePara(ReadDevicePara)(6,Pchar
('COM1'),10,32,1,retstr);
HSMC.Caption := 'ReadDevicePara';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ReadDeviceParaA:TFarProc;
retstrchar;
iWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
ReadDeviceParaA := GetProcAddress(OneHandle,'ReadDeviceParaA');
if not (ReadDeviceParaA = nil) then
begin
I:=TReadDeviceParaA(ReadDeviceParaA)(6,Pchar
('COM1'),10,32,1,retstr);
HSMC.Caption := 'ReadDeviceParaA';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button7Click(Sender: TObject);
var
OneHandle : THandle; //定义一个句柄变量
ReadDeviceParaB:TFarProc;
retstrchar;
iWORD;
begin
OneHandle := LoadLibrary('HmCommDevDrv.dll'); //动态载入DLL,并返回其句柄
try
if OneHandle <> 0 then //如果载入成功则获取函数的地址
ReadDeviceParaB := GetProcAddress(OneHandle,'ReadDeviceParaB');
if not (ReadDeviceParaB = nil) then
begin
I:=TReadDeviceParaB(ReadDeviceParaB)(6,Pchar
('COM1'),1,32,1,retstr);
HSMC.Caption := 'ReadDeviceParaB';
HSFHZ.Caption:=inttostr(I);
CSFHZ.Caption:=retstr;
end
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
end;
end;
procedure TForm1.Button8Click(Sender: TObject);
var
Handle:THandle;
Min1:TFarProc;
Max1:TFarProc;
sschar;
begin
Handle:=LoadLibrary('MaxMin.dll'); //将"MaxMin.dll"的文件映象映射进调用进程的地址空间
if Handle<>0 then
begin
Min1:=GetProcAddress(Handle,'Min1'); //取得DLL中函数Min1( )的地址
Max1:=GetProcAddress(Handle,'Max1'); //取得DLL中函数Max1( )的地址
if (Min1<>nil) and (Min1<>nil) then
begin
edtMin.Text:=IntToStr(TMin1(Min1)(StrToInt(edtInt1.Text),
StrToInt(edtInt2.Text),StrToInt(edtInt3.Text),pchar(ss)));
//调用动态链接库中的函数Min1
label1.Caption:=pchar(ss);
edtMax.Text:=IntToStr(TMax1(Max1)(StrToInt(edtInt1.Text),
StrToInt(edtInt2.Text),StrToInt(edtInt3.Text),pchar(ss))); //调用动态链接库中的函数Max1
label5.Caption:=pchar(ss);
end
else
ShowMessage('调用函数"GetProcAddress"时出错!');
FreeLibrary(Handle); //从进程的地址空间中解除"MaxMin.dll"文件的映射
end;
end;
end.
打开串口后 不能关闭 读写设备的函数都不能使用 返回值为 1 而且串口根本没有数据
动态连接库不是我做的,是另外一个厂家给的。我也没有源代码;调用函数都是正常的就是实现不了描述的功能,我用的是stdcall char* 定义成了pchar 可是没有返回值的。