基于欧姆龙hostLink协议的串口通信DLL V3.2版 点击:2625 | 回复:5



wjun7610

    
  • 精华:0帖
  • 求助:0帖
  • 帖子:14帖 | 199回
  • 年度积分:0
  • 历史总积分:436
  • 注册:2004年2月01日
发表于:2011-04-05 15:10:06
楼主

Omron_HostLink.dll V3.2 HostLink通信协议串口通讯动态链接库DLL(以下简称DLL),是为满足工业通信需要, 

针对工业领域要求上位机对欧姆龙系列PLC通讯实时采集与控制的组态编程而设计。 

本DLL是采用Delphi语言开发的标准串口通讯库,具有以下特点: 

1)、遵循欧姆龙HostLink通信协议; 

2)、支持多串口并发操作,支持多线程串口通讯,将串口通讯对主程序的影响降至最低; 

3)、实时性、可靠性好,通用性强; 

4)、适用于多PLC(下位机)联网和上位机通信,满足多方面的需要; 

5)、函数接口功能全,操作简单,支持欧姆龙PLC的绝大部分地址的读写功能函数; 

6)、支持USB、PC扩展卡等扩展串口号; 

7)、支持多种操作系统win9x/win2000/winXP(标准Win32 DLL); 

8)、易于快速开发(VC等非RAD开发环境的开发); 

9)、更新原有函数调用方式采用数组调用数据发生与接收,初学者快速入门; 

10)、可在多种编程环境下使用,例如VB、VC、Delphi、PB、Labview、易语言等开发环境; 

11)、扩展了函数功能读取下位机中的双字、单精度浮点等类型数据,更加符合工业自动化领域的工控软件的开发。 

软件版本更新及相关说明: 

Omron_HostLink.dll V3.2 HostLink通信协议串口通讯链接库,实现了对欧姆 龙全系列PLC的HostLink协议的支持, 

(删减了一些特权指令)wangjun于2010年在原有omron.dll V2.0版基础上的更新版,重写了函数结构,采用数据接收 

和发送缓冲区(数组)方式传递,读出和写入单元数据使用更方便。对于16位整数和32位整数读取写入均按有符号整数 

处理,小数的返回按单精度浮点型(2进制浮点)处理读取方式。提供扩展功能函数,方便用户实现混合数据读取。 

能够同时满足32个串口并发情况下的正常使用。 

Omron_HostLink.dll V3.2 分为完美版和单机版两个版本,本DLL于2010年最新规整优化了部分源码, 

通过DLL发送缓存区与接收缓存区数据传递的功能扩展,使多语言环境的使用更加便利。 

全新的串口通信DLL控件,完美的VB、VC、delphi例程及工件手册是,广大立志利用各类高级语言进行上位机软件开发的首先工具。

 

注意:实例在使用时不能超出试用版的地址限制,否则无法正确通讯。

电 话:13912935690 025-84359545 

腾讯QQ:157610979 

邮 箱:157610979@QQ.com 

wjun7610@yahoo.com.cn 

chinawanglong@163.com 


正在下载,请等待……
下载附件需0积分!




wjun7610

  • 精华:0帖
  • 求助:0帖
  • 帖子:14帖 | 199回
  • 年度积分:0
  • 历史总积分:436
  • 注册:2004年2月01日
发表于:2011-04-05 15:11:29
1楼
在VC中使用DLL一般都是采用动态声明的方式,函数说明中给出的是Delphi的函数原型,

在VC中声明时只要注意一下类型的对应即可,Delphi中的longint类型对应VC中的int类型

Delphi中的Pchar对应VC中的char* ,下面给出主要函数的声明:

在使用的文件的cpp中声明一个句柄:

HINSTANCE hinstDLL;

用来标识导入的动态链接库。

1)、按下例说明声明相关各个函数:(在cpp文件的头处声明)

