C#与欧姆龙NX系列PLC通讯

news/2024/11/28 23:32:12/

  由于项目需要C#跟欧姆龙NX系列PLC通讯,网上百度NX系列的PLC这方面的案例很少,且NXPLC不支持Fins通信,也不支持OPCUA.只能通过Ethernet/IP与其进行通信

采用CX-Compolet控件
 

先上图片:

 

实现核心代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OMRON.Compolet.CIP;
using System.Collections;

namespace Plc
{
    /// <summary>
    /// 欧姆龙CIPCompolet协议
    /// </summary>
    public class OmronCIP
    {
        /// <summary>
        /// 软元件代码
        /// </summary>
        private enum CIPElementCode
        {
            UCMM = 0,
            Class3=1
        };

         /// <summary>
        /// 接收的等待时间
        /// </summary>
        private int m_nWaitingTime;

        /// <summary>
        /// 通信网口
        /// </summary>
        private NXCompolet m_TcpLink;
        /// <summary>
        /// 心跳时间
        /// </summary>
        private int HeartBeatTimer;
        /// <summary>
        /// 本地端口
        /// </summary>
        private int LocalPort;
        /// <summary>
        /// PLC地址
        /// </summary>
        private string PeerAddress;
        /// <summary>
        /// 接收数据超时
        /// </summary>
        private long ReceiveTimeLimit;
        /// <summary>
        /// 第二连接PLC地址
        /// </summary>
        private string RoutePath;
        /// <summary>
        /// 连接类型
        /// </summary>
        private int ConnectionType;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tl">网口</param>
        public OmronCIP(string peerAddress= "192.168.250.1",long receiveTimeLimit=750,int localPort=2, int connectionType =0
            ,int heartBeatTimer=0,string routePath= "2%192.168.250.1")
        {
            //变量初使化赋值
            PeerAddress = peerAddress;
            ReceiveTimeLimit = receiveTimeLimit;
            LocalPort = localPort;
            LocalPort = localPort;
            ConnectionType = connectionType;
            HeartBeatTimer = heartBeatTimer;
            RoutePath = routePath;
            //建立PLC连接变量
            m_TcpLink = new NXCompolet();
            m_TcpLink.ConnectionType = (OMRON.Compolet.CIP.ConnectionType)ConnectionType;
            m_TcpLink.DontFragment = false;
            m_TcpLink.HeartBeatTimer = HeartBeatTimer;
            m_TcpLink.LocalPort = LocalPort;
            m_TcpLink.PeerAddress = PeerAddress;
            m_TcpLink.ReceiveTimeLimit = ReceiveTimeLimit;
            m_TcpLink.RoutePath = RoutePath;
            m_TcpLink.UseRoutePath = false;                       
        }

        public NXCompolet GetTcp
        {
            get
            {
                return m_TcpLink;
            }

        }
        /// <summary>
        /// 设置接收的等待时间
        /// </summary>
        /// <param name="nMs">超时时间,单位:S</param>
        public void SetWaitingTime(int nMs)
        {
            int nCount = nMs;

            if (nCount <= 0)
            {
                m_nWaitingTime = 1;
                ReceiveTimeLimit = 1;
            }        
            else
            {
                m_nWaitingTime = nCount;
                ReceiveTimeLimit = nCount;
            }         
        }
        /// <summary>
        /// 写入位
        /// </summary>
        /// <param name="BinaryValue"></param>
        /// <param name="bVals"></param>
        /// <returns></returns>
        public bool WriteBit(string BinaryValue, bool bVals)
        {
            byte[] val;
            try
            {
                if(bVals)
                {
                    val = StringToByteArray("0100");
                }
                else
                {
                    val = StringToByteArray("0000");
                }
                m_TcpLink.WriteRawData(BinaryValue, val);
                return true;
            }
            catch(Exception ex)
            {
                              
            }
            return false;
        }

        /// <summary>
        /// 写入多个位
        /// </summary>
        /// <param name="valueList"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool WriteMultiBit(string[] valueList, bool[] data)
        {
            try
            {
                string[] varlist = valueList;

                if (varlist == null || varlist.Length == 0)
                {
                    return false;
                }
                
                for(int i= 0; i < valueList.Length; i++)
                {
                    byte[] val;
                    try
                    {
                        if (data[i])
                        {
                            val = StringToByteArray("0100");
                        }
                        else
                        {
                            val = StringToByteArray("0000");
                        }
                        m_TcpLink.WriteRawData(valueList[i], val);                       
                    }
                    catch
                    {
                        throw new NotSupportedException();
                    }
                }               
                return true;
            }
            catch (Exception ex)
            {
                            
            }
            finally
            {

            }
            return false;
        }

