The TcpSocket object allows Q-SYS cores to make client TCP/IP connections to devices on the network.
The functions in the v2 TcpSocket communication library allow an Event-Based scheme to simplify scripting. Also, the v2 TCP Socket functionality allows for connection monitoring and automatic reconnect, buffer searches and custom EOL sequences. Asynchronous callbacks are no longer necessary as the Event Handler functionality now takes care of that behind the scenes.
For an example of the how the Event-Handled TcpSocket functions, see “Button to Send Text String to Simple TCP/IP Socket”.
Beginning in Q-SYS Designer 5.3, optional callback functions may be defined instead of using the singular EventHandler. Depending upon the application, defining separate functions for each event may be preferable. The choice to use either a single EventHandler to capture all socket events or separate callback functions is up to the programmer. There is no need to change a functioning script because this new functionality exists and you may choose to never use it. But some programmers may feel that the callback method better matches their logical style.
Name |
Notes |
Description |
---|---|---|
.EventHandler |
Signature of function is 'function(socket, event, err)' |
Function called on any socket event. See definition of 'event' in the table below. |
.ReadTimeout |
Optional; Default is 0 ( disabled ) |
Time, in seconds, to wait for data to be available on socket before raising an Error through the EventHandler. |
.WriteTimeout |
Optional; Default is 0 ( disabled ) |
Time, in seconds, to wait for data write to complete before raising an Error through the EventHandler. |
.ReconnectTimeout |
Optional; Default is 5 seconds |
Time in seconds to wait before attempting to reconnect. 0 disables automatic reconnect. |
.IsConnected |
Read-Only |
Returns true if socket is connected |
.BufferLength |
Read-Only |
Amount of data in buffer, in bytes |
Name |
Arguments |
Description |
---|---|---|
( ) |
Creates a new TcpSocket instance. |
|
( ipAddress/hostname, port ) |
Attempts to connect to the specified ip/host name and port, 'ipAddress/hostname' is string, 'port' is integer |
|
( ) |
Disconnects the socket. |
|
( data ) |
Writes specified data to the socket. Raises error if socket is not connected. |
|
( length ) |
Attempts to read up to 'length' bytes from socket. Data is removed from socket buffer. 'length' is positive integer. |
|
:ReadLine |
( EOL, <custom> ) |
Attempts to read a 'line' from the socket buffer. 'EOL' is defined in the table below. '<custom>' is an optional string only used by EOL.Custom. |
:Search |
( str, [start_pos] ) |
Searches the socket buffer for string 'str' (starting at integer 'start_pos') and returns the index of where 'str' is found. 'start_pos' defaults to 1. |
Name |
Description |
---|---|
Connected |
The socket has connected to the remote host. |
Reconnect |
The socket is attempting to reconnect to the remote host |
Data |
There is new data available in the socket buffer |
Closed |
The socket was closed by the remote host |
Error |
The socket was closed due to error. The error argument to the EventHandler will have more information, which can be displayed if a variable was created to catch the error message. |
Timeout |
A read or write timeout was triggered and the socket was closed. |
Name |
Function Signature |
Description |
---|---|---|
.Connected |
function( tcpTable )' |
Assign a function which is called upon connection to remote host |
.Reconnect |
function( tcpTable )' |
Assign a function which is called when socket is attempting to reconnect to the remote host |
.Data |
function( tcpTable, data )' |
Assign a function which is called when there is new data available in the socket buffer |
.Closed |
function( tcpTable )' |
Assign a function which is called when the socket is closed by the remote host |
.Error |
function( tcpTable, error )' |
Assign a function which is called when the socket is closed due to error. The error argument in the function will contain more information, which can be displayed if a variable was created to catch the error message. |
.Timeout |
function( tcpTable, error)' |
Assign a function which is called when a read or write timeout is triggered and the socket was closed. |
Name |
Description |
---|---|
Any |
The end of line is any sequence of any number of carriage return and/or linefeed characters. This is the appropriate choice if the protocol uses simply a carriage return as the end of message. |
CrLf |
The end of the line is an optional carriage return, followed by a linefeed. (In other words, it is either a "\r\n" or a "\n".) This format is useful in parsing text-based Internet protocols, since the standards generally prescribe a "\r\n" line-terminator, but nonconformant clients sometimes use just "\n". |
CrLfStrict |
The end of a line is a single carriage return, followed by a single linefeed. (This is also known as "\r\n". The ASCII values are 0x0D 0x0A). |
Lf |
The end of a line is a single linefeed character. (This is also known as "\n". It is ASCII value is 0x0A.) |
Null |
The end of line is a single byte with the value 0 — an ASCII NUL. |
Custom |
The end of line is defined by the string passed into the ReadLine() method. |
This script automatically connects to the remote socket server. When a user presses a button, the script sends a text string. Any received text is displayed in the debug window if terminated with a <CR><LF>. The script will automatically attempt to reconnect after 5 seconds if the socket is closed,.
address = "192.168.1.100" port = 1234 sock = TcpSocket.New() sock.ReadTimeout = 0 sock.WriteTimeout = 0 sock.ReconnectTimeout = 5
sendData = 'Hello\x0d\x0a'
sock.EventHandler = function(sock, evt, err) if evt == TcpSocket.Events.Connected then print( "socket connected" ) elseif evt == TcpSocket.Events.Reconnect then print( "socket reconnecting..." ) elseif evt == TcpSocket.Events.Data then print( "socket has data" ) line = sock:ReadLine( TcpSocket.EOL.CrLf ) print( "reading until CrLf got "..line ) elseif evt == TcpSocket.Events.Closed then print( "socket closed by remote" ) elseif evt == TcpSocket.Events.Error then print( "socket closed due to error", err ) elseif evt == TcpSocket.Events.Timeout then print( "socket closed due to timeout" ) else print( "unknown socket event", evt ) --should never happen end
end
Controls.Inputs[1].EventHandler = function() sock:Write(sendData)
end
sock:Connect(address, port) |
address = "192.168.1.100" port = 1234 sock = TcpSocket.New() sock.ReadTimeout = 0 sock.WriteTimeout = 0 sock.ReconnectTimeout = 5
sendData = 'Hello\x0d\x0a'
sock.Connected = function(sock) print("TCP socket is connected") end sock.Reconnect = function(sock) print("TCP socket is reconnecting") end sock.Data = function(sock) print("TCP socket has data:",sock:Read(sock.BufferLength) ) end sock.Closed = function(sock) print("TCP socket was closed by the remote end") end sock.Error = function(sock, err) print("TCP socket had an error:",err) end sock.Timeout = function(sock, err) print("TCP socket timed out",err) end
Controls.Inputs[1].EventHandler = function() sock:Write(sendData) end
sock:Connect(address, port) |
QSC.com | Software and Firmware | Resources | QSC Self Help Portal
© 2009 - 2018 QSC, LLC. All rights reserved. QSC and the QSC logo are trademarks of QSC, LLC in the U.S. Patent and Trademark office and other countries. All other trademarks are the property of their respective owners.
http://patents.qsc.com