使用NetToPLCSimとPLCは、仮想マシンに接続されています。

1、仮想計算機システムWin10 ... ...インストール内部VS.

2、ブログ内に設置ネイティブWin10 ... ...システム15。

3、変換ソフト:NetToPLCSIM。

4、マシンと同じルータ(注)に接続された仮想マシン:

画像

図5に示すように、ブリッジモードへの仮想マシンのセット、および選択されたNICがルータに接続されています。

6、カードのIPアドレスを自動的に取得するように設定、および無効化、有効化、IPアドレスを取得するためにカードをしましょう。

ネイティブIP:画像

仮想マシンのIP:画像

通常は、2台のマシンが成功し、相互にpingを実行することができます!ネットワークが構成され、基本的に準備ができています。それ以外の場合は、ファイアウォールを確認してください。私は満たしていない、何のGETはありません。

そして、ボー・マップを開いて、/取得ダニを入れなければなりません!

  1.設定PLCのIP 画像

2、GET / PUTティック

                  画像

シミュレーション、シミュレーション上の最初のクリックが、PLCをダウンロードしていない3、。


サーバの起動サーバのデフォルトを停止することができます4、オープンNettoplcsim :(ソフトウェア)、注意を払う、ソフトウェアをオープンしてください。

画像

5、追加ドッキング画面を開きます。PLCSIM IP]をクリックし、あなたが追加した場合最初のシミュレーションが表示されないことを注意します。

画像ノーPLC

6、PLC PLCSIM IPをクリックしてダウンロードし、PLCシミュレータを開きます。これは非常に重要です、それは自分の選択PLCから出て来なければなりません。..

画像

7、NET IP ADDRを選択します。

画像

8、OK注意ラックとスロット選択

画像

画像

図9に示すように、VSは、仮想マシン内の通信をテスト

 内部の 静的 無効 TestS7()
        {


            PLC PLCは= 新しい PLC(CpuType.S71200、 " 192.168.3.62 "、0、1)。
            試します
            {
                log("begin open plc");
                plc.Open();//一个PLC异步函数,注意,异步函数的异常只能通过
                log("plc is open!");
            }
            catch(Exception e)
            {
                log(e.Message);
            }
        }

结果:

画像

10,尝试读取写入byte[]数组:

  RecipePLC 数据类型具有:(配方)

    一个字符串: string[20] 占用了22个字节.Index0是其大小,应该是22,index1 是实际字符长度.应该是0.现在

   一个step数组:step是一个plc数据类型. 其为30个step元素.

每个step含有:

画像

OK,我们先来完成如何读取写入这个结构:

10.1首先在vs中架构一个PLCSTRING类,因为 string这个类不好投射.


 public class PlcString:IPlcParse
    {
        private readonly byte[] bytes;
        public PlcString() : this(256) { }
        public PlcString(int len )
        {
            bytes = new byte[len];
            bytes[0] = len <= 0xfe ? (byte)len : (byte)0xfe;



        }
        public void SetString(string str)
        {
            int len = bytes.Length;
            if (str != string.Empty)
            {
                byte[] str_bytes = ASCIIEncoding.ASCII.GetBytes(str);
                bytes[1] = (byte)str_bytes.Length;
                str_bytes.CopyTo(bytes, 2);
            }
            else
            {
                bytes[1] = 0;

            }
        }
        public string GetString()
        {
            if (bytes[1] != 0)
            {
                byte[] strs = new byte[(int)bytes[1]];
                Buffer.BlockCopy(bytes, 2, strs, 0, (int)bytes[1]);
                return ASCIIEncoding.ASCII.GetString(strs);
            }
            else
            {
                return string.Empty;
            }


        }

        public static explicit operator String(PlcString obj)
        {
            return obj.GetString();
        }

        public  object FromBytes(byte[] bytes, ref double numBytes)
        {
            numBytes = Math.Ceiling(numBytes);
            if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0)
                numBytes++;
            Buffer.BlockCopy(bytes, (int)numBytes, this.bytes, 0, this.bytes.Length);
            numBytes += this.bytes.Length;
            return this;
        }

        public int GetSize()
        {
            return bytes.Length;
        }

        public void ToBytes(byte[] bytes, ref double numBytes)
        {
            numBytes = Math.Ceiling(numBytes);
            if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0)
                numBytes++;
            Buffer.BlockCopy(this.bytes, 0,bytes, (int)numBytes, this.bytes.Length);
            numBytes += this.bytes.Length;
        }
        public void Display()
        {
            foreach (var x in bytes)
                Console.Write(" {0:X2}", x);
            Console.WriteLine();
        }
    }

