beautypg.com

7 programming example and general hints – Despatch Protocol Plus Modbus Communications User Manual

Page 48

background image

Chromalox Instruments and Controls

A-51643 Rev. 6 10/06/03

42

7 Programming Example and General Hints

This section provides an example of Microsoft Visual Basic

TM

code used to communicate with a

Protocol

TM

Plus controller. In addition, some general programming hints are provided.

7.1 Code Example

The following Visual Basic™ code example assumes that an MSComm control named ‗comMain‘ has

been placed on a form named ‗frmComm‘. The form also contains a command button named

‗Command1‘, and a text box named ‗Text1‘. Code is shown below in Bold type.

The example code sends a Modbus message to a Protocol

TM

Plus controller when the command button is

clicked. The text box is cleared when the command button is clicked, and displays ―OK‖ when a good

reply is received from the controller. The received message data byte values are also displayed in the text

box.

The following form declarations are made:

Option Explicit

Dim TxMessage As String

Dim RxMessage As String

TxMessage and RxMessage will be used as message data buffers for sending and receiving, respectively.

The following code procedure is also added to the form to calculate the Modbus CRC value:

Private Function GetCRC(MBuffer As String) As Long

Dim CRCValue As Long

Dim MLength As Long

Dim i As Integer

Dim j As Integer

Dim LsbSet As Boolean

'get message length:

MLength = Len(MBuffer)