|
Sub DL_OnSend() - Send Sequence Data Manipulation |
|
|
To allow additional calculations and algorithms (e.g. checksums) on Send Sequence data, the following procedure can be defined in a Docklight script:
Sub DL_OnSend() ... my script code ... End Sub
Before sending out a new Send Sequence, the DL_OnSend() procedure is called by the Docklight script engine. Inside the DL_OnSend() procedure, the following functions are available to read and manipulate the current sequence data:
Remarks
Using the DL.OnSend_GetSize(), DL.OnSend_Peek(..) and DL.OnSend_Poke functions, checksum calculations and other algorithms can be easily implemented. See the example below.
The DL_OnSend() procedure is only executed while the script is running. While executing the DL_OnSend() code, no further communication processing and display updates are performed. To avoid performance and timing problems, keep the execution time low. Avoid nested loops for example, and do not perform time-consuming calculations.
See Timing and Program Flow for some insight on how Docklight handles send data events and executes the DL_OnSend() code section.
See the DL.CalcChecksum method and the MODBUS protocol example for information about how to calculate more sophisticated checksums, such as CRC values.
' Example DL_OnSend() event code
' Predefined Send Sequences ' (0) Test: Test<CR><LF><NUL>
' Endless loop to prevent the script from terminating immediately Do DL.Pause 1 ' (the pause reduces CPU load while idle) Loop
Sub DL_OnSend() ' Simple checksum: Last byte of sequence ' is a checksum on all previous bytes, mod 256 seqSize = DL.OnSend_GetSize() ' we need at least a two-byte sequence If seqSize > 1 Then chksumHex = DL.CalcChecksum("MOD256", DL.OnSend_GetData("H"), "H", 1, seqSize -1) ' Overwrite the last character of the Send Sequence ' with the actual checksum value DL.OnSend_Poke seqSize, CInt("&h" + chkSumHex)
' Using the Peek function for additional documentation DL.AddComment "Checksum on", False, False For i = 1 To seqSize - 1 DL.AddComment " " & DL.OnSend_Peek(i, "H"), False, False Next DL.AddComment " is " & DL.OnSend_Peek(seqSize, "H") & "(Hex), " & DL.OnSend_Peek(seqSize, "D") & "(Decimal)" End If End Sub
After starting the script and manually sending the "Test" sequence, the ASCII communication window of Docklight could display the following output:
Checksum on 54 65 73 74 0D 0A is B7(Hex), 183(Decimal)
1/15/2008 08:54:50.393 [TX] - Test<CR><LF>
|