Up | Example 01 - Calculate Area | Example 02 - Calculate mean value | Example 03 - Manipulate the results from two LineFinders | Example 04 - Dynamic threshold | Example 05 - Auto Exposure | Example 06 - DrawLine | Example 07 - Overlays | Example 08 - Python Objects | Example 09 - Timing | Example 10 - Image Averaging | Example 11 - Image Resampling | Example 12 - Constant Contrast | Example 13 - Serial Communication | Example 14 - Python results | Example 15 - Making a result string | Example 16 - Running tools from a script | Example 17 - Image Manipulation using Python Scripting | Example 18 - Calculating the median angle | Example 19 - Iterating objects located by a blob | Example 20 - Resampling using non-linear calibration | Example 21 - Custom Scorpion Python extension | Example 22 - Accessing Image Pixels | Example 23 - Implementing a tcp/ip Socket Server | Example 24 - Setting ExternalReference from calculated four points | Example 25 - Rotating a reference around in a circle | Example 26 - Grabbing an image from an MOXA Video IP Server | Example 27 - Toolbox Switch | Example 28 - Color Matcher Iteration | Example 29 - Audio Notification | Example 30 - Windows MessageBox | Example 31 -  Client to tcp Socket Server | Example 32 -  Read / Write External Data from / to file | Example 33 - Changing a tool's ROI | Example 34 - Histogram Equalization | Example 35 - Robust Adam 6060 scripts | Example 36 - Bubblesort | Example 37 - Element Statistics | Example 38 - Saving 3D Image | Example 39 - Disabling Zoom in Image Windows | Example 40 - Filtering timeseries | Example 41 - Scorpion Watchdog keep system running | Example 42 - Binary Search | Example 43 - Creating an ordered pointcloud | Example 44 - UDP Socket Communication

 

  

 
Example 23 - Implementing a tcp/ip Socket Server

If you want to implement TCP/IP communication using sockets, you can use Python and the socket API. The socket API is a BSD
implementation and is well documented in the Python help system. Using sockets, you have to decide if your Scorpion application is going
to be the server or the client. This example shows how you can be a server. Example 31 shows a tcp/ip client.

Look in Central.Start for the initialization code which sets up the serversocket. Then look in the script socktrig
for the code that checks if a request is inbound on the serversocket. If so, a clientsocket is used for subsequent operation. If the client socket
is closed, a new incoming connection will be allowed. In this fashion the client can connect and close without any protocol handshakes.

What should the client side code look like? In Python you can use this script:

from socket import *
def Trigger():
    ip='localhost' 
    port=9901
    csock=socket(AF_INET,SOCK_STREAM)
    csock.settimeout(1.0) 
    csock.connect( (ip,port) )
    csock.send('PartPresent')
    print csock.recv(100)
    csock.close()

The example is contained in the example profile SocketServer.zip.  The example requires python 2.3 or higher to be installed.

Example : Socket Server - server side

Central Start

import sys
from socket import *
ip='localhost'
port=9901

ssock=socket(AF_INET,SOCK_STREAM)
ssock.settimeout(0.2)
ssock.bind ( (ip,port) )
ssock.listen(1)
csock=0
connected=0
tries=0

Central Stop

ssock.close()

SockTrig

# called from scheduler

def socktrig():

  import socket

  global csock
  global connected

  if connected:
    # socket is there
    try:
      msg=csock.recv(256)
      if msg=='':
         # socket closed
         print 'Client disconnected'
         csock.close()
         csock=0
         connected=0
      elif msg==GetValue('Trigger.Text'):   # contains PartPresent
        print 'PartPresent'
        ExecuteCmd('GrabExecute','')
      else:
        print 'Unknown command',msg
    except socket.timeout:
      pass                                  #no data in buffer yet, no error condition
    except:
      try:
        csock,addr=ssock.accept()
        csock.settimeout(0.2)
        print 'Client connected',addr
        connected=1
      except:
        print 'Timeout waiting for connection'
  else:
    # accept new connection if not connected
    try: 
      csock,addr=ssock.accept()
      csock.settimeout(0.2)
      print 'Client connected',addr
      connected=1
    except:
      print 'Still waiting for connection'

 

 

Sending response # sends message in external tool

global csock

if connected:csock.send(GetValue('ResponsePass.Text'))
 
 

Scorpion Vision Version XII : Build 646 - Date: 20170225
Scorpion Vision Software® is a registered trademark of Tordivel AS.
Copyright © 2000 - 2017 Tordivel AS.