public void connect(string ipAddress, int rack, int slot) { MyClient = new S7Client(); this.ipAddress = ipAddress; this.rack = rack; this.slot = slot; int result = MyClient.ConnectTo(this.ipAddress, this.rack, this.slot); if (result == 0) { Console.WriteLine("Connected to CPU at IP Address " + ipAddress); S7Client.S7OrderCode oc = new S7Client.S7OrderCode(); result = MyClient.GetOrderCode(ref oc); if (result == 0) { Console.WriteLine("CPU Order Code:\t" + this.orderCode); S7Client.S7BlocksList bl = new S7Client.S7BlocksList(); result = MyClient.ListBlocks(ref bl); if (result == 0) { Console.WriteLine("OB Count:\t" + bl.OBCount); Console.WriteLine("FC Count:\t" + bl.FCCount); Console.WriteLine("FB Count:\t" + bl.FBCount); Console.WriteLine("DB Count:\t" + bl.DBCount); Console.WriteLine("SFC Count:\t" + bl.SFCCount); Console.WriteLine("SFB Count:\t" + bl.SFBCount); Console.WriteLine("SDB Count:\t" + bl.SDBCount); } else //Failed to List Blocks { string error = "Failed to list blocks"; throw new wPlcException(error, result); } } else //Failed to get Order Code { string error = "Failed to get Order Code"; throw new wPlcException(error, result); } } else //Failed to connect to CPU { string error = "Failed to connect to CPU"; throw new wPlcException(error, result); } }
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!"); }