|
Sub DL_OnReceive() - Evaluating Receive Sequence Data |
|
|
To analyse the Receive Sequence data (e.g. check the actual values received for a wildcard area) or perform additional tasks after receiving the sequence, the following procedure can be defined in a Docklight script:
Sub DL_OnReceive() ... my script code ... End Sub
After detecting a new Receive Sequence and performing the predefined Actions (add comment, send a sequence, ...), the DL_OnReceive() procedure is called by the Docklight script engine. Inside the DL_OnReceive() procedure, the following functions are available to read out the Receive Sequence data:
Remarks
The DL.OnReceive_GetData() method is a good way to analyse the actual data received when you are using ASCII protocols with printing characters only. If you require the HEX or decimal value of individual characters, you may use the DL.OnReceive_Peek( .. ) function as a convenient alternative. See the DL_OnSend() event procedure for a related example.
The DL_OnReceive() procedure is only executed while the script is running. While executing the DL_OnReceive() 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.
DL_OnReceive() procedures are not executed while a Pause or a WaitForSequence method is blocking the program flow. If a Receive Sequence is detected, the DL_OnReceive() call is queued and executed after Pause (or WaitForSequence) returns. See Example 2 below for a workaround to this problem.
See also Timing and Program Flow for some insight on how Docklight handles receive data events and executes the DL_OnReceive() code section.
' Example DL_OnReceive() event code ' ' Predefined Send Sequence ' (0) Send Value: ' VALUE=<?><?><CR><LF> ' ' Predefined Receive Sequence ' (0) Value Received: ' VALUE=<?><?><CR><LF> ' ' Run this test on a COM port with a loopback connector ' (TX connected to RX of the same port).
finished = False DL.ClearCommWindows Do DL.Pause 1 ' (the pause reduces CPU load while idle) Loop Until finished
Sub DL_OnReceive() If DL.OnReceive_GetName() = "Value Received" Then ' Read the value from the receive data myValue = Mid(DL.OnReceive_GetData(), 7, 2) ' Ensure this is a numeric value If IsNumeric(myValue) Then ' increase myValue = myValue + 1 If myValue < 100 Then ' If the value is still below 100, send it out again newValueStr = CStr(myValue) DL.SendSequence "Send Value", newValueStr Else DL.AddComment "VALUE=99, stopping..." finished = True End If End If End If End Sub
After starting the script and manually sending out a "Send Value" sequence with parameter value "95", the Communication Window could look like this:
3/9/2009 18:33:14.210 [TX] - VALUE=95<CR><LF>
3/9/2009 18:33:14.212 [RX] - VALUE=95<CR><LF>
3/9/2009 18:33:14.218 [TX] - VALUE=96<CR><LF>
3/9/2009 18:33:14.220 [RX] - VALUE=96<CR><LF>
3/9/2009 18:33:14.227 [TX] - VALUE=97<CR><LF>
3/9/2009 18:33:14.228 [RX] - VALUE=97<CR><LF>
3/9/2009 18:33:14.236 [TX] - VALUE=98<CR><LF>
3/9/2009 18:33:14.237 [RX] - VALUE=98<CR><LF>
3/9/2009 18:33:14.245 [TX] - VALUE=99<CR><LF>
3/9/2009 18:33:14.246 [RX] - VALUE=99<CR><LF> VALUE=99, stopping...
' Example using DL_OnReceive() in code with Pause statements
' Predefined Send Sequence ' (0) Hello: ' Hello<CR><LF> ' ' Predefined Receive Sequence ' (0) Hello: ' Hello<CR><LF> ' ' Run this test on a COM port with a loopback connector ' (TX connected to RX of the same port).
DL.ClearCommWindows ' Get the communication started started = True DL.SendSequence "Hello" ' Wait for about 1 second, but make sure that the DL_OnReceive() events ' are processed meanwhile pauseWithEvents 1000 ' Stop sending and wait until all data came back properly started = False DL.Pause 20 ' Data throughput? DL.AddComment DL.AddComment "Number of 'Hello' sequences detected: " & DL.GetReceiveCounter("Hello")
Sub DL_OnReceive() ' Send out the same sequence that has just been received If started Then DL.SendSequence DL.OnReceive_GetIndex() End If End Sub
Sub pauseWithEvents(milliseconds) ' Unlike the DL.Pause command, this function allows DL_OnReceive() ' statements to be processed while waiting startTime = Timer While (Timer - startTime) < milliseconds / 1000 ' consider midnight 'jump' / reset of the Timer variable If Timer < (startTime - 1) Then startTime = startTime - 86400 DL.Pause 1 Wend End Sub
After starting the script, Docklight will keep sending and receiving the "Hello" sequence for about 1 second. The total number of sequences sent and received depends on the COM port settings (baud rate) and your PC's performance. The Communication Window could look like this:
3/9/2009 18:35:35.681 [TX] - Hello<CR><LF>
3/9/2009 18:35:35.682 [RX] - Hello<CR><LF>
3/9/2009 18:35:35.689 [TX] - Hello<CR><LF>
3/9/2009 18:35:35.691 [RX] - Hello<CR><LF>
3/9/2009 18:35:35.697 [TX] - Hello<CR><LF>
...
3/9/2009 18:35:36.693 [TX] - Hello<CR><LF>
3/9/2009 18:35:36.694 [RX] - Hello<CR><LF>
3/9/2009 18:35:36.702 [TX] - Hello<CR><LF>
3/9/2009 18:35:36.703 [RX] - Hello<CR><LF>
Number of 'Hello' sequences detected: 122
|