        public bool WriteWord(string BinaryValue, string bVals)
        {
            try
            {
                string valWrite = BinaryValue;
                if (valWrite.StartsWith("_"))
                {
                    MessageBox.Show("The SystemVariable can not write!");
                    return false;
                }
                object val = RemoveBrackets(bVals);
                if (m_TcpLink.GetVariableInfo(BinaryValue).Type == VariableType.STRUCT)
                {
                    val = ObjectToByteArray(val);
                }
                m_TcpLink.WriteVariable(BinaryValue, val);
                return true;
            }
            catch(Exception ex)
            {
                

            }

            return false;
        }

        public bool WriteString(string BinaryValue, string bVals)
        {
            try
            {
                string valWrite = BinaryValue;
                if (valWrite.StartsWith("_"))
                {
                    MessageBox.Show("The SystemVariable can not write!");
                    return false;
                }
                object val = this.RemoveBrackets(bVals);
                if (m_TcpLink.GetVariableInfo(BinaryValue).Type == VariableType.STRING)
                {
                    //val = this.ObjectToByteArray(val);
                }
                m_TcpLink.WriteVariable(BinaryValue, val);
                return true;
            }
            catch (Exception ex)
            {


            }
            return false;
        }
        /// <summary>
        /// 读取位
        /// </summary>
        /// <param name="BinaryValue"></param>
        /// <returns></returns>
        public bool ReadBit(string BinaryValue)
        {
          
            try
            {
                if (BinaryValue == null)
                {
                    return false;
                }
                object obj = m_TcpLink.ReadRawData(BinaryValue);
                VariableInfo info = m_TcpLink.GetVariableInfo(BinaryValue);
                string val = this.ByteArrayToString(obj as byte[]);
                if( val == "01-00")
                {
                    return true;
                }
                else if(val == "00-00")
                {
                    return false;
                }
                else
                {
                    return false;
                }
            }   
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC读取异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
               
            }
            return false;
        }