typedef int (_stdcall *pOpen)(int nport, int BaudRate, int DataBits, char* Parity, int StopBits, char* User);
typedef int (_stdcall *pClose)(int nport);
typedef int (_stdcall *pSetDelay)(int nport);
typedef int (_stdcall *pComTrue)(int nport);
typedef int (_stdcall *pComWork)(int nport);

typedef int (_stdcall *pMS)(int nport, int node);
typedef int (_stdcall *pSC)(int nport, int node, int State);
typedef int (_stdcall *pMM)(int nport, int node);
typedef int (_stdcall *pTS)(int nport, int node);
typedef int (_stdcall *pReadInt)(int nport, int node, int address, int Count, char* Order, int* RxdBuffer);
typedef int (_stdcall *pReadTc)(int nport, int node, int address, int Count, int* RxdBuffer);
typedef int (_stdcall *pWriteInt)(int nport, int node, int address, int Count, char* Order, int* TxdBuffer);

typedef int (_stdcall *pSetF)(int nport, int node, int address, int Bit, char* Order);
typedef int (_stdcall *pReSetF)(int nport, int node, int address, int Bit, char* Order);
typedef int (_stdcall *pCancelF)(int nport, int node, int address, int Bit, char* Order);
typedef int (_stdcall *pCancelAllF)(int nport, int node);

typedef int (_stdcall *pSet)(int nport, int node, int address, int Bit, char* Order);
typedef int (_stdcall *pReSet)(int nport, int node, int address, int Bit, char* Order);
typedef int (_stdcall *pReadBit)(int nport, int node, int address, int Bit, int Count, char* Order, int* RxdBuffer);
typedef int (_stdcall *pReadDInt)(int nport, int node, int address, int Count,int* RxdBuffer);
typedef int (_stdcall *pReadFloat)(int nport, int node, int address, int Count, float* RxdBuffer);
typedef int (_stdcall *pWriteDInt)(int nport, int node, int address, int Count, int* TxdBuffer);
typedef int (_stdcall *pWriteFloat)(int nport, int node, int address, int Count, float* TxdBuffer);

typedef int (_stdcall *pBitBin)(int value, int Bitaddress);
typedef int (_stdcall *p32I_16h)(int value);
typedef int (_stdcall *p32I_16l)(int value);
typedef int (_stdcall *p16I_32I)(int valueH, int valueL);
typedef int (_stdcall *p32f_16h)(float value);
typedef int (_stdcall *p32f_16l)(float value);
typedef float (_stdcall *p16I_32f)(int valueH, int valueL);

2)、建立动态链接库的新函数名:(在cpp文件的头处声明)

pOpen mOpen;
pClose mClose;
pSetDelay mSetDelay;
pComTrue mComTrue;
pComWork mComWork;

pMS mMS;
pSC mSC;
pMM mMM;
pTS mTS;
pReadInt mReadInt;
pReadTc mReadTc;
pWriteInt mWriteInt;

pSetF mSetF;
pReSetF mReSetF;
pCancelF mCancelF;
pCancelAllF mCancelAllF;

pSet mSet;
pReSet mReSet;
pReadBit mReadBit;
pReadDInt mReadDInt;
pReadFloat mReadFloat;
pWriteDInt mWriteDInt;
pWriteFloat mWriteFloat;

pBitBin mBitBin;
p32I_16h m32I_16h;
p32I_16l m32I_16l;
p16I_32I m16I_32I;
p32f_16h m32f_16h;
p32f_16l m32f_16l;
p16I_32f m16I_32f;

3)、导入动态链接库,如例所示:(在cpp文件的OnInitDialog过程建立):

hinstDLL = LoadLibrary("Omron_HostLink.dll");

4)、判断dll文件是否存在并声明并建立动态链接库中的函数与新函数名的对应关系,

如下:(在cpp文件的OnInitDialog过程建立):

if (hinstDLL)
{
mOpen = (pOpen)GetProcAddress (hinstDLL,"OmronComOpen");
mClose = (pClose)GetProcAddress (hinstDLL,"OmronComClose");
mSetDelay = (pSetDelay)GetProcAddress (hinstDLL,"OmronSetDelay");
mComTrue = (pComTrue)GetProcAddress (hinstDLL,"OmronComTrue");
mComWork = (pComWork)GetProcAddress (hinstDLL,"OmronComWork");

mMS = (pMS)GetProcAddress (hinstDLL,"OmronMS");
mSC = (pSC)GetProcAddress (hinstDLL,"OmronSC");
mMM = (pMM)GetProcAddress (hinstDLL,"OmronMM");
mTS= (pTS)GetProcAddress (hinstDLL,"OmronTS");
mReadInt = (pReadInt)GetProcAddress (hinstDLL,"OmronReadInt");
mReadTc = (pReadTc)GetProcAddress (hinstDLL,"OmronReadTc");
mWriteInt = (pWriteInt)GetProcAddress (hinstDLL,"OmronWriteInt");

mSetF = (pSetF)GetProcAddress (hinstDLL,"OmronSetF");
mReSetF = (pReSetF)GetProcAddress (hinstDLL,"OmronReSetF");
mCancelF = (pCancelF)GetProcAddress (hinstDLL,"OmronCancelF");
mCancelAllF = (pCancelAllF)GetProcAddress (hinstDLL,"OmronCancelAllF");

mSet = (pSet)GetProcAddress (hinstDLL,"OmronSet");
mReSet = (pReSet)GetProcAddress (hinstDLL,"OmronReSet");
mReadBit = (pReadBit)GetProcAddress (hinstDLL,"OmronReadBit");
mReadDInt = (pReadDInt)GetProcAddress (hinstDLL,"OmronReadDInt");
mReadFloat = (pReadFloat)GetProcAddress (hinstDLL,"OmronReadFloat");
mWriteDInt = (pWriteDInt)GetProcAddress (hinstDLL,"OmronWriteDInt");
mWriteFloat = (pWriteFloat)GetProcAddress (hinstDLL,"OmronWriteFloat");

mBitBin = (pBitBin)GetProcAddress (hinstDLL,"DecBitBin");
m32I_16h = (p32I_16h)GetProcAddress (hinstDLL,"Int32ToInt_16h");
m32I_16l = (p32I_16l)GetProcAddress (hinstDLL,"Int32ToInt_16l");
m16I_32I= (p16I_32I)GetProcAddress (hinstDLL,"Int16ToInt32");
m32f_16h = (p32f_16h)GetProcAddress (hinstDLL,"Float32ToInt_16h");
m32f_16l = (p32f_16l)GetProcAddress (hinstDLL,"Float32ToInt_16l");
m16I_32f= (p16I_32f)GetProcAddress (hinstDLL,"Int16ToFloat32");

AfxMessageBox("Omron_HostLink.dll已成功载入!");
}
else
{
AfxMessageBox("没找到Omron_HostLink.dll!");
SendMessage(WM_CLOSE);
}

注:双引号中为动态链接库中的原有函数名。


函数中用到了char*型参数,这里介绍下char*与Cstring的相互转换的函数:

(1)char*->CString

char* sz;
CString str;
str.Format("%s",sz); //可以用此函数将读取的值转成字符串

(2) CString -> char*

CString str;
char* sz = str.GetBuffer(0);//可将字符串转成char*给函数赋值


5)、当不再需要使用DLL时记得关闭串口及释放动态链接库,(在OnDestroy事件中释放)

if(hinstDLL)
{
int k = mComTrue(mnport);
if (k==1)
{
mClose(mnport);
}
FreeLibrary(hinstDLL);
}

wjun7610

  • 精华:0帖
  • 求助:0帖
  • 帖子:14帖 | 199回
  • 年度积分:0
  • 历史总积分:436
  • 注册:2004年2月01日
发表于:2011-04-05 15:12:29
2楼
上面的DLL使用要点也适用于其他各种windows标准DLL的VC调用。

wjun7610

  • 精华:0帖
  • 求助:0帖
  • 帖子:14帖 | 199回
  • 年度积分:0
  • 历史总积分:436
  • 注册:2004年2月01日
发表于:2011-05-16 22:29:54
3楼

当需要进行16位整数、32位整数、32位浮点数值进行混合读写时,所需拓展函数包括:

1)、DecBitBin 字整数的位抽取;

2)、Int32ToInt_16h 双字到字转换高位字;

3)、Int32ToInt_16l 双字到字转换低位字;

4)、Int16ToInt32 字到双字的转换;

5)、Float32ToInt_16h 浮点到字转换高位字;

6)、Float32ToInt_16l 浮点到字转换低位字;

7)、Int16ToFloat32 字到浮点的转换;

8)、介绍如何实现数据的混合读取和写入;

这里以VB程序为例叙述一下如何进行混合数据的读写。

混合数据的读写,一般都是与PLC的DM区元件的进行操作,因其他

元件区可能不一定支持32位整数及32位单精度浮点数。

这里PLC中DM0、DM1、DM4、DM5是16位整数元件,DM2是32位整数元件,DM6是32位单精度浮点元件

1、混合数据的读

Dim nodeI, addrI, ReadInt As Long
Dim buffer(1024) As Long ‘定义一个数据接收缓冲区 整型为1024,浮点型为256
Dim strreal As String
nodeI = Val(Combnode.Text) ‘取得PLC节点号
addrI = 0 ‘其实地址从DM0开始,DM0~DM7,8个寄存器值
ReadInt = OmronReadInt(ComPort, nodeI, addrI, 8, "DM", buffer(0)) ‘读取字元件值
If ReadInt = 8 Then
ReadText(0).Text = Trim(Str(buffer(0)))
ReadText(1).Text = Trim(Str(buffer(1)))
ReadText(2).Text = Trim(Str(Int16ToInt32(buffer(3), buffer(2)))) ‘DM2为32位整数元件
ReadText(3).Text = Trim(Str(buffer(4)))
ReadText(4).Text = Trim(Str(buffer(5)))
strreal = Trim(Str(Int16ToFloat32(buffer(7), buffer(6)))) ‘DM6为32位单精度浮点数元件
ReadText(5).Text = Format(strreal, "0.000")
End If

2、混合数据的写

Dim SendInt As Integer
Dim SendFloat As Single
Dim nodeI, addrI, addrBit, ReadInt, SendDint As Long
Dim nameplc As String
Dim buffer(1024) As Long ‘定义一个数据发送缓冲区 整型为1024,浮点型为256

‘将混合的数据转换成16位整数,数组
SendInt = Val(SendText(0).Text) ‘16位整数
buffer(0) = SendInt
SendInt = Val(SendText(1).Text) ‘16位整数
buffer(1) = SendInt
SendDint = Val(SendText(2).Text) ‘32位整数
buffer(2) = Int32ToInt_16l(SendDint)
buffer(3) = Int32ToInt_16h(SendDint)
SendInt = Val(SendText(3).Text) ‘16位整数
buffer(4) = SendInt
SendInt = Val(SendText(4).Text) ‘16位整数
buffer(5) = SendInt
SendFloat = Val(SendText(5).Text) ‘32位浮点数
buffer(6) = Float32ToInt_16l(SendFloat)
buffer(7) = Float32ToInt_16h(SendFloat)

Call OmronWriteInt(ComPort, nodeI, addrI, 8, "DM", buffer(0)) ‘按16位整数写入PLC

 

sheant

  • 精华:0帖
  • 求助:0帖
  • 帖子:1帖 | 4回
  • 年度积分:0
  • 历史总积分:58
  • 注册:2007年1月18日
发表于:2011-10-05 08:28:12
4楼

这是你写的dll吗,哪里可以下载,是不是要收费?我一直用VC,能不能写个VC串口编程使用范例.

QQ945785181

  • 精华:0帖
  • 求助:4帖
  • 帖子:5帖 | 86回
  • 年度积分:0
  • 历史总积分:156
  • 注册:2010年3月14日
发表于:2013-07-02 09:24:31
5楼

这个。。。        


热门招聘
相关主题

官方公众号

智造工程师