Reading and writing of PLC variables
As mentioned in the introduction, the ADS Script DLL is primarily designed for scripting languages. Nevertheless, at this point is a Visual Basic program. It only serves to better demonstrate the capabilities of the ADS Script DLL. For applications that are to be created with Visual Basic, the ADS-OCX is better suited.
First load the PLC program into the local PLC (runtime system 1) on your PC. Then start the Visual Basic program:
Unless you provide some other indication, a communication channel is created to your local PLC. By entering a different AdsAmsNetId and port number, you can connect to a different runtime system on another PC. The above-mentioned PLC program, however, must still be active on that other runtime system. It is also necessary that a remote computer, if selected, is entered into the TwinCAT System Control.
You can either read the PLC variables by address (IndexGroup and IndexOffset), or by variable name. The same applies to writing new values. This has no meaning for the operator. It is only to list the two possibilities.
In the upper right corner the state of the PLC can be read and changed. A 5 corresponds to the RUN state, while a 6 corresponds to STOP.
Visual Basic 6 program
Option Explicit
Public TcClientSync As TCSCRIPTLib.TcScriptSync
Public nAdsAmsPort As Integer
Public strAdsAmsNetId As String
Private Sub Form_Load()
Set TcClientSync =
CreateObject("TcScript.TcScriptSync")
Call TcClientSync.ConnectTo("",
801)
End Sub
Private Sub Form_Terminate()
Set TcClientSync = Nothing
End Sub
Private Sub cmdConnect_Click()
nAdsAmsPort = CLng(txtAdsAmsPort.Text)
if (Len(txtAdsAmsNetId.Text) > 0) Then
strAdsAmsNetId =
txtAdsAmsNetId.Text
Else
strAdsAmsNetId =
""
End If
Set TcClientSync =
CreateObject("TcScript.TcScriptSync")
Call TcClientSync.ConnectTo(strAdsAmsNetId,
nAdsAmsPort)
End Sub
Private Sub cmdRead_Click()
Dim VarBool As Variant
Dim VarInt8 As Byte
Dim VarInt16 As Integer
Dim VarInt32 As Long
Dim VarReal32 As Single
Dim VarReal64 As Double
VarBool = TcClientSync.ReadBool(&H4020,
0)
lblBoolean.Caption = VarBool
VarInt8 = TcClientSync.ReadInt8(&H4020,
2)
lblInt8.Caption = VarInt8
VarInt16 = TcClientSync.ReadInt16(&H4020,
4)
lblInt16.Caption = VarInt16
VarInt32 = TcClientSync.ReadInt32(&H4020,
6)
lblInt32.Caption = VarInt32
VarReal32 = TcClientSync.ReadReal32(&H4020,
10)
lblReal32.Caption = VarReal32
VarReal64 = TcClientSync.ReadReal64(&H4020,
14)
lblReal64.Caption = VarReal64
End Sub
Private Sub cmdReadByName_Click()
Dim VarBool As Variant
Dim VarInt8 As Byte
Dim VarInt16 As Integer
Dim VarInt32 As Long
Dim VarReal32 As Single
Dim VarReal64 As Double
VarBool =
TcClientSync.ReadBoolVar(".PLCBoolVar")
lblBoolean.Caption = VarBool
VarInt8 =
TcClientSync.ReadInt8Var(".PLCSIntVar")
lblInt8.Caption = VarInt8
VarInt16 =
TcClientSync.ReadInt16Var(".PLCIntVar")
lblInt16.Caption = VarInt16
VarInt32 =
TcClientSync.ReadInt32Var(".PLCDIntVar")
lblInt32.Caption = VarInt32
VarReal32 =
TcClientSync.ReadReal32Var(".PLCRealVar")
lblReal32.Caption = VarReal32
VarReal64 =
TcClientSync.ReadReal64Var(".PLCLRealVar")
lblReal64.Caption = VarReal64
End Sub
Private Sub cmdWrite_Click()
Dim VarBool As Boolean
Dim VarInt8 As Byte
Dim VarInt16 As Integer
Dim VarInt32 As Long
Dim VarReal32 As Single
Dim VarReal64 As Double
If (txtBoolean.Text <> "")
Then
VarBool =
CBool(txtBoolean.Text)
Call
TcClientSync.WriteBool(&H4020, 0, VarBool)
txtBoolean.Text =
VarBool
End If
If (txtInt8.Text <> "")
Then
VarInt8 =
CByte(txtInt8.Text)
Call
TcClientSync.WriteInt8(&H4020, 2, VarInt8)
txtInt8.Text =
VarInt8
End If
If (txtInt16.Text <> "")
Then
VarInt16 =
CInt(txtInt16.Text)
Call
TcClientSync.WriteInt16(&H4020, 4, VarInt16)
txtInt16.Text =
VarInt16
End If
If (txtInt32.Text <> "")
Then
VarInt32 =
CLng(txtInt32.Text)
Call
TcClientSync.WriteInt32(&H4020, 6, VarInt32)
txtInt32.Text =
VarInt32
End If
If (txtReal32.Text <> "")
Then
VarReal32 =
CSng(txtReal32.Text)
Call
TcClientSync.WriteReal32(&H4020, 10, VarReal32)
txtReal32.Text =
VarReal32
End If
If (txtReal64.Text <> "")
Then
VarReal64 =
CDbl(txtReal64.Text)
Call
TcClientSync.WriteReal64(&H4020, 14, VarReal64)
txtReal64.Text =
VarReal64
End If
End Sub
Private Sub cmdWriteByName_Click()
Dim VarBool As Boolean
Dim VarInt8 As Byte
Dim VarInt16 As Integer
Dim VarInt32 As Long
Dim VarReal32 As Single
Dim VarReal64 As Double
If (txtBoolean.Text <> "")
Then
VarBool =
CBool(txtBoolean.Text)
Call
TcClientSync.WriteBoolVar(".PLCBoolVar", VarBool)
txtBoolean.Text =
VarBool
End If
If (txtInt8.Text <> "")
Then
VarInt8 =
CByte(txtInt8.Text)
Call
TcClientSync.WriteInt8Var(".PLCSIntVar", VarInt8)
txtInt8.Text =
VarInt8
End If
If (txtInt16.Text <> "")
Then
VarInt16 =
CInt(txtInt16.Text)
Call
TcClientSync.WriteInt16Var(".PLCIntVar", VarInt16)
txtInt16.Text =
VarInt16
End If
If (txtInt32.Text <> "")
Then
VarInt32 =
CLng(txtInt32.Text)
Call
TcClientSync.WriteInt32Var(".PLCDIntVar", VarInt32)
txtInt32.Text =
VarInt32
End If
If (txtReal32.Text <> "")
Then
VarReal32 =
CSng(txtReal32.Text)
Call
TcClientSync.WriteReal32Var(".PLCRealVar",
VarReal32)
txtReal32.Text =
VarReal32
End If
If (txtReal64.Text <> "")
Then
VarReal64 =
CDbl(txtReal64.Text)
Call
TcClientSync.WriteReal64Var(".PLCLRealVar",
VarReal64)
txtReal64.Text =
VarReal64
End If
End Sub
Private Sub cmdReadState_Click()
lblAdsState.Caption =
TcClientSync.ReadAdsState()
End Sub
Private Sub cmdWriteState_Click()
If (txtAdsState.Text <> "")
Then
If (txtAdsState.Text
<> TcClientSync.ReadAdsState()) Then
Call TcClientSync.WriteAdsState(txtAdsState.Text)
End If
End If
End Sub
PLC program
VAR_GLOBAL
PLCBoolVar AT %MX0.0: BOOL := TRUE;
PLCSIntVar AT %MB2 : USINT := 75;
PLCIntVar AT %MB4 : UINT := 12423;
PLCDIntVar AT %MB6 : UDINT := 43234532;
PLCRealVar AT %MB10 : REAL := 3.1415;
PLCLRealVar AT %MB14 : LREAL := 3.141592654;
END_VAR
Language / IDE | Unpack sample program |
---|---|
Visual Basic 6 |