13. Start and stop a PLC
Imports PLCcom
Public Class newClass
Private Device As PLCcomDevice
'see section 'connect' for declare and connect a PLCcom-Device
#Region "StartPLC"
Private Sub btnStartPLC_Click(sender As Object, e As EventArgs) Handles btnStartPLC.Click
'execute function
Dim res As OperationResult = Device.StartPLC()
'evaluate results
txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
#End Region
#Region "StopPLC"
Private Sub btnStopPLC_Click(sender As Object, e As EventArgs) Handles btnStopPLC.Click
'execute function
Dim res As OperationResult = Device.StopPLC()
'evaluate results
txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
#End Region
End Class
14. Reading block list from PLC
Imports PLCcom
Public Class newClass
Private Device As PLCcomDevice
'see section 'connect' for declare and connect a PLCcom-Device
Private Sub btnGetBlockList_Click(sender As Object, e As EventArgs) Handles btnGetBlockList.Click
Dim BlockType As eBlockType = eBlockType.AllBlocks
Dim res As BlockListResult = Device.GetBlockList(BlockType)
'evaluate results
txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
txtResult.Text = String.Empty
If res.Quality = OperationResult.eQuality.GOOD Then
Dim sb As New System.Text.StringBuilder()
For Each ble As BlockListEntry In res.BlockList
sb.Append(ble.BlockType.ToString())
sb.Append(ble.BlockNumber.ToString())
sb.Append(Environment.NewLine)
Next
txtResult.Text = sb.ToString()
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
15. Get length of block
Imports PLCcom
Public Class newClass
Private Device As PLCcomDevice
'see section 'connect' for declare and connect a PLCcom-Device
Private Sub btnGetBlockLen_Click(sender As Object, e As EventArgs) Handles btnGetBlockLen.Click
'get Len from DB100
Dim BlockNumber As Integer = 100
Dim BlockType As eBlockType = eBlockType.DB
'evaluate results
Dim res As BlockListLengthResult = Device.GetBlockLenght(BlockType, BlockNumber)
'evaluate results
txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
txtMessage.ForeColor = If(res.Quality = OperationResult.eQuality.GOOD, Color.Black, Color.Red)
txtResult.Text = String.Empty
If res.Quality = OperationResult.eQuality.GOOD Then
Dim sb As New System.Text.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)
End If
End Sub
End Class
16. Backup block
Imports PLCcom
Public Class newClass
Private Device As PLCcomDevice
'see section 'connect' for declare and connect a PLCcom-Device
Private Sub btnBackupBlock_Click(sender As Object, e As EventArgs) Handles btnBackupBlock.Click
'Backup OB1
Dim BlockNumber As Integer = 1
Dim Blocktype As eBlockType = eBlockType.OB
'open SaveFileDialog
Dim sfd As New SaveFileDialog()
sfd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*"
Dim dr As DialogResult = sfd.ShowDialog()
If dr = DialogResult.OK Then
'read Block into ReadPLCBlockResult
Dim res As ReadPLCBlockResult = Device.ReadPLCBlock_MC7(Blocktype, BlockNumber)
txtMessage.Text = res.Message
txtResult.Text = ""
If res.Quality = OperationResult.eQuality.GOOD Then
'save buffer in specified file
Dim fs As 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)
End If
Else
MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
End Class
17. Restore block
Imports PLCcom
Public Class newClass
Private Device As PLCcomDevice
'see section 'connect' for declare and connect a PLCcom-Device
Private Sub btnRestoreBlock_Click(sender As Object, e As EventArgs) Handles btnRestoreBlock.Click
Dim ofd As New OpenFileDialog()
ofd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*"
Dim dr As DialogResult = ofd.ShowDialog()
If dr = DialogResult.OK Then
Dim fs As New System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim buffer As Byte() = New Byte(fs.Length - 1) {}
fs.Read(buffer, 0, CInt(fs.Length))
fs.Close()
'Write Buffer into PLC
Dim Requestdata As New WritePLCBlockRequest(buffer, eBlockType.OB, 1)
Dim res As OperationResult = Device.WritePLCBlock_MC7(Requestdata)
txtMessage.Text = res.Message
txtResult.Text = ""
If res.Quality = OperationResult.eQuality.GOOD Then
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)
End If
Else
MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
End Class
18. Delete block
Imports PLCcom
Public Class newClass
Private Device As PLCcomDevice
'see section 'connect' for declare and connect a PLCcom-Device
Private Sub btnDeleteBlock_Click(sender As Object, e As EventArgs) Handles btnDeleteBlock.Click
'Delete DB100
Dim BlockNumber As Integer = 100
Dim BlockType As eBlockType = eBlockType.DB
Dim res As OperationResult = Device.DeleteBlock(BlockType, BlockNumber)
'evaluate results
txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
If res.Quality = OperationResult.eQuality.GOOD Then
MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
Reference : https://www.plccom.net/en/plccom/for-s7/fors7-code-examples/