这个类实现了如下的接口:

 public interface IPlcParse
    {
        void ToBytes(byte[] bytes, ref double numBytes);
        object FromBytes(byte[] bytes, ref double numBytes);
        int GetSize();
    }

https://gitee.com/mao_qin_bin/s7netplus

算了,太多了,大家到这个地方下载吧. 注意版权.是我的!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

在原有的S7的基础上更改的实现.(git下来之后,我们再开始下一步)

下面我们来实现VS中的step步:

public class Recipe_Step
    {
        public short element1;
        public short element2;
        public short element3;
        public short element4;
        public short element5;
        public short element6;
        public double element7;
        public double element8;
        public double element9;
        public double element10;
        public double element11;
        public double element12;
        public double element13;
        public bool element14;
        public bool element15;
        public short element16;
    }

画像

1,int--投射成short

2,real-投射成double

3,注意大小是64个字节.我们来计算下我们的类的大小.

 public static void demo5()
        {

            PlcClassHelper.DisplayObject(new Recipe_Step());


        }

结果:

 Recipe_Step : ConsoleApp1.PlcClass.Recipe_Step   Size:44

Class of Recipe_Step
   Int16 : 0   Size:2
   Int16 : 0   Size:2
   Int16 : 0   Size:2
   Int16 : 0   Size:2
   Int16 : 0   Size:2
   Int16 : 0   Size:2
   Double : 0   Size:4
   Double : 0   Size:4
   Double : 0   Size:4
   Double : 0   Size:4
   Double : 0   Size:4
   Double : 0   Size:4
   Double : 0   Size:4
   Boolean : False   Size:2
   Boolean : False   Size:2
   Int16 : 0   Size:2

OK.

然后再测试下Recipe1:

 public class Recipe
    {
        public static Recipe StaticInstance=new Recipe();
        public PlcString Recipe_Name = new PlcString(20, "");
        public Recipe_Step[] Recipe_Steps = new Recipe_Step[30];
        private Recipe()
        {
            for(var i = 0; i < Recipe_Steps.Length; i++)
            {
                Recipe_Steps[i] = new Recipe_Step();
            }
        }
    }

这是PLC数据块(非优化)编译好的

画像

这是我测试的:

Recipe : ConsoleApp1.PlcClass.Recipe   Size:1342

Class of Recipe
   PlcString :  String =   Bytes = : 14  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0    Size:22

Array of ConsoleApp1.PlcClass.Recipe_Step[] :
     Recipe_Step : ConsoleApp1.PlcClass.Recipe_Step   Size:44

Class of Recipe_Step
       Int16 : 0   Size:2
       Int16 : 0   Size:2
       Int16 : 0   Size:2
       Int16 : 0   Size:2
       Int16 : 0   Size:2
       Int16 : 0   Size:2
       Double : 0   Size:4
       Double : 0   Size:4
       Double : 0   Size:4
       Double : 0   Size:4
       Double : 0   Size:4
       Double : 0   Size:4
       Double : 0   Size:4
       Boolean : False   Size:2
       Boolean : False   Size:2
       Int16 : 0   Size:2
     Recipe_Step : ConsoleApp1.PlcClass.Recipe_Step   Size:44

完全正确.下面设定下一些数据并且从PLC的读取:

10.5 设置PLC数据

画像

画像

10.6 从PLC读取数据:

画像

完全一致:

10.7 将数据写入PLC:

 public static void demo6()
        {

            Recipe curRecipe = Recipe.StaticInstance;//引用配方
            Recipe.Initial();
            short t = 0;
            foreach(var step in curRecipe.Recipe_Steps)
            {
                step.element1 = t++;
                step.element16 = t++;


            }






            Plc plc = new Plc(CpuType.S71200, "192.168.3.62", 0, 1);
            try
            {
                plc.Open();
                plc.WriteBytes(DataType.DataBlock、1、0、PlcClass.ToBytes(curRecipe))。
                PlcClassHelper.DisplayObject(curRecipe)。
            }
            キャッチ(PlcException電子)
            {
                Console.WriteLineを( " Exception'Message {0}は、例外のにErrcode {1}である "、e.Message、e.ErrorCode.ToString())。
            }
            最後に
            {
                plc.Close();
            }

        }

上記初期staticInstanceは、実際の列に初期化されます。

表示結果:PLC

画像

表示結果:VS

画像

おすすめ

転載: www.cnblogs.com/frogkiller/p/12346066.html