         /// <summary>
         /// 读取多个位
         /// </summary>
         /// <param name="BinaryMultiValue"></param>
         /// <param name="result"></param>
        public void ReadMultiBit(string BinaryMultiValue,ref string result)
        {
            try
            {
                if (BinaryMultiValue == null)
                {
                    return;
                }
                string[] varlist = BinaryMultiValue.Replace(" ", String.Empty).Split(',');
                Hashtable retVals = m_TcpLink.ReadRawDataMultiple(varlist);
                string multival = string.Empty;
                for (int index = 0; index < varlist.Length; index++)
                {
                    string varName = varlist[index];
                    string val = this.ByteArrayToString(retVals[varName] as byte[]);

                    
                    multival += val + ",";
                }
                multival = multival.TrimEnd(',');
                result = multival;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC读取异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {

            }
            return;
        }
        /// <summary>
        /// 读取字
        /// </summary>
        /// <param name="BinaryValue"></param>
        /// <returns></returns>
        public bool ReadString(string BinaryValue,ref string result)
        {

            try
            {
                if (BinaryValue == null)
                {
                    return false;
                }
                object obj = m_TcpLink.ReadVariable(BinaryValue);
                if (obj == null)
                {
                    throw new NotSupportedException();
                }
                VariableInfo info = m_TcpLink.GetVariableInfo(BinaryValue);
                string str = GetValueOfVariables(obj);
                result = str;
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC读取异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {

            }
            return false;
        }
        /// <summary>
        /// 读取字
        /// </summary>
        /// <param name="BinaryValue"></param>
        /// <returns></returns>
        public short ReadWord(string BinaryValue)
        {

            try
            {
                if (BinaryValue == null)
                {
                    return 0;
                }
                object obj = m_TcpLink.ReadVariable(BinaryValue);
                if (obj == null)
                {
                    throw new NotSupportedException();
                }
                VariableInfo info = m_TcpLink.GetVariableInfo(BinaryValue);
                string str = GetValueOfVariables(obj);
                short res = Convert.ToInt16(str);
                return res;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC读取异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {

            }
            return 0;
        }
        /// <summary>
        /// 读取多个字
        /// </summary>
        /// <param name="valueList"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool ReadMultiWord(string[] valueList,ref short[] data)
        {
            try
            {
                string[] varlist = valueList;
                if (varlist == null || varlist.Length == 0)
                {
                    return false;
                }
                short[] res = new short[varlist.Length];
                Hashtable retVals = m_TcpLink.ReadVariableMultiple(varlist);
                string multival = string.Empty;
                if (retVals == null)
                {
                    throw new NotSupportedException();
                }
                for (int index = 0; index < varlist.Length; index++)
                {
                    string varName = varlist[index];
                    object val = retVals[varName];
                    string valStr = this.GetValueOfVariables(val);            
                    multival += valStr + ",";
                    res[index] = Convert.ToInt16(valStr);

                }
                multival = multival.TrimEnd(',');
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC读取异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
               
            }
            return false;
        }

 

        /// <summary>
        /// 打开设备
        /// </summary>
        /// <returns></returns>
        public  bool Open()
        {
            try
            {
                m_TcpLink.Active = true;
                if (m_TcpLink.IsConnected)
                {
                    return true;
                }
                else
                {
                    m_TcpLink.Active = false;                    
                    MessageBox.Show("Connection failed !" + System.Environment.NewLine + "Please check PeerAddress.", "PLC异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                m_TcpLink.Active = false;
            }
            return false;
        }
        public bool IsOpen()
        {
            if(m_TcpLink != null)
            {
                return m_TcpLink.IsConnected;
            }
            return false;
        }
        /// <summary>
        /// 关闭设备
        /// </summary>
        public  void Close()
        {
            try
            {
                if (m_TcpLink.IsConnected)
                {
                    m_TcpLink.Active = false;   
                                   
                }
                else
                {
                    m_TcpLink.Active = false;                   
                    MessageBox.Show("Connection failed !" + System.Environment.NewLine + "Please check PeerAddress.", "PLC异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "PLC异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                m_TcpLink.Active = false;
            }
        }

        #region 常用功能函数
        private void DisplayVariableInfomation(string str, Control con)
        {
            ListView listViewOfVariableNames = con as ListView;
            ListViewItem item = listViewOfVariableNames.Items.Add(str);
            VariableInfo info = m_TcpLink.GetVariableInfo(str);
            if (info.IsArray)
            {
                string text = info.Type.ToString();
                foreach (long num in info.NumberOfElements)
                {
                    text += "[" + num.ToString() + "]";
                }
                item.SubItems.Add(text);
            }
            else
            {
                item.SubItems.Add(info.Type.ToString());
            }
            item.SubItems.Add(string.Empty);
        }

        private bool IsMustElementAccess(VariableInfo info)
        {
            bool toReturn = false;
            if (info.IsArray)
            {
                if (info.Type == VariableType.STRING || info.Type == VariableType.UNION)
                {
                    toReturn = true;
                }
            }

            return toReturn;
        }

        private bool IsMustMemberAccess(VariableInfo info)
        {
            bool toReturn = false;

            if (info.Type == VariableType.UNION)
            {
                toReturn = true;
            }

            return toReturn;
        }

        private String GetAccessablePath(String path)
        {
            String newPath = String.Empty;
            newPath += path;
            VariableInfo info = m_TcpLink.GetVariableInfo(path);
            if (this.IsMustElementAccess(info))
            {
                // get only first element
                for (int i = 0; i < info.Dimension; i++)
                {
                    newPath += "[" + info.StartArrayElements[i].ToString() + "]";
                }
                return this.GetAccessablePath(newPath);
            }
            else if (this.IsMustMemberAccess(info))
            {
                // get only first member
                newPath += "." + info.StructMembers[0].Name;
                return this.GetAccessablePath(newPath);
            }
            else
            {
                return newPath;
            }
        }

        private string GetValueOfVariables(object val)
        {
            string valStr = string.Empty;
            if (val.GetType().IsArray)
            {
                Array valArray = val as Array;
                if (valArray.Rank == 1)
                {
                    valStr += "[";
                    foreach (object a in valArray)
                    {
                        valStr += this.GetValueString(a) + ",";
                    }
                    valStr = valStr.TrimEnd(',');
                    valStr += "]";
                }
                else if (valArray.Rank == 2)
                {
                    for (int i = 0; i <= valArray.GetUpperBound(0); i++)
                    {
                        valStr += "[";
                        for (int j = 0; j <= valArray.GetUpperBound(1); j++)
                        {
                            valStr += this.GetValueString(valArray.GetValue(i, j)) + ",";
                        }
                        valStr = valStr.TrimEnd(',');
                        valStr += "]";
                    }
                }
                else if (valArray.Rank == 3)
                {
                    for (int i = 0; i <= valArray.GetUpperBound(0); i++)
                    {
                        for (int j = 0; j <= valArray.GetUpperBound(1); j++)
                        {
                            valStr += "[";
                            for (int z = 0; z <= valArray.GetUpperBound(2); z++)
                            {
                                valStr += this.GetValueString(valArray.GetValue(i, j, z)) + ",";
                            }
                            valStr = valStr.TrimEnd(',');
                            valStr += "]";
                        }
                    }
                }
            }
            else
            {
                valStr = this.GetValueString(val);
            }
            return valStr;
        }

        private string GetValueString(object val)
        {
            if (val is float || val is double)
            {
                return string.Format("{0:R}", val);
            }
            else
            {
                return val.ToString();
            }
        }

        private object RemoveBrackets(string val)
        {
            object obj = string.Empty;
            if (val.IndexOf("[") >= 0)
            {
                string str = val.Trim('[', ']');
                str = str.Replace("][", ",");
                obj = str.Split(',');
            }
            else
            {
                obj = val;
            }
            return obj;
        }

        private string ByteArrayToString(byte[] ba)
        {
            if (ba.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                return BitConverter.ToString(ba);
            }
        }

        private byte[] StringToByteArray(string hex)
        {
            if (hex == String.Empty)
            {
                return new Byte[0];
            }
            int byteNumber = hex.Length / 2;
            byte[] bytes = new byte[byteNumber];
            for (int i = 0; i < hex.Length; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }

        private byte[] ObjectToByteArray(object obj)
        {
            if (obj is Array)
            {
                Array arr = obj as Array;
                Byte[] bin = new Byte[arr.Length];
                for (int i = 0; i < bin.Length; i++)
                {
                    bin[i] = Convert.ToByte(arr.GetValue(i));
                }
                return bin;
            }
            else
            {
                return new Byte[1] { Convert.ToByte(obj) };
            }
        }
        #endregion

    }
}

 


http://www.ppmy.cn/news/459497.html

相关文章

MIDAS GTS NX在高边坡工程中的应用

2.3 原状边坡工况分析 2.3.1 一般工况 1&#xff09;设置一般工况 一般工况下&#xff0c;通常进行边坡的非线性应力分析即可。在GTS NX中&#xff0c;应力分析几乎是所有其他分析的基础&#xff0c;应力分析的模型也可以作为其它分析的基础模型。对于一般工况的应力分析的模型…

Jetson Xavier NX 内核编译、源码编译

Jetson Xavier NX 简单介绍&#xff1a; NX通过外形小巧的模组系统将超级计算机的性能带到了边缘端。 高达21 TOPS的加速计算能力可并行运行现代神经网络并处理来自多个高分辨率传感器的数据&#xff0c;这是完整AI系统的要求。NVIDIA Jetson Xavier NX 16GB EMMC ,8GB DDR 1、…

Jetson Nano\NX\AGX 学习笔记

Jetson Nano\NX\AGX 学习笔记 1. jetson平台安装pytorch、torchvision2. yolov5模型部署deepstream,x86\jetson平台2.0. 下载 deepstream6.12.1. 下载yolov5s模型或者训练自己的yolov5s模型2.2. 下载yolov5、DeepStream-Yolo代码,并把yolov5s.pt转换为yolov5s.engine文件(~~ten…

【模型部署】Jetson Xavier NX(eMMC)部署YOLOv5-5.0

文章目录 前言NVIDIA JetsonJetson Xavier NX版本区别&#xff08;SD | eMMC&#xff09;规格参数 Jetpack4.6.1环境搭建烧录系统&#xff08;OS&#xff09;SSD启动SSD分区设置为启动项 深度学习环境搭建设置语言/地区等信息烧录SDK组件换清华源&#xff08;可选&#xff09; …

Jetson Xavier NX部署PaddlePaddle框架

文章目录 前言一、刷机、开机等基本环境配置可以参考如下文章我的环境信息 二、源码编译安装paddlepaddle1.编译前准备工作2.首先编译安装NCCL3.编译安装PaddlePaddle4.测试方法 三、Jetson Xavier NX安装paddlehub1.paddlehub无法直接pip安装&#xff0c;报错如下2.源码编译安…

Jetson Xavier NX 内核编译、源码编译流程

[NVIDIA] 从零入手 Jetson Xavier NX 内核编译、源码编译流程 – ***注明&#xff1a;感谢网友提醒&#xff0c;我当时买的时候nx套件还没开售&#xff0c;因为核心板兼容b01&#xff0c;先买了核心板和b01的板子合在一起用的&#xff0c;所以本文是基于jetson xavier nx模块 …

J-004 Jetson电路设计之HDMI设计--NANO XAVIER NX

HDMI电路设计 1 简介2 框图介绍3 原理图介绍 1 简介 NANO & XAVIER NX提供一路HDMI接口&#xff0c;DP接口与HDMI是兼容的&#xff0c;可用于扩展一路HDMI。其中引脚说明: PIN名称描述方向类型63HDMI_DP_TXDN0DisplayPort 1 Lane 0 or HDMI Lane 2输出HDMI/DP65HDMI_DP_T…

J-002 Jetson电路设计之电源设计--NANO XAVIER NX

Jetson电源设计 1 电源说明1.1 电源和系统引脚描述1.2 电源控制框图详情 2 上电的时许2.1 框图分析2.2 上电时序 3 GND引脚 1 电源说明 Jetson NANO和XAVIER NX核心板的电源为DC-5V。 1.1 电源和系统引脚描述 PIN名称描述方向类型251-260VDD_IN主电源输入5.0V235PMIC_BBATRT…