13. Start and stop a PLC
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
#region StartPLC
private void btnStartPLC_Click(object sender, EventArgs e)
{
//execute function
OperationResult res = Device.StartPLC();
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
if (res.Quality.Equals(OperationResult.eQuality.GOOD))
{
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
#region StopPLC
private void btnStopPLC_Click(object sender, EventArgs e)
{
//execute function
OperationResult res = Device.StopPLC();
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
if (res.Quality.Equals(OperationResult.eQuality.GOOD))
{
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
}
}
14. Reading block list from PLC
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnGetBlockInfo_Click(object sender, EventArgs e)
{
eBlockType BlockType = eBlockType.AllBlocks;
BlockListResult res = Device.GetBlockList(BlockType);
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
txtResult.Text = string.Empty;
if (res.Quality == OperationResult.eQuality.GOOD)
{
StringBuilder sb = new StringBuilder();
foreach (BlockListEntry ble in res.BlockList)
{
sb.Append(ble.BlockType.ToString());
sb.Append(ble.BlockNumber.ToString());
sb.Append(Environment.NewLine);
}
txtResult.Text = sb.ToString();
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
15. Get length of block
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnGetBlockLen_Click(object sender, EventArgs e)
{
//get Len from DB100
int BlockNumber = 100;
eBlockType BlockType = eBlockType.DB;
//evaluate results
BlockListLengthResult res = Device.GetBlockLenght(BlockType, BlockNumber);
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
txtMessage.ForeColor = res.Quality == OperationResult.eQuality.GOOD ? Color.Black : Color.Red;
txtResult.Text = string.Empty;
if (res.Quality == OperationResult.eQuality.GOOD)
{
StringBuilder sb = new StringBuilder();
sb.Append(res.BlockType.ToString());
sb.Append(res.BlockNumber.ToString());
sb.Append(" Len:");
sb.Append(res.BlockLength.ToString());
sb.Append(Environment.NewLine);
txtResult.Text = sb.ToString();
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
16. Backup block
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnBackupBlock_Click(object sender, EventArgs e)
{
//Backup OB1
int BlockNumber = 1;
eBlockType Blocktype = eBlockType.OB;
//open SaveFileDialog
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*";
DialogResult dr = sfd.ShowDialog();
if (dr == DialogResult.OK)
{
//read Block into ReadPLCBlockResult
ReadPLCBlockResult res = Device.ReadPLCBlock_MC7(Blocktype, BlockNumber);
txtMessage.Text = res.Message;
txtResult.Text = "";
if (res.Quality == OperationResult.eQuality.GOOD)
{
//save buffer in specified file
System.IO.FileStream fs = new System.IO.FileStream(sfd.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
fs.Write(res.Buffer, 0, res.Buffer.Length);
fs.Close();
MessageBox.Show("Block " + Blocktype.ToString() + BlockNumber.ToString() + " successful saved in " + sfd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("operation unsuccessful", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
{
MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
17. Restore block
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnRestoreBlock_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Close();
//Write Buffer into PLC
WritePLCBlockRequest Requestdata = new WritePLCBlockRequest(buffer, eBlockType.OB, 1);
OperationResult res = Device.WritePLCBlock_MC7(Requestdata);
txtMessage.Text = res.Message;
txtResult.Text = "";
if (res.Quality == OperationResult.eQuality.GOOD)
{
MessageBox.Show("Block " + Requestdata.BlockInfo.Header.BlockType.ToString() + Requestdata.BlockInfo.Header.BlockNumber.ToString() + " successful saved in PLC from Source " + ofd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("operation unsuccessful", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
{
MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
18. Delete block
using System;
using PLCcom;
using System.Windows.Forms;
namespace CodeDokuCSharp
{
class newClass
{
PLCcomDevice Device;
//see section 'connect' for declare and connect a PLCcom-Device
private void btnDeleteBlock_Click(object sender, EventArgs e)
{
//Delete DB100
int BlockNumber = 100;
eBlockType BlockType = eBlockType.DB;
OperationResult res = Device.DeleteBlock(BlockType, BlockNumber);
//evaluate results
txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
if (res.Quality == OperationResult.eQuality.GOOD)
{
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
Reference : https://www.plccom.net/en/plccom/for-s7/fors7-code-examples/