• 大小: 995KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-05-14
  • 语言: C#
  • 标签: 西门子  PLC  S7  C#  snap7  

资源简介

C#基于.net直接访问西门子PLC的类库——S7.net,可以实现西门子200含Smart、300、1200、1500寄存器地址的直接读写操作

资源截图

代码片段和文件信息

using System;
using System.Globalization;

namespace S7.Net
{
    /// 
    /// Conversion methods to convert from Siemens numeric format to C# and back
    /// 

    public static class Conversion
    {
        /// 
        /// Converts a binary string to Int32 value
        /// 

        /// 
        /// 
        public static int BinStringToInt32(this string txt)
        {
            int ret = 0;

            for (int i = 0; i < txt.Length; i++)
            {
                ret = (ret << 1) | ((txt[i] == ‘1‘) ? 1 : 0);
            }
            return ret;
        }

        /// 
        /// Converts a binary string to a byte. Can return null.
        /// 

        /// 
        /// 
        public static byte? BinStringToByte(this string txt)
        {
            if (txt.Length == 8) return (byte)BinStringToInt32(txt);
            return null;
        }

        /// 
        /// Converts the value to a binary string
        /// 

        /// 
        /// 
        public static string ValToBinString(this object value)
        {
            int cnt = 0;
            int cnt2 = 0;
            int x = 0;
            string txt = ““;
            long longValue = 0;

            try
            {
                if (value.GetType().Name.IndexOf(“[]“) < 0)
                {
                    // ist nur ein Wert
                    switch (value.GetType().Name)
                    {
                        case “Byte“:
                            x = 7;
                            longValue = (long)((byte)value);
                            break;
                        case “Int16“:
                            x = 15;
                            longValue = (long)((Int16)value);
                            break;
                        case “Int32“:
                            x = 31;
                            longValue = (long)((Int32)value);
                            break;
                        case “Int64“:
                            x = 63;
                            longValue = (long)((Int64)value);
                            break;
                        default:
                            throw new Exception();
                    }

                    for (cnt = x; cnt >= 0; cnt += -1)
                    {
                        if (((Int64)longValue & (Int64)Math.Pow(2 cnt)) > 0)
                            txt += “1“;
                        else
                            txt += “0“;
                    }
                }
                else
                {
                    // ist ein Array
                    switch (value.GetType().Name)
                    {
                        case “Byte[]“:
                            x = 7;
                            byte[] ByteArr = (byte[])value;
             

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    .......       158  2018-10-11 02:28  s7netplus-develop\.gitignore

    .......       587  2018-10-11 02:28  s7netplus-develop\appveyor.yml

    .......    191272  2018-10-11 02:28  s7netplus-develop\Documentation\Documentation.docx

    .......    573506  2018-10-11 02:28  s7netplus-develop\Documentation\Documentation.pdf

    .......    121055  2018-10-11 02:28  s7netplus-develop\Documentation\protection 2.png

    .......     17466  2018-10-11 02:28  s7netplus-develop\Documentation\protection.png

    .......      8392  2018-10-11 02:28  s7netplus-develop\Documentation\struct.png

    .......       356  2018-10-11 02:28  s7netplus-develop\GitVersion.yml

    .......      1082  2018-10-11 02:28  s7netplus-develop\License.txt

     文件         70  2019-02-28 13:29  s7netplus-develop\Readme!!!.txt

    .......      1706  2018-10-11 02:28  s7netplus-develop\README.md

    .......       435  2018-10-11 02:28  s7netplus-develop\S7.Net\Compat\TcpClientMixins.cs

    .......      9106  2018-10-11 02:28  s7netplus-develop\S7.Net\Conversion.cs

    .......      5342  2018-10-11 02:28  s7netplus-develop\S7.Net\COTP.cs

    .......      3775  2018-10-11 02:28  s7netplus-develop\S7.Net\Enums.cs

    .......      5250  2018-10-11 02:28  s7netplus-develop\S7.Net\PLC.cs

    .......      7326  2018-10-11 02:28  s7netplus-develop\S7.Net\PLCAddress.cs

    .......     28297  2018-10-11 02:28  s7netplus-develop\S7.Net\PlcAsynchronous.cs

    .......      1090  2018-10-11 02:28  s7netplus-develop\S7.Net\PlcException.cs

    .......      2270  2018-10-11 02:28  s7netplus-develop\S7.Net\PLCExceptions.cs

    .......      8325  2018-10-11 02:28  s7netplus-develop\S7.Net\PLCHelpers.cs

    .......     26420  2018-10-11 02:28  s7netplus-develop\S7.Net\PlcSynchronous.cs

    .......       427  2018-10-11 02:28  s7netplus-develop\S7.Net\Properties\AssemblyInfo.cs

    .......       596  2018-10-11 02:28  s7netplus-develop\S7.Net\Properties\S7.Net.snk

    .......      2343  2018-10-11 02:28  s7netplus-develop\S7.Net\Protocol\ConnectionRequest.cs

    .......      5717  2018-10-11 02:28  s7netplus-develop\S7.Net\Protocol\S7WriteMultiple.cs

    .......      3272  2018-10-11 02:28  s7netplus-develop\S7.Net\Protocol\Serialization.cs

    .......      1342  2018-10-11 02:28  s7netplus-develop\S7.Net\S7.Net.csproj

    .......      2482  2018-10-11 02:28  s7netplus-develop\S7.Net\TPKT.cs

    .......       670  2018-10-11 02:28  s7netplus-develop\S7.Net\Types\Bit.cs

............此处省略56个文件信息

评论

共有 条评论