Codesys使用记录-CODESYS移植-酷德网 - CoDeSys软件教程下载,CoDeSys工控资讯门户 

酷德网

HSC6490-cn中国制造,中国芯~
查看: 330|回复: 1

Codesys使用记录

[复制链接]
  • TA的每日心情
    慵懒
    2017-11-9 20:49
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2026-3-18 09:49:30 | 显示全部楼层 |阅读模式
    文章目录





      前言一 Codesys的IODrv是什么?




      二、使用步骤




        1.XML配置文件2.IODrv缓冲区数据更新
      总结


    前言

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

    提示:以下是本篇文章正文内容,下面案例可供参考
    一 Codesys的IODrv是什么?

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

    1.XML配置文件

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


    [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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <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" />
    				  <Default>0</Default>
    				  <Name name="local:out4">Mvb_0x005</Name>
    				</Parameter>  				
    			</HostParameterSet>
    		</Connector>
    	</Device>
    </DeviceDescription>
    
    


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

    Codesys使用记录-1.png


    2.IODrv缓冲区数据更新

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

    [C] 纯文本查看 复制代码
    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官方资料,本文仅作为一个日常记录。


    +10
    不赞一个?
    打赏鼓励一下!
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2026-9-9 14:38
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2026-8-25 15:29:59 | 显示全部楼层
    .xml文件是不是 也是一种程序  如果你想添加一个io的功能  就比如说输出吧  你需要按照他的那个格式去写 ,codesys ide 才能解析   
    +10
    不赞一个?
    打赏鼓励一下!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

    本版积分规则

    
    顶部qrcode底部
    关注酷德网订阅号,获取更多资讯!

    QQ|Archiver|手机版|小黑屋|酷德论坛 ( 苏ICP备16065247号-1 )|网站地图

    GMT+8, 2026-10-9 10:30 , Processed in 0.141470 second(s), 53 queries .

    酷德网

    © 2001-2013 Hicodesys. 技术支持 by 酷德网

    快速回复 返回顶部 返回列表