 |
plc.myforum.ro PLCs users www.plc.myforum.ro!
|
| View previous topic :: View next topic |
| Author |
Message |
oldsnake Junior

Joined: 24 Jun 2012 Posts: 14
|
Posted: 31/07/2015 11:07 Post subject: VB.net 2013 & S7Prosim |
|
|
I write below code but not connect to S7PLCSIM.
Please write code and help me solve.
Thank.
| Code: | Option Explicit On
Public Class Form1
Private WithEvents S7ProSim1 As S7PROSIMLib.S7ProSim
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
S7ProSim1 = New S7PROSIMLib.S7ProSim
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
S7ProSim1.Connect()
End Sub
Private Sub Form_Unload(ByVal cancel As Integer)
S7ProSim1.Disconnect()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim s As String
s = S7ProSim1.GetState()
Label1.Text = s
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim s As String
S7ProSim1.SetState("RUN_P")
s = S7ProSim1.GetState()
Label1.Text = s
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim s As String
S7ProSim1.SetState("STOP")
s = S7ProSim1.GetState()
Label1.Text = s
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pData As Byte
pData = 8
S7ProSim1.WriteFlagValue(0, 0, pData)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim pData As Byte
S7ProSim1.ReadFlagValue(0, 0, 2, pData)
Label1.Text = pData
End Sub
End Class |
|
|
| Back to top |
|
 |
zwang Junior 2

Joined: 30 Jul 2015 Posts: 42 Location: China
|
Posted: 31/07/2015 20:30 Post subject: |
|
|
Take a look at NetToPLCSim
http://sourceforge.net/p/nettoplcsim/discussion/912717/thread/52b93a3a/
or
Using S7-PROSIM with Siemens S7-PLCSIM
https://alexsentcha.wordpress.com/using-s7-prosim-with-siemens-s7-plcsim/
Example VB.NET application with S7-PLCSIM version 5.4 and S7-PROSIM COM API:
| Code: | Private WithEvents S7ProSim As New S7PROSIMLib.S7ProSim
'Connection
S7ProSim.Connect()
S7ProSim.SetScanMode(1)
S7ProSim.Disconnect()
Sub Data()
Dim pDataB As Byte
Dim pDataW As Short
Dim pDataI As Integer
Dim pDataInA(3) As Short
Dim pDataInB(2) As Byte
'Write bit input 100.4
S7ProSim.WriteInputPoint(100, 5, True)
'Write inputs array 3x-16 bit integer, address 120, 122, 124
pDataInA(0) = 12
pDataInA(1) = 34
pDataInA(2) = CShort(TrackBar1.Value)
S7ProSim.WriteInputImage(120, pDataInA)
'Write inputs WORD, address 130
pDataInB(0) = 12
pDataInB(1) = 24
S7ProSim.WriteInputImage(130, pDataInB)
'Read Byte output, address 100.0 (S7PROSIMLib.PointDataTypeConstants.S7_Byte = 2)
S7ProSim.ReadOutputPoint(100, 0, 2, pDataB)
CheckBox1.Checked = IsBitSet(pDataB, 4)
'Read Byte from DB, address 156 byte address 20 (S7PROSIMLib.PointDataTypeConstants.S7_Byte = 2)
S7ProSim.ReadDataBlockValue(156, 20, 0, 2, pDataB)
LabelByte.Text = CStr(pDataB)
'Read integer MW 16 bit (S7PROSIMLib.PointDataTypeConstants.S7_Word = 3)
S7ProSim.ReadFlagValue(1200, 0, 3, pDataW)
LabelWord.Text = CStr(pDataW)
'Read DWORD from DB 12 address 58 (S7PROSIMLib.PointDataTypeConstants.S7_DoubleWord = 4)
S7ProSim.ReadDataBlockValue(12, 58, 0, 4, pDataI)
LabelInt.Text = CStr(pDataI)
'Read float 32 bit from DB 10 DWORD address 64 (S7PROSIMLib.PointDataTypeConstants.S7_DoubleWord = 4)
S7ProSim.ReadDataBlockValue(10, 64, 0, 4, pDataI)
LabelFL.Text = String.Format("{0,10:N2}", CDbl(BitConverter.ToSingle(BitConverter.GetBytes(pDataI), 0)))
'LabelFL = 738.45
End Sub
Public Function IsBitSet(ByVal InByte As Byte, ByVal Bit As Byte) As Boolean
IsBitSet = ((InByte And (2 ^ Bit)) > 0)
End Function |
http://sourceforge.net/projects/nettoplcsim/
http://nettoplcsim.sourceforge.net/index.html |
|
| Back to top |
|
 |
zwang Junior 2

Joined: 30 Jul 2015 Posts: 42 Location: China
|
Posted: 31/07/2015 20:32 Post subject: |
|
|
Take a look at NetToPLCSim
http://sourceforge.net/p/nettoplcsim/discussion/912717/thread/52b93a3a/
or
Using S7-PROSIM with Siemens S7-PLCSIM
https://alexsentcha.wordpress.com/using-s7-prosim-with-siemens-s7-plcsim/
Example VB.NET application with S7-PLCSIM version 5.4 and S7-PROSIM COM API:
| Code: | Private WithEvents S7ProSim As New S7PROSIMLib.S7ProSim
'Connection
S7ProSim.Connect()
S7ProSim.SetScanMode(1)
S7ProSim.Disconnect()
Sub Data()
Dim pDataB As Byte
Dim pDataW As Short
Dim pDataI As Integer
Dim pDataInA(3) As Short
Dim pDataInB(2) As Byte
'Write bit input 100.4
S7ProSim.WriteInputPoint(100, 5, True)
'Write inputs array 3x-16 bit integer, address 120, 122, 124
pDataInA(0) = 12
pDataInA(1) = 34
pDataInA(2) = CShort(TrackBar1.Value)
S7ProSim.WriteInputImage(120, pDataInA)
'Write inputs WORD, address 130
pDataInB(0) = 12
pDataInB(1) = 24
S7ProSim.WriteInputImage(130, pDataInB)
'Read Byte output, address 100.0 (S7PROSIMLib.PointDataTypeConstants.S7_Byte = 2)
S7ProSim.ReadOutputPoint(100, 0, 2, pDataB)
CheckBox1.Checked = IsBitSet(pDataB, 4)
'Read Byte from DB, address 156 byte address 20 (S7PROSIMLib.PointDataTypeConstants.S7_Byte = 2)
S7ProSim.ReadDataBlockValue(156, 20, 0, 2, pDataB)
LabelByte.Text = CStr(pDataB)
'Read integer MW 16 bit (S7PROSIMLib.PointDataTypeConstants.S7_Word = 3)
S7ProSim.ReadFlagValue(1200, 0, 3, pDataW)
LabelWord.Text = CStr(pDataW)
'Read DWORD from DB 12 address 58 (S7PROSIMLib.PointDataTypeConstants.S7_DoubleWord = 4)
S7ProSim.ReadDataBlockValue(12, 58, 0, 4, pDataI)
LabelInt.Text = CStr(pDataI)
'Read float 32 bit from DB 10 DWORD address 64 (S7PROSIMLib.PointDataTypeConstants.S7_DoubleWord = 4)
S7ProSim.ReadDataBlockValue(10, 64, 0, 4, pDataI)
LabelFL.Text = String.Format("{0,10:N2}", CDbl(BitConverter.ToSingle(BitConverter.GetBytes(pDataI), 0)))
'LabelFL = 738.45
End Sub
Public Function IsBitSet(ByVal InByte As Byte, ByVal Bit As Byte) As Boolean
IsBitSet = ((InByte And (2 ^ Bit)) > 0)
End Function |
http://sourceforge.net/projects/nettoplcsim/
http://nettoplcsim.sourceforge.net/index.html |
|
| Back to top |
|
 |
valz Member 3


