Using the HartDLL in Visual Basic

 

Version 7.0.0, April 30th, 2010

Interfacing to the DLL

A module is provided which contains the definition of the most important constants, required structures and the functions exported by the DLL. The module is named BaHartDrv70_Iface.vb and located in the directory CommonVb. However, there is a detailed description of an example implementation in the manual of HartDLL 7.0. The following is only showing some of the coding details in a general way.

 

Declaration of Required Structures and Initialization of the Data Byte Arrays

You may use the types predefined in the interface class.

Private strConfiguration As HartDLL_Iface.T_strConfiguration

Private strConnection As HartDLL_Iface.T_strConnection

Private strConfirmation As HartDLL_Iface.T_strConfirmation

Private strDataBuffer As HartDLL_Iface.T_strDataBuffer

 

In the Form Load Event of the main form the arrays should be initialized.

'Allocate memory for unmanaged arrays

ReDim strConnection.abyUniqueID(5) 'Size must match SizeConst!

ReDim strConfirmation.aby_Data(64) 'Size must match SizeConst!

ReDim strDataBuffer.aby_Data(64)   'Size must match SizeConst!

 

Opening and Configuring the Driver

If the driver is successfully opened it returns a valid handle and may be configured.

hChannel = HartDLL_Iface.BHDrv_OpenChannel(iComPort)

If hChannel <> HartDLL_Iface.INVALID_DRV_HANDLE Then

  'Set the desired configuration

  HartDLL_Iface.BHDrv_GetConfiguration(hChannel, strConfiguration)

  strConfiguration.byNumRetries = 0

  strConfiguration.byRetryIfBusy = 1

  HartDLL_Iface.BHDrv_SetConfiguration(hChannel, strConfiguration)

End If

 

Closing the Driver

In the Form Closing Event the driver should be closed if it was opened.

If hChannel <> HartDLL_Iface.INVALID_DRV_HANDLE Then

  HartDLL_Iface.BHDrv_CloseChannel(hChannel)

End If

 

Establishing a Connection

For executing productive services (commands) in HART a unique identifier for the device is needed. This unique identifier is read by the command 0. In the driver this is supported by the function ConnectByAddr().

SetNotConnected()

hService = HartDLL_Iface.BHDrv_ConnectByAddr(              hChannel, _

                                                                  0, _

                                             HartDLL_Iface.DRV_WAIT, _

                                                                  0)

If hService <> HartDLL_Iface.INVALID_SRV_HANDLE Then

  HartDLL_Iface.BHDrv_FetchConnection(hService, strConnection)

  byLastError = strConnection.byError

  If byLastError = HartDLL_Iface.SRV_SUCCESSFUL Then

    SetConnected()

  End If

End If

 

The function FetchConnection is used to get the required data (unique id and others) from the  driver.

 

Reading Packed ASCII Texts

PackedASCII is a format in the HART protocol encoding a text using 6 bits for each character. However the driver converts from 8 to 6 bit representation. Because .NET is usually based on unicode for texts the component StringBuilder is used to convert into 8 bit ASCII strings. Hart Command 13 is used to read the Tag Name.

hService = HartDLL_Iface.BHDrv_DoCommand(              hChannel, _

                                                             13, _

                                         HartDLL_Iface.DRV_WAIT, _

                                      strDataBuffer.aby_Data(0), _

                                                              0, _

                                                           1234, _

                                   strConnection.abyUniqueID(0))

If hService <> HartDLL_Iface.INVALID_SRV_HANDLE Then

  HartDLL_Iface.BHDrv_FetchConfirmation(hService, strConfirmation)

  byLastError = strConfirmation.byError

  If byLastError = HartDLL_Iface.SRV_SUCCESSFUL Then

    'Get the tag name

    HartDLL_Iface.BHDrv_PickPackedASCII(sb, 8, 0, strConfirmation.aby_Data(0))

    txtTagName.Text = sb.ToString()

  Else

    txtTagName.Text = "-/-"

  End If

End If

 

The function FetchConfirmation retrieves the data returned by the device in the reponse. By PickPackedASCII the string is decoded from the response buffer and returned into sb.The object sb is an instance of the StringBuilder. To use the StringBuilder you have to import the following namespace.

Imports System.Text

 

Reading a Float Value

Command 1 is used to read the measured value from the device. For decoding the response PickFloat is the right method.

hService = HartDLL_Iface.BHDrv_DoCommand(                    hChannel, _

                                                                    1, _

                                               HartDLL_Iface.DRV_WAIT, _

                                            strDataBuffer.aby_Data(0), _

                                                                    0, _

                                                                 1234, _

                                         strConnection.abyUniqueID(0))

If hService <> HartDLL_Iface.INVALID_SRV_HANDLE Then

  HartDLL_Iface.BHDrv_FetchConfirmation(hService, strConfirmation)

  byLastError = strConfirmation.byError

  If byLastError = HartDLL_Iface.SRV_SUCCESSFUL Then

    'Decode the PV

     txtPV.Text = HartDLL_Iface.BHDrv_PickFloat _

       (                           1, _

         strConfirmation.aby_Data(0), _

             HartDLL_Iface.MSB_FIRST  _

       ).ToString("###0.0###")

  Else

    txtPV.Text = "-/-"

  End If

End If

 

Writing PackedASCII Texts

Command 18 is used to write the Tag Name. Again StringBuilder is used as a transfer object for the encoding function PutPackedASCII.

'Insert the new tag name

sb.Insert(0, txtNewTagName.Text.ToUpper())

If txtTagName.Text.Length < 8 Then

  sb.Append(" ", 8 - txtTagName.Text.Length)

End If

'Copy ConfirmationBuffer to DataBuffer

'This would be not necessary here but should show how to cope with the raw data

strDataBuffer.aby_Data = strConfirmation.aby_Data 'Copy the byte array

'Store Tag Name to the buffer

HartDLL_Iface.BHDrv_PutPackedASCII(sb, 8, 0, strDataBuffer.aby_Data(0))

'Write Tag Descriptor Date

hService = HartDLL_Iface.BHDrv_DoCommand(                    hChannel, _

                                                                   18, _

                                               HartDLL_Iface.DRV_WAIT, _

                                            strDataBuffer.aby_Data(0), _

                                                                   21, _

                                                                 4321, _

                                         strConnection.abyUniqueID(0))

 

Contact

Borst Automation 

 

Im Wingert 4
DE-65626 Fachingen

GERMANY

Voice: +49 (0)6432 989176

Fax: +49 (0)6432 989129

Home: http://borst-automation.com

Email: info@borst-automation.de