Zuviel Werbung? - > Hier kostenlos beim SPS-Forum registrieren

Results 1 to 2 of 2

Thread: PLCSim from Excel using VBA and S7PROSIMLib

  1. #1
    Join Date
    Erhielt 0 Danke für 0 Beiträge


    Zuviel Werbung?
    -> Hier kostenlos registrieren
    [Excuse me but I don't speak German]

    I'm trying to access PLCSim from Excel using VBA and S7PROSIMLib.

    I want perform two operations:

    1) Read operation: takes a DB data address from a cell an retrieves it's current value placing it in another cell.

    2) Write operation: takes a DB data address from a cell and a user input value from another cell and uses them to force the value into de PLCSim.

    Read works OK but I can't figure out how to make Write work properly. I'm using the code below:

    Private WithEvents S7ProSim As S7PROSIMLib.S7ProSim

    Public Sub WriteToPLC(BlockNumber As Long, ByteIndex As Long, BitIndex As Long, Data As Variant)
    Set S7ProSim = New S7PROSIMLib.S7ProSim

    Call S7ProSim.WriteDataBlockValue(BlockNumber, ByteIndex, BitIndex, Data)
    End Sub

    I get a "PS_E_BADTYPE" error. The problem is related to the data type required by S7PROSIM COM object (const VARIANT*):

    long BlockNumber,
    long ByteIndex,
    long BitIndex,
    const VARIANT* pData)

    I get it to work if I always force the same value (e.g. using Const CData0 As Byte = 0). But I'm seeking the end user to be able to force values at will just by typing them in a cell (a range).

    Could anyone tell me which data type or data conversion should I use?
    Thanks, Jon.
    Reply With Quote Reply With Quote PLCSim from Excel using VBA and S7PROSIMLib  

  2. #2
    Join Date
    Erhielt 1,439 Danke für 1,064 Beiträge


    Hi Jon,
    you have to set the type-field of a variable of type variant to the correct size.

    Maybe there are ways in VBA to set the variant type-field directly, but the following 'trick' should work too:
    Dim bVal As Variant
    Dim wVal As Variant
    Dim dwVal As Variant
    Dim b As Byte
    Dim w As Integer
    Dim dw As Long
    b = 43
    w = 32767
    dw = 12345678
    bVal = b ' bVal is now of type Byte (1 Byte)
    wVal = w ' wVal is now of type Integer (2 Bytes)
    dwVal = dw ' dwVal is now of type long (4 Bytes)
    Call S7ProSim.WriteDataBlockValue(1, 0, 0, bVal) ' Write a byte to DB1.DBB0
    Call S7ProSim.WriteDataBlockValue(1, 2, 0, wVal) ' Write a word to DB1.DBW2
    Call S7ProSim.WriteDataBlockValue(1, 4, 0, dwVal) ' Write a dword to DB1.DBD4

  3. Folgender Benutzer sagt Danke zu Thomas_v2.1 für den nützlichen Beitrag:

    jondownloads (08.07.2011)

Similar Threads

  1. WIN AC MP4.1 und PLCSIM
    By Blackmike in forum HMI
    Replies: 1
    Last Post: 23.05.2009, 10:54
  2. Plcsim 5.4
    By jabba in forum Simatic
    Replies: 6
    Last Post: 08.02.2008, 21:24
  3. Plcsim
    By magmaa in forum Simatic
    Replies: 4
    Last Post: 28.10.2007, 13:18
  4. Plcsim Ton
    By kpeter in forum Simatic
    Replies: 5
    Last Post: 26.02.2007, 13:51
  5. S7-plcsim
    By snuggles in forum Simatic
    Replies: 6
    Last Post: 30.11.2006, 09:37


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts