AdsWriteVarConnect
Creates a fixed connection of a Visual Basic variable and a variable from an ADS device.
object.AdsWriteVarConnect(
adsVarName As String,
cbLength As Long,
nRefreshType As ADSOCXTRANSMODE,
nCycleTime As Integer,
pData As Variant
) As Long
Parameter
adsVarName
[in] Name of the ADS variable
cbLength
[in] Length of the data in bytes (see VB variable lengths)
nRefreshType
[in] Type of data exchange between VB variable and ADS variable (see the ADSOCXTRANSMODE data type). The method AdsWriteVarConnect supports (sensibly) only the ADSTRANS_CLIENTCYCLE mode. The value of the Visual Basic variable is written cyclically to the ADS device.
nCycleTime
[in] Write cycle in ms
pData
[in] Visual Basic variable from which the data is written into the ADS variable
Return value
See ADS error codes
Comments
If the connection to an ADS variable is no longer required, it should be released using the AdsWriteDisconnect() method. If only certain values are required in a form, the connection should only be created when the form is loaded and released again when the form is closed.
The variable type String is not supported.
Note on PLC: Ensure that Symbol download is enabled in PLC Control under Project / Options / TwinCAT. More detailed information can be found in the PLC Control manual.
The first parameter of the method consists of the POE name and the PLC variable to be addressed. If, for instance, the variable 'SPSVar1' from the function 'Funk1' is to be accessed, then 'Funk1.SPSVar1' must be supplied as the first parameter. When global variables are being accessed, the POE name is omitted, as, for instance, in '.SPSGlobVar'. The parameter adsVarName does not distinguish between upper and lower case letters.
Note on NC: Symbol download must be enabled for each axis in the System Manager. This can be specified in the configuration dialog for the axis under General. The 'Create symbols' box must be checked. See System Manager manual.
The symbolic names of the individual NC parameters have a fixed specification, and can be found in the NC documentation.
Example
Dim VBVarInteger(0) As Integer
Dim VBVarSingle(0) As Single
Dim VBVarBoolean(0) As Boolean
'wird beim Starten des Programms aufgerufen
Private Sub Form_Load()
'Verbindung zu den Variablen in der SPS herstellen
Call AdsOcx1.AdsWriteVarConnect("MAIN.PLCVarInteger", 2&, 1, 110, VBVarInteger)
Call AdsOcx1.AdsWriteVarConnect("MAIN.PLCVarSingle", 4&, 1, 110, VBVarSingle)
Call AdsOcx1.AdsWriteVarConnect("MAIN.PLCVarBoolean", 2&, 1, 110, VBVarBoolean)
End Sub
'wird beim Beenden des Programms aufgerufen
Private Sub Form_Unload(Cancel As Integer)
'Verbindung zu den Variablen in der SPS beenden
Call AdsOcx1.AdsWriteDisconnect(VBVarInteger)
Call AdsOcx1.AdsWriteDisconnect(VBVarSingle)
Call AdsOcx1.AdsWriteDisconnect(VBVarBoolean)
End Sub
'wird vom Bediener aufgerufen
Private Sub cmd_write_Click()
VBVarInteger(0) = CInt(txt_int.Text)
VBVarSingle(0) = CSng(txt_single.Text)
VBVarBoolean(0) = IIf(chk_boolean.Value = 1, True, False)
End Sub