This section shows an example on simple communication with the Application Server using the Tcl command language. Tcl is a very simple scripting and command language, see the official TCL website for more information. A binary distribution for Windows can be found here: https://www.magicsplat.com/tcl-installer/index.html.


Other scripting languages, like Python, should be able to control the Application Server in similar fashion. Tcl is chosen here for its simplicity to demonstrate control principles as it can establish communication using only a few socket configuration commands.

Setting up Application Server communication in Tcl

First create a socket connected to localhost on port 51234:


% set so [socket localhost 51234]
sock000001F0932F2CB0


Then configure it to line buffering:


% fconfigure $so -buffering line


Finally we can define a new command sendcmd, which will send the command, get the response, and return it. 


% proc sendcmd {so cmd} {puts $so $cmd; gets $so data; return $data}

Controlling Application Server from Tcl

We can now send commands to the Application Server and receive the responses using the sendcmd command:


% sendcmd $so {acq start}
ok;acq start;starting...

% sendcmd $so {acq stop}
ok;acq stop;stopped

Receiving information on object detections

The Application Server provides a second TCP/IP channel where object detections are announced. We can open a second Tcl command shell, create an async socket, and define a callback function that would be executed when any data arrives. In the callback, we print the data sent by the Application Server to the standard output, until the socket is closed.


We can paste the following code into the Tcl command shell:


proc readData {serverChannel} {
       global end
       set noOfChars [gets $serverChannel data]
       if {$noOfChars!=-1} {
               puts "read: $data"
       }
       if {[eof $serverChannel]} {
               close $serverChannel
               set end 1
       }
}


set sd [socket -async localhost 51300]
fconfigure $sd -blocking 0
fileevent $sd readable [list readData $sd]
vwait end


The first part defines the callback function. The second part creates the socket, then configures it as non-blocking, then sets the event callback using fileevent, and finally enters the event loop using the vwait command. This will make the event loop run until a variable end is created, which happens when the callback detects that the remote closed the connection.


The complete application setup is shown in the following screenshot. Apart of the perClass Mira window, we have two separate shells, one Tcl shell to issue commands and one shell receiving object detections. Once we connect to the object detection channel on port 51300 in , the perClass Mira Output window will display the connection details. In the command shell, we can now enable the command detection output using the "acq info enable" (not shown in the screenshot), and then start the acquisition using the "acq start" command. 


As soon as objects are detected in the live stream, the respective messages will also be sent over the object detection socket, which will be read and printed to the standard output.