PLCSim from Excel using VBA and S7PROSIMLib

jondownloads

Level-1
Beiträge
9
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
[Excuse me but I don't speak German]

Hi,
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

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

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

STDMETHOD(CS7ProSim::WriteDataBlockValue)(
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.
 
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:
Code:
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
 
Zurück
Oben