Codesys使用记录

本文详细解释了Codesys的IODrv功能,包括其在PLC程序中的作用,如何通过XML配置文件进行I/O映射,以及如何在IoDrvStartBusCycle接口中更新缓冲区数据。通过实例展示了配置步骤和数据交互过程。
摘要由CSDN通过智能技术生成


前言

提示:Codesys的IODrv功能使用心得:

CODESYS是一种广泛使用的工业自动化开发环境,用于编写和管理PLC(可编程逻辑控制器)程序。它提供了一个集成的开发环境,包括编辑器、调试器、仿真器和监视工具,使工程师能够创建和调试复杂的自动化控制系统。


提示:以下是本篇文章正文内容,下面案例可供参考

一 Codesys的IODrv是什么?

codesys提供的一套I/O驱动框架,其中包含I/O驱动程序的源代码框架和一些描述文件(XML)。用户可以通过更新IODrv中的输入和输出缓冲区,从而将总线或者系统状态跟Codesys进行周期性的更新和交互。

二、使用步骤

1.XML配置文件

配置文件(示例)如下所示,IODrv通过ParameterId绑定输入输出节点,IODrv源码中只需要按照XML文件中配置的节点个数,进行数据的拷贝和交换即可。

<?xml version="1.0" encoding="UTF-8"?>
<DeviceDescription xmlns="http://www.3s-software.com/schemas/DeviceDescription-1.0.xsd" xmlns:ts="http://www.3s-software.com/schemas/TargetSettings-0.1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<Types namespace="local">
	<BitfieldType basetype="std:BYTE" name="TBitSingleByte">
      <Component identifier="Bit0" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit0">Bit0</VisibleName>
      </Component>
      <Component identifier="Bit1" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit1">Bit1</VisibleName>
      </Component>
      <Component identifier="Bit2" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit2">Bit2</VisibleName>
      </Component>
      <Component identifier="Bit3" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit3">Bit3</VisibleName>
      </Component>
      <Component identifier="Bit4" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit4">Bit4</VisibleName>
      </Component>
      <Component identifier="Bit5" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit5">Bit5</VisibleName>
      </Component>
      <Component identifier="Bit6" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit6">Bit6</VisibleName>
      </Component>
      <Component identifier="Bit7" type="std:BOOL">
        <Default />
        <VisibleName name="local:TBitSingleByte.Bit7">Bit7</VisibleName>
      </Component>
    </BitfieldType>
    <StructType name="MvbPort_32Byte">
     ........此处省略具体变量
    </StructType>
	</Types>
	<Device>
		<DeviceIdentification>
			<!-- The information of this section is used for management in the device repository. It is not downloaded -->
			<!-- Range 0-32767 managed by 3S ? Smart Software Solutions; Range 32768 ? end can be used by customer -->
			<Type>32768</Type>
			<!-- Id is a unsigned long in hex formnat. First part (unsigned short) of Id is vendor id. 0001 is reserved for template devices. Must be replaced with your vendor id! Second part is vendor specific. -->
			<Id>0002 0001</Id>
			<!-- Format has to be 1.2.3.4 -->
			<Version>1.0.0.0</Version>
		</DeviceIdentification>
		<DeviceInfo>
			<Name name="local:SubModelName">PC104_DLDQ_MVB</Name>
			<Description name="local:DeviceDescription">PC104 DLDQ MVB</Description>
			<Vendor name="local:VendorName">Demo Corporation</Vendor>
			<OrderNumber>0001</OrderNumber>
		</DeviceInfo>
		<!-- Child connector. The moduleType of the connector is evaluated by the IO driver. The interface must be of the same name as in the parent connector -->
		<Connector moduleType="32768" interface="Common.PCI" role="child" explicit="false" connectorId="1" hostpath="-1">
			<Slot count="1" allowEmpty="false"></Slot>
		</Connector>
		<!-- Parent connector. The moduleType is downloaded and can be evaluated by the IO driver, e.g. with CAL_IoMgrConfigGetFirstConnector .
		      The interface must be of the same name as in the child connector of the submodules-->
		<Connector moduleType="32769" interface="local.myInternalModuleConnector" role="parent" explicit="false" connectorId="2" hostpath="1">
			<!-- The interface name is used in for the tabs shown in CoDeSys (for example I/O mapping).  The given name is used as a prefix to the specific tab page. -->
			<InterfaceName name="local:myInternalModuleConnector">Internal</InterfaceName>
			<DriverInfo needsBusCycle="true"></DriverInfo>
			<HostParameterSet>
				<!-- IN  ParameterId -->
				<Parameter ParameterId="1000" type="local:MvbPort_32Byte">
				  <Attributes channel="input" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:in0">Mvb_0x100</Name>
				</Parameter>
				<Parameter ParameterId="1001" type="local:MvbPort_32Byte">
				  <Attributes channel="input" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:in1">Mvb_0x101</Name>
				 </Parameter> 
				<Parameter ParameterId="1002" type="local:MvbPort_32Byte">
				  <Attributes channel="input" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:in2">Mvb_0x102</Name>
				</Parameter>
				<Parameter ParameterId="1003" type="local:MvbPort_32Byte">
				  <Attributes channel="input" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:in3">Mvb_0x103</Name>
				 </Parameter> 
				<Parameter ParameterId="1004" type="local:MvbPort_32Byte">
				  <Attributes channel="input" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:in4">Mvb_0x104</Name>
				 </Parameter> 
				 <!-- OUT  ParameterId -->
				<Parameter ParameterId="2000" type="local:MvbPort_32Byte">
				  <Attributes channel="output" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:out0">Mvb_0x001</Name>
				</Parameter>
				<Parameter ParameterId="2001" type="local:MvbPort_32Byte">
				  <Attributes channel="output" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:out1">Mvb_0x002</Name>
				</Parameter>
				<Parameter ParameterId="2002" type="local:MvbPort_32Byte">
				  <Attributes channel="output" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:out2">Mvb_0x003</Name>
				</Parameter>
				<Parameter ParameterId="2003" type="local:MvbPort_32Byte">
				  <Attributes channel="output" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:out3">Mvb_0x004</Name>
				</Parameter>  	
				<Parameter ParameterId="2004" type="local:MvbPort_32Byte">
				  <Attributes channel="output" download="true" functional="false" offlineaccess="readwrite" onlineaccess="readwrite" />
				  <Default>0</Default>
				  <Name name="local:out4">Mvb_0x005</Name>
				</Parameter>  				
			</HostParameterSet>
		</Connector>
	</Device>
