Ejemplo n.º 1
0
 //------------------------------------------------------------------------------
 // CPU Info : unit info
 //------------------------------------------------------------------------------
 static void CpuInfo()
 {
     S7Client.S7CpuInfo Info = new S7Client.S7CpuInfo();
     int res = Client.GetCpuInfo(ref Info);
     if (Check(res, "Unit Info"))
     {
         Console.WriteLine("  Module Type Name : "+ Info.ModuleTypeName);
         Console.WriteLine("  Serial Number    : "+ Info.SerialNumber);
         Console.WriteLine("  AS Name          : "+ Info.ASName);
         Console.WriteLine("  Module Name      : "+ Info.ModuleName);
     };
 }
Ejemplo n.º 2
0
        public void upload()
        {
            Console.WriteLine("Getting CPU information...");

            S7Client.S7OrderCode oc = new S7Client.S7OrderCode();
            int result = MyClient.GetOrderCode(ref oc);

            if (result == 0)
            {
                this.setOrderCode(oc);
            }
            else
            {
                string error = "Failed to get Order Code";
                throw new wPlcException(error, result);
            }

            S7Client.S7CpuInfo ci = new S7Client.S7CpuInfo();
            result = MyClient.GetCpuInfo(ref ci);

            if (result == 0)
            {
                this.setCpuInfo(ci);
            }
            else
            {
                string error = "Failed to get CPU info";
                throw new wPlcException(error, result);
            }

            Console.WriteLine("Uploading program blocks... ");

            Dictionary<wBlockType, ushort[]> blockList = new Dictionary<wBlockType, ushort[]>();
            Dictionary<wBlockType, int> blockCount = new Dictionary<wBlockType, int>();
            int totalBlockCount = 0;

            blockList = new Dictionary<wBlockType, ushort[]>();

            S7Client.S7BlocksList bl = new S7Client.S7BlocksList();

            result = MyClient.ListBlocks(ref bl);
            List<wBlockType> blockTypeList = new List<wBlockType>();

            if (result == 0)
            {
                if (bl.OBCount > 0)
                    blockTypeList.Add(wBlockType.OB);
                if (bl.FBCount > 0)
                    blockTypeList.Add(wBlockType.FB);
                if (bl.FCCount > 0)
                    blockTypeList.Add(wBlockType.FC);
                if (bl.DBCount > 0)
                    blockTypeList.Add(wBlockType.DB);
                if (bl.SFBCount > 0)
                    blockTypeList.Add(wBlockType.SFB);
                if (bl.SFCCount > 0)
                    blockTypeList.Add(wBlockType.SFC);
                if (bl.SDBCount > 0)
                    blockTypeList.Add(wBlockType.SDB);
            }
            else
            {
                string error = "Failed to list all blocks";
                throw new wPlcException(error, result);
            }

            foreach (wBlockType blockType in blockTypeList)
            {
                blockList.Add(blockType, new ushort[MAX_BLOCK]);
                int resultBlockCount = blockList[blockType].Length;               
                result = MyClient.ListBlocksOfType((int)blockType, blockList[blockType], ref resultBlockCount);
                if (result == 0)
                {
                    blockCount.Add(blockType, resultBlockCount);
                    totalBlockCount += resultBlockCount;
                }
                else
                {
                    string error = "Failed to list blocks of type " + blockType;
                    throw new wPlcException(error, result);
                }
            }

            int uploadedBlockCount = 0;

            foreach (wBlockType blockType in blockTypeList)
            {

                for (int i = 0; i < blockCount[blockType]; i++)
                {
                    //p.setValue(((uploadedBlockCount + 1) / totalBlockCount) * 100);
                    S7Client.S7BlockInfo blockInfo = new S7Client.S7BlockInfo();
                    result = MyClient.GetAgBlockInfo((int)blockType, blockList[blockType][i], ref blockInfo);
                        
                    if (result == 0)
                    {
                        byte[] buffer = new byte[16384];
                        int bufferSize = buffer.Length;

                        if (blockType != wBlockType.SFC && blockType != wBlockType.SFB)
                        {
                            result = MyClient.FullUpload((int)blockType, blockList[blockType][i], buffer, ref bufferSize);

                            if (result != 0)
                            {
                                string error = "Could not upload " + blockType + blockList[blockType][i];
                                throw new wPlcException(error, result);
                            }
                        }
                        else
                        {
                            bufferSize = 0;
                        }

                        byte[] data = new byte[bufferSize];
                        Array.Copy(buffer, data, data.Length);
                        this.addCpuBlock(blockInfo, data);
                        Console.WriteLine(this.blocks.Last().ToString() + " loaded. Size: " + this.blocks.Last().loadSize + " bytes.");
                    }
                    else
                    {
                        string error = "Could not get Block Info for " + blockType + blockList[blockType][i];
                        throw new wPlcException(error, result);
                    }

                    uploadedBlockCount++;
                }

            }
            Console.WriteLine("Done!");
        }
Ejemplo n.º 3
0
 // This method will be called when the thread is started.
 public void DoWork()
 {
     while (!_shouldStop)
     {
         if (plc != null)
         {
             if (plc.Connected())
             {
                 parent.setConnected();
                 Console.Out.WriteLine("Connected");
                 S7Client.S7CpuInfo Info = new S7Client.S7CpuInfo();
                 plc.GetCpuInfo(ref Info);
                 int i = 0;
                 plc.PlcGetStatus(ref i);
                 parent.setText(Info.SerialNumber);
                 parent.setStatus(i);
             }
             else
             {
                 parent.setDiconnected();
                 Console.Out.WriteLine("Not Connected");
             }
         }
     }
     Thread.Sleep(100);
 }