Use the Communication components to enable communication with other computers.
This section contains the following topics:
Com Port Component
The TTWComPort component is used to send and receive data through a com port.
Notable Properties
Baud: Sets the baud rate, for example at 19200.
ComNumber: Sets the com port number.
Script Example
The TTWComPort has the following methods for sending data:
.PutString("Hello"): sends the string "Hello"
.PutChar(Asc("A")): sends the character "A"
To receive data, data triggers must be registered on the TTWComPort. The data trigger will be fired whenever the incoming data matches a given trigger.
Tip: A protocol message can start with "START" and end with "END". Register "START" and "END" as triggers so that the user is notified whenever the incoming data matches either "START" or "END".
To register a data trigger, call the AddDataTrigger function of the TTWComPort using the method below:
AddDataTrigger(trigger_text, case_sensitive): The trigger_text is the text to match and case_sensitive is a boolean value. The result value of the function is a trigger handle that should be stored. In this example, it is named as follows below:
SOHDataTrigger = TWComPort1.AddDataTrigger(SOHText, false) EOHDataTrigger = TWComPort1.AddDataTrigger(EOHText, false)OnTriggerData(CP, TriggerHandle): Is called whenever the incoming data matches one of the triggers. The TriggerHandle contains the handle of the trigger that matched the trigger data. It can be tested like this:if TriggerHandle = SOHDataTrigger then msgbox "SOH received" end if if TriggerHandle = EOHDataTrigger then msgbox "EOH received" end ifThe OnTriggerData only tests that the incoming data matches one of the registered triggers. To receive the actual data, use the event called OnTriggerAvail. This event is called whenever there is new incoming data. The event looks like this:
OnTriggerAvail(CP, Count), where count is the number of new characters received.
Implement the following function to gather all the incoming data:
Sub TWComPort1TriggerAvail(CP, Count) \q---- Data is available, read it into the sMessage buffer---- for i=0 to Count-1 sMessage = sMessage & Chr(TWComPort1.GetChar) next End subGeneric Com Port Component
TTWGenericComPort is a specialized version of the TTWComPort and has a predefined start and end of message characters, 0x01 and 0x04 respectively, and also a start of transmission character 0x02.
Events
It also has an event called OnDataAvailable that is triggered whenever a new message is received. The event looks like this:
OnDataAvailable(Sender, MessageHeader, StringList)
Sender: Is the TTWGenericComPort component
MessageHeader: Is the data between 0x01 and 0x02.
StringList: Is a TstringList with the data between 0x02 and 0x04. Each field in the data is separated with a pipe character ’|’.
A message looking like this:
0x01 'My header' 'Field1|Field2|Field3' 0x04causes the OnDataAvailable event to be triggered and the MessageHeader would contain \qMy header'; the StringList would contain the strings:
Field1 Field2 Field3The StringList items can be accessed like this:
for i:=0 to StringList.Count-1 msgbox StringList.Strings(i) nextClient Socket Component
The TClientSocket component is used to connect to a TCP/IP server. It manages the connection when it is open, and terminates the connection when the template is through.
Notable Properties
Host: Host name or IP address of the server to connect to.
Port: Port used to connect to the server.
Active: Indicates whether the socket connection is open and available for communication with other machines.
Basic Methods
Open: Call Open to initiate the socket connection. Open sets the Active property to True.
Close: Call Close to shut down the socket connection. Close sets the Active property to False.
Socket.SendText(<Text>): Use Socket.SendText to write a string to the socket connection.
Script Example
The example below shows how to use the TClientSocket component to send a few commands to Viz Engine.
Trying to connect to the host specified:
Sub cbConnectClick(Sender) ClientSocket1.Active = False ClientSocket1.Host = txtHost.Text ClientSocket1.Active = True End subA valid connection is established. Enable the send button:
Sub ClientSocket1Connect(Sender, Socket) cbSend.Enabled = True End sub