AdsSyncRead[Datatype]VarReq

AdsSyncReadBoolVarReq

AdsSyncReadIntegerVarReq

AdsSyncReadLongVarReq

AdsSyncReadSingleVarReq

AdsSyncReadDoubleVarReq

AdsSyncReadStringVarReq

Reads data synchronously from an ADS device, and writes it into a Visual Basic variable of type boolean, integer, long, single, double or string.

object.AdsSyncRead[Datatype]VarReq(
  hVar As Long,
  cbLength As Long,
  pData As [Datatype]
) As Long

Parameter

hVar

[in] Handle of the ADS variable (see the AdsCreateVarHandle() method)

cbLength

[in] Length of the data in bytes (see VB variable lengths)

pData

[in] Visual Basic variable into which the data is written from the ADS variable

Return value

See ADS error codes

Attention: In case of an error the VB variable (pData), whose value should be written, is set to "0".

Comments

The execution of the Visual Basic program is stopped until the data from the ADS device is available or until the time in the property AdsAmsCommTimeout is exceeded.
Note on the String data type: When specifying the length of the data, note that it refers to the length of the variable in the Visual Basic program. Since Visual Basic represents a character with 2 bytes, the length of the variable must be determined with LenB(), not with Len().

VB sample

Dim hVar As Long
Dim VBVar As Single
'Handle der SPS-Variable holen
Call AdsOcx1.AdsCreateVarHandle("MAIN.PLCVar", hVar)
'Variable auslesen
Call AdsOcx1.AdsSyncReadSingleVarReq(hVar, 4&, VBVar)
'Variablen anzeigen
Label1.Caption = VBVar
'Handle wieder freigeben
Call AdsOcx1.AdsDeleteVarHandle(hVar)
Dim hVar As Long
Dim VBVar As String
'Handle der SPS-Variable holen
Call AdsOcx1.AdsCreateVarHandle("MAIN.PLCVar", hVar)
'Visual Basic initialisieren
VBVar = Space(10)
'Variable auslesen
Call AdsOcx1.AdsSyncReadStringVarReq(hVar, LenB(VBVar), VBVar)
'Variablen anzeigen
Label1.Caption = VBVar
'Handle wieder freigeben
Call AdsOcx1.AdsDeleteVarHandle(hVar)

Delphi sample

procedure TForm1.Button1Click(Sender: TObject);
var     res1, res2, res3 :integer;
    //handles
    hBoolean, hSmallint, hLongint, hSingle, hDouble, hString : integer;
    //read buffer
    vWordBool : WordBool;
    vSmallint : Smallint;
    vLongint : Longint;
    vSingle : Single;
    vDouble : Double;
    vString : WideString;
begin
    res1 := AdsOcx1.AdsCreateVarHandle( 'MAIN.vBOOL', hBoolean );
    res2 :=  AdsOcx1.AdsSyncReadBoolVarReq( hBoolean, sizeof(vWordBool), vWordBool );
    res3 := AdsOcx1.AdsDeleteVarHandle( hBoolean );
    Label1.Caption := Format('res1: %d, res2: %d, res3: %d,  Value: %s', [res1, res2, res3, BoolToStr(vWordBool, TRUE)]);

    res1 := AdsOcx1.AdsCreateVarHandle( 'MAIN.vINT', hSmallint );
    res2 :=  AdsOcx1.AdsSyncReadIntegerVarReq( hSmallint, sizeof(vSmallint), vSmallint );
    res3 := AdsOcx1.AdsDeleteVarHandle( hSmallint );
    Label2.Caption := Format('res1: %d, res2: %d, res3: %d,  Value: %d', [res1, res2, res3, vSmallint]);

    res1 := AdsOcx1.AdsCreateVarHandle( 'MAIN.vDINT', hLongint );
    res2 :=  AdsOcx1.AdsSyncReadLongVarReq( hLongint, sizeof(vLongint), vLongint );
    res3 := AdsOcx1.AdsDeleteVarHandle( hLongint );
    Label3.Caption := Format('res1: %d, res2: %d, res3: %d,  Value: %d', [res1, res2, res3, vLongint]);

    res1 := AdsOcx1.AdsCreateVarHandle( 'MAIN.vREAL', hSingle );
    res2 :=  AdsOcx1.AdsSyncReadSingleVarReq( hSingle, sizeof(vSingle), vSingle );
    res3 := AdsOcx1.AdsDeleteVarHandle( hSingle );
    Label4.Caption := Format('res1: %d, res2: %d, res3: %d,  Value: %f', [res1, res2, res3, vSingle]);

    res1 := AdsOcx1.AdsCreateVarHandle( 'MAIN.vLREAL', hDouble );
    res2 :=  AdsOcx1.AdsSyncReadDoubleVarReq( hDouble, sizeof(vDouble), vDouble );
    res3 := AdsOcx1.AdsDeleteVarHandle( hDouble );
    Label5.Caption := Format('res1: %d, res2: %d, res3: %d,  Value: %f', [res1, res2, res3, vDouble]);

    res1 := AdsOcx1.AdsCreateVarHandle( 'MAIN.vSTRING', hString );
    SetLength(vString,80{standard length of the PLC string variable});
    res2 :=  AdsOcx1.AdsSyncReadStringVarReq( hString, Length(vString)*2{byte length!}, vString );
    res3 := AdsOcx1.AdsDeleteVarHandle( hString );
    Label6.Caption := Format('res1: %d, res2: %d, res3: %d,  Length: %d, Value: %s', [res1, res2, res3, Length(vString), vString]);
end;