Loading...
The TCP Server in the Configuration dialog allows for connections from local or remote TCP clients. The network adapter (which is tied to an IP address) and port are scriptable, allowing the TCP Server to connect to various devices on the fly. Incoming messages, e.g. feedback or status information can be received via Event Listeners. Messages can be sent using Widget Designer's scripting.
Previously, the Connection Manager offered the only method to add TCP Servers. The node system and some commands still use those connections which allows a more advanced message handling and can also be more useful for streaming information.
To add a TCP Server, open the Devices menu and select "TCP Server > Create TCP Server". This will open the Configuration dialog. Alternatively, you can add a new TCP Server in the Configuration dialog with the "+" button when the dialog is already open.
In the dialog, the left side lists all devices and connections that were already added to the project. The circle in front of the entry symbolizes the current connection status:
The TCP Server is enabled and active, which means that Clients can connect to it. |
|
The TCP Server is enabled but not active as the port could not be initialized on the chosen network adapter, e.g. because it is already used by another application. |
|
The connection or device is disabled, i.e. the "Enable" box is not checked. |
On the right side you see several options:
The Type informs you about the type of device or connection.
The Groups lists the groups to which this device belongs.
The Name is the unique identifier for this object in WD and the general rules for naming objects apply here as well: only letters, numbers and underscores are allowed; the first symbol must be a letter.
It is possible to change the default name to a more descriptive one. When scripting, enter this Name to access available members or use the Project object and device Type instead; examples are shown further down.
The Id offers an alternative way to address the device when scripting.
The "Enable" check box is selected by default and can be used to disable the device or connection internally.
Per default, the network Adapter is set to "Any" and the Port is set to 10001 (or higher). Change the values if needed and click the "Apply" button. You can also use the shortcut [Ctrl + Enter] to do so.
The IP Address displays which IP address is set up for the chosen network adapter.
The Number of Connected Clients displays how many TCP clients are connected to the TCP Server. Their IP addresses are listed below.
Once the settings are applied, the dialog can be closed. The newly created connection will also be added to the Devices menu > TCP Server and can be opened from here or with the Devices menu > Configuration.
As soon as a connection is created, it can be addressed via scripting in order to send or receive messages.
To send messages to all connected TCP clients, create a CustomScript button or anything with a scripting field. Enter the device's identifier name into the script field (per default that is "Tcp_Server1") and Script Assist will offer a list of all TCP Server Members.
To send a message, choose the "Send" member. Literal text should always be enclosed in either single or double quotation marks; otherwise, the text is handled like a variable. The second example combines literal text with the global variable "Now" to add the current date and time.
Tcp_Server1.Send("Test")
Tcp_Server1.Send("This is my time: " + Now)
Displaying incoming messages is also easy:
vstring = Tcp_Server1.LastMessageReceived
Note that this recalls the last message any time the command is executed. It is also possible to execute a script automatically whenever a new message is received. To accomplish this, use an Event Listener as explained below.
As an alternative to explicitly naming the connection, the "Project" object can be used. Choose the connection type and address the connection by entering its ID or name. Script Assist then offers you the same list of members. This is the alternative for the first example from above:
Project.Tcp_Server(1).Send("Test")
Substituting the ID with a dynamic variable allows automation. Actions can be performed on many connections of the same type simultaneously, e.g. by using for-loops:
For i = 1 to 10 {
Project.Tcp_Server(i).Send("Test")
}
The chapter "Project and Context Member" shows more examples with for-loops and if-clauses; of course, normal variables can also be used.
Event Listeners can be added in the Configuration dialog and provide the easiest way to handle incoming messages.
Please see the chapter Event Listener and Group Event Listener for a detailed description of its functionality and TCP Server Events for an overview (with examples and description) of the available events.