[netMarketing类库] OmronPlcNet类: OmronPlc基于tcp的Fins通讯类,可用NJ系列,CP1H系列的欧姆龙PLC

命名空间:netMarketing.automation.plc.OmronPlcNet

功能:OmronPlc基于tcp的Fins读写类。可用NJ系列,CP1H系列的欧姆龙PLC


功能详细说明:

OmronPlcNet类是基于tcp的Fins通讯类。可用NJ系列,CP1H系列的欧姆龙PLC。


常用函数列表

/// 构造函数
public OmronPlcNet(Byte clientNode, Byte serverNode)
/// 初始化plc端口连接
public bool InitPort(string IpAdrr, int Port)
/// 关闭连接
public void Close()
/// plc是否连接上了
public bool IsConnected
/// plc握手协议
public bool Handshake()
/// 重新连接plc
public bool ReConneting()
/// 读数据,double型
public Double[] ReadDouble(UInt16 startAdrr, UInt16 readLength, Byte AreaType)
/// 写数据,double型
public bool WriteDouble(UInt16 startAdrr, Byte AreaType, params Double[] doubleData)
/// 读字符串
public string ReadString(UInt16 startAdrr, UInt16 readLength)
/// 读数据,Single型
public Single[] ReadSingles(UInt16 startAdrr, UInt16 readLength, Byte AreaType)
/// 写数据,single型
public bool WriteSingles(UInt16 startAdrr, Byte AreaType, params Single[] singleData)
/// 读数据,int16型
public Int16[] ReadInt16(UInt16 startAdrr, UInt16 readLength, Byte AreaType)
/// 写字符串
public bool WriteString(UInt16 startAdrr, params byte[] byteData)
/// 写数据,int16型
 public bool WriteInt16(UInt16 startAdrr, Byte AreaType, params Int16[] Int16Data)
/// 读数据, int32型
public Int32[] ReadInt32(UInt16 startAdrr, UInt16 readLength, Byte AreaType)
/// 写数据, int32型
public bool WriteInt32(UInt16 startAdrr, Byte AreaType, params Int32[] Int32Data)
/// 读位, 返回bool型
public Boolean[] ReadBits(UInt16 startAdrr, Byte bitNo, UInt16 readLength, Byte AreaType)
/// 写位
public bool WriteBits(UInt16 startAdrr, Byte bitNo, Byte AreaType, params Boolean[] BoolenData)


示例程序:

下面的例子是对上面OmronPlcNet类的一个二次封装应用的例子。