</DeviceDescription>

XML文件可以安装到Codesys上作为一个设备使用,和工程中的变量做一些映射关系,如下图所示:
Codeys安装XML文件

2.IODrv缓冲区数据更新

因为Codesys提供了完整的I/O映射代码框架,我们只需要更改IoDrvStartBusCycle接口中的代码即可,如下代码所示为目前项目中的数据更新接口,将MVB源端口和宿端口数据通过input和output缓冲区和Codesys上层I/O映射关联起来,适合不同客户的项目开发,也方便用户对上层通信相关逻辑应用的编写。

STATICITF RTS_RESULT CDECL IoDrvStartBusCycle(RTS_HANDLE hIoDrv, IoConfigConnector *pConnector)
{
	int i = 0;
	int j = 0;
	
	if (pConnector->wType != 32769)
		return ERR_OK;
	
	if (head._sourcePortNum == 0 && head._sinkPortNum == 0)
	{
		return ERR_OK;
	}
	/* Our dummy driver: Copy outputs to inputs. Remove this code in your real driver */
	//	for (i=0; i< MAX_CHANNELS; i++)
	//	{
	//		memcpy(&s_uInputs[i], &s_uOutputs[i], sizeof(InOutInfo));
	//	}
	
	for (i = 0; i < head._sourcePortNum; i++)
	{
		memcpy(&(head._sourcePortList[i]->_portInfo._portData[0]), &s_uOutputs[i], sizeof(InOutInfo));
	}
	
	for (j = 0; j < head._sinkPortNum; j++)
	{
		memcpy(&s_uInputs[j], &(head._sinkPortList[j]->_portInfo._portData[0]), sizeof(InOutInfo));
	}
	return IoDrvGetModuleDiagnosis(hIoDrv, pConnector);
}

总结

以上就是今天要说的关于Codesys的IODrv相关内容,本文仅仅简单介绍了项目中使用IODrv的过程,IODrv详细的可以查阅Codesys官方资料,本文仅作为一个日常记录。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值