Excel Using Hart
Excel Using Hart
Getting Started
The worksheet which is used is very simple.
http://borst-automation.com
[email protected]
Download: http://borst-automation.com/downloads/UsingHartInExcel.zip
Coding Details
While the module HartTest is containing
the little test program the module
HartInterface contains the necessary
structures and functions declarations. The
following is an example of the declaration
of one of the functions in the DLL.
Public Declare Function BHDrv_DoCommand Lib "BaHartDrv70.dll" _
(ByVal hDrv As Long, _
ByVal byCmd As Byte, _
ByVal byQOS As Byte, _
pstrRequestData As Any, _
ByVal byReqLen As Byte, _
ByVal lAppKey As Long, _
pstrUniqueID As Any _
) As Long
Open Port
For opening the com port the number of the port is taken from
the excel sheet.
'Open Com from Cell E2
'Configuration will be default
iComPort = Range("E2")
hDrv = BHDrv_OpenChannel(iComPort)
Connect
The connection is established to retrieve the unique identifier
from the device and store it into the structure strConnection.
The unique identifier is stored in the application program to
allow also the operation with multiplexer devices.
'Connect to device with address 0
hSrv = BHDrv_ConnectByAddr(hDrv, 0, DRV_WAIT, 2)
If hSrv <> INVALID_SRV_HANDLE _
Then
BHDrv_FetchConnection hSrv, strConnection
Read Date
If the connection was O.K. command 13 is send to get tag,
descriptor and date.
'Read tag descriptor date
hSrv = BHDrv_DoCommand(hDrv, 13, DRV_WAIT, byReqData(0), 0, 0, ByVal strConnection.sUniqueID)
If hSrv <> INVALID_SRV_HANDLE _
Then
BHDrv_FetchConfirmation hSrv, strConfirmation
If strConfirmation.byError = SRV_SUCCESSFUL _
Then
Range("B3") = Format(BHDrv_PickInt8(18, ByVal strConfirmation.sData), "0")
Range("C3") = Format(BHDrv_PickInt8(19, ByVal strConfirmation.sData), "0")
Range("D3") = Format(BHDrv_PickInt8(20, ByVal strConfirmation.sData) + 1900, "0")
Stop
The data from the response frame is stored in cells B3, C3 and
D3 of the worksheet.
Write Date
Then day, month and year are picked from the excel worksheet
and are inserted into the request data byte array.
'Set day
byReqData(18) = Range("B4")
'Set month
byReqData(19) = Range("C4")
'Set Year
byReqData(20) = Range("D4") - 1900
Send Command 18
Finally command 18 is sent.
'Send command 18
hSrv = BHDrv_DoCommand(hDrv, 18, DRV_WAIT, byReqData(0), 21, 0, ByVal strConnection.sUniqueID)