/// <summary>
         /// PLC对象
         /// </summary>
         public class OmronPlcObj
         {
             //节点号可以任意,创建一个实例用于读取,另一个用于写操作
             private static OmronPlcNet omrRead = new OmronPlcNet(0xD6, 0x01);
             private static OmronPlcNet omrWrite = new OmronPlcNet(0xD5, 0x01);
             private configSetting sysConfig = null;
             private WorkSetting workConfig = null;

             public OmronPlcObj(configSetting data, WorkSetting data2)
             {
                 this.sysConfig = data;
                 this.workConfig = data2;
             }

             public void Close()
             {
                 omrRead.Close();
                 omrWrite.Close();
             }

             public OmronPlcNet returnPlcReadObj()
             {
                 return omrRead;
             }

             public OmronPlcNet returnPlcWriteObj()
             {
                 return omrWrite;
             }

             /// <summary>
             /// plcRead初始化
             /// <para>异常:ArgumentException</para>
             /// </summary>
             /// <returns></returns>
             public bool initRead()
             {
                 bool f1 = true;
                 if (sysConfig.vitualPlc) { return true; }
                 if (!omrRead.InitPort(sysConfig.plcIP, sysConfig.plcPort))
                 {
                     f1 = false;
                 }
                 if (!f1) throw new ArgumentException("连接PLC_Read 失败!");
                 return f1;
             }

             /// <summary>
             /// plcWrite初始化
             /// <para>异常:ArgumentException</para>
             /// </summary>
             /// <returns></returns>
             public bool initWrite()
             {
                 //如果plcWrite的IP与plcRead的IP设置得一样,则plcWrite对象指向plcRead对象. 
                 //即两者变成一个对象, plcRead即负责读与负责写
                 //因为从实际连接效果来看, 如果plcRead, plcWrite连接同一个IP与端口, 经常会连接失败
                 bool f1 = true;
                 if (sysConfig.plcIP.Equals(sysConfig.plcIPWRite))
                     omrWrite = omrRead;
                 else
                 {
                     if (sysConfig.vitualPlc) { return true; }
                     if (!omrWrite.InitPort(sysConfig.plcIPWRite, sysConfig.plcPortWRite))
                     {
                         f1 = false;
                     }
                     if (!f1) throw new ArgumentException("连接PLC_Write 失败!");
                 }
                 return f1;
             }


             /// <summary>
             /// 读plc int
             /// </summary>
             /// <param name="startAddr"></param>
             /// <param name="len"></param>
             /// <returns></returns>
             public int[] readInt(ushort startAddr,ushort len=1)
             {
                 if (sysConfig.vitualPlc) { return new int[] { 0 }; }
                 return omrRead.ReadInt16(startAddr, len, OmronPlcNet.DM_W).ToList().ConvertAll(s => (int)s).ToArray();
             }

             public bool writeInt(ushort startAddr,short[] data, ushort len=1)
             {
                 if (sysConfig.vitualPlc) { return true; }
                 return omrWrite.WriteInt16(startAddr, OmronPlcNet.DM_W, data);
             }

             /// <summary>
             /// 读PLC字符串
             /// <para>异常:Exception</para>
             /// </summary>
             /// <param name="startAddr"></param>
             /// <param name="len"></param>
             /// <returns></returns>
             public string readString(ushort startAddr, ushort len)
             {
                 try
                 {
                     if (sysConfig.vitualPlc || sysConfig.vitualScaner) { return randomHelper.GenerateRndNumLetter(workConfig.productSNlength); }
                     if (startAddr == 0) return string.Empty;

                     var ary1 = omrRead.ReadInt16(startAddr, (ushort)(workConfig.productSNlength / 2), OmronPlcNet.DM_W);
                     var sb1 = new StringBuilder();
                     foreach (var m in ary1)
                     {
                         var v = ConvertHelper.ConvertBase(m, 10, 16);
                         sb1.Append((char)(ConvertHelper.ToInt32((ConvertHelper.ConvertBase(v.LeftStr(2), 16, 10)), 0)));
                         sb1.Append((char)(ConvertHelper.ToInt32((ConvertHelper.ConvertBase(v.RightStr(2), 16, 10)), 0)));
                     }
                     return sb1.ToString();
                 }
                 catch (Exception ex)
                 {
                     throw ex;
                 }
             }

             /// <summary>
             /// 写PLC字符串
             /// <para>异常:Exception</para>
             /// </summary>
             /// <param name="startAddr"></param>
             /// <param name="str"></param>
             /// <returns></returns>
             public bool writeString(ushort startAddr, string str)
             {
                 try
                 {
                     if (sysConfig.vitualPlc) { return true; }
                     if (startAddr == 0) return false;

                     var ary1 = Encoding.UTF8.GetBytes(str).ToList();
                     if (ary1.Count % 2 != 0)
                     {
                         ary1.Add(0);
                     }
                     var list1 = new List<short>();
                     for (int i = 0; i < ary1.Count; i += 2)
                     {
                         list1.Add(
                             short.Parse(
                             ConvertHelper.ConvertBase(
                             ConvertHelper.ConvertBase(ary1[i], 10, 16) +
                             ConvertHelper.ConvertBase(ary1[i + 1], 10, 16), 16, 10))
                             );
                     }
                     return omrWrite.WriteInt16(startAddr, OmronPlcNet.DM_W, list1.ToArray());
                 }
                 catch (Exception ex)
                 {
                     throw ex;
                 }
             }
         
         }


--------------------- 

作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!

返回类库功能说明目录


#转载请注明出处 www.skcircle.com 《少有人走的路》勇哥的工业自动化技术网站。如果需要本贴图片源码等资源,请向勇哥索取。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«   2021年1月   »
123
45678910
11121314151617
18192021222324
25262728293031
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:

Powered By Z-BlogPHP 1.6.0 Valyria

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864