Joined: 26 Jul 2015 Posts: 771
|
Posted: 31/07/2015 20:45 Post subject: |
|
|
arcangel21 put a link
VB.Net – exchange data with Siemens PLC
http://plc.myforum.ro/who-can-crack-the-comdrvs7-please--vt3876.html
https://alexsentcha.wordpress.com/libnodave-exchange-data-with-siemens-plc-2/vb-net-exchange-data-with-siemens-plc/
Library contains all necessary functions for:
– Connection with PLC
| Code: | Public fds As libnodave.daveOSserialType
Public di As libnodave.daveInterface
Public dc As libnodave.daveConnection
Public res As Integer
Public buf(100) As Byte
Public localMPI As Integer = 0
Public rack As Integer = 0
Public slot As Integer = 2
Public plcMPI As Integer = 2
Public Connection As Boolean = False
Public IP As String = "192.168.1.100"
Private Sub ConnectPLC.....
fds.rfd = libnodave.openSocket(102, IP)
fds.wfd = fds.rfd
If fds.rfd > 0 Then ' if step 1 is ok
di = New libnodave.daveInterface(fds, "IF1",
0, libnodave.daveProtoISOTCP,
libnodave.daveSpeed187k)
di.setTimeout(1000000)
res = di.initAdapter
If res = 0 Then ' init Adapter is ok
' rack amd slot don't matter in case of MPI
dc = New libnodave.daveConnection(di, 0,
rack, slot)
res = dc.connectPLC()
If res = 0 Then
Connection = True
ToolStripStatusLabel1.Text = "Connected " + IP
End If
End If
End If
End Sub
Private Sub DisconnectPLC....
dc.disconnectPLC()
ToolStripStatusLabel1.Text = "Disconnected."
End Sub
|
– Read data from PLC
| Code: | Private Sub Button2_Click(ByVal sender....
Dim x1 As Double = 12345.9
Dim x2 As Integer = 123456
Dim x3 As Short = 1234
Dim x4 As Byte = 34
If Connection Then
'write 4 bytes s7 float to MD4
buf = BitConverter.GetBytes(libnodave.daveToPLCfloat(x1))
res = dc.writeBytes(libnodave.daveFlags, 0, 4, 4, buf)
'write 4 bytes s7 DWORD or DINT to MD12
buf = BitConverter.GetBytes(libnodave.daveSwapIed_32(x2))
res = dc.writeBytes(libnodave.daveFlags, 0, 12, 4, buf)
'write 2 bytes s7 WORD or INT to MW568
buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
res = dc.writeBytes(libnodave.daveFlags, 0, 568, 2, buf)
'write byte to MB80
buf(0) = x4
res = dc.writeBytes(libnodave.daveFlags, 0, 80, 1, buf)
'write 2 bytes s7 WORD or INT to DB34.DBW2
buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
res = dc.writeBytes(libnodave.daveFlags, 34, 2, 2, buf)
Else
MsgBox("No connection with PLC!")
End If
End Sub |
Write data to PLC
| Code: |
Private Sub Button2_Click(ByVal sender....
Dim x1 As Double = 12345.9
Dim x2 As Integer = 123456
Dim x3 As Short = 1234
Dim x4 As Byte = 34
If Connection Then
'write 4 bytes s7 float to MD4
buf = BitConverter.GetBytes(libnodave.daveToPLCfloat(x1))
res = dc.writeBytes(libnodave.daveFlags, 0, 4, 4, buf)
'write 4 bytes s7 DWORD or DINT to MD12
buf = BitConverter.GetBytes(libnodave.daveSwapIed_32(x2))
res = dc.writeBytes(libnodave.daveFlags, 0, 12, 4, buf)
'write 2 bytes s7 WORD or INT to MW568
buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
res = dc.writeBytes(libnodave.daveFlags, 0, 568, 2, buf)
'write byte to MB80
buf(0) = x4
res = dc.writeBytes(libnodave.daveFlags, 0, 80, 1, buf)
'write 2 bytes s7 WORD or INT to DB34.DBW2
buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
res = dc.writeBytes(libnodave.daveFlags, 34, 2, 2, buf)
Else
MsgBox("No connection with PLC!")
End If
End Sub |
– Write bits to PLC
| Code: | Private Sub Button3_Click(....
Dim Adr As Integer
Dim Par As Boolean = True
Dim InputNum As Integer = 20 'input I20.3
Dim BitNum As Integer = 3
If Par Then
buf(0) = 255 'write 1
Else
buf(0) = 0 'write 0
End If
If Connection Then
Adr = InputNum * 8 + BitNum
'write Input I20.3
res = dc.writeBits(libnodave.daveInputs, 0, Adr, 1, buf)
'write Merke M20.3
res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf)
'write output Q20.3
res = dc.writeBits(libnodave.daveOutputs, 0, Adr, 1, buf)
Else
MsgBox("No connection with PLC!")
End If
End Sub
|
– Reading byte from PLC and setting bits
| Code: | Private Sub Timer1_Tick(....
Dim a As Byte
If Connection Then
res = dc.readBytes(libnodave.daveInputs, 0, 0, 1, buf)
If res = 0 Then
a = dc.getU8
If IsBitSet(a, 0) Then
Panel1.BackColor = Color.Lime
Else
Panel1.BackColor = Color.DarkGreen
End If
If IsBitSet(a, 1) Then
Panel2.BackColor = Color.Lime
Else
Panel2.BackColor = Color.DarkGreen
End If
Else
ToolStripStatusLabel1.Text = "Read data. " + libnodave.daveStrerror(res)
End If
End Sub
Public Function IsBitSet(ByVal InByte As Byte, ByVal Bit As Byte) As Boolean
'Is a n'th bit in InByte 1 of not?
IsBitSet = ((InByte And (2 ^ Bit)) > 0)
End Function |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro
|