beautypg.com

Despatch Protocol Plus Modbus Communications User Manual

Page 49

background image

Chromalox Instruments and Controls

A-51643 Rev. 6 10/06/03

43

'initialize the CRC register:

CRCValue = 65535

For i = 1 To MLength

'this routine is called for each character of a message

CRCValue = CRCValue Xor CInt(Asc(Mid(MBuffer, i, 1)))

'for each bit in the new character,

'do this loop:

For j = 1 To 8

'determine if LSB is now a 1:

LsbSet = ((CRCValue And 1) = 1)

'shift the crc (buffer) one bit to the right:

CRCValue = Int(CRCValue / 2)

'if LSB was a 1,

If LsbSet Then

'XOR buffer value with the CRC polynomial:

CRCValue = CRCValue Xor 40961 '&HA001

End If

Next j

Next i

GetCRC = CRCValue

End Function

The following code, called in the form‘s Load procedure, is used to initialize the COM port:

Private Sub Form_Load()

'make sure the port is closed:

If frmComm.comMain.PortOpen Then frmComm.comMain.PortOpen = False

'select desired COM port:

frmComm.comMain.CommPort = 1 '1=COM1, etc.