Run Python Script Automatically in Comm Operator

Comm Operator v4.5 supports Python Script in “Auto Send” function. Python Script can be run automatically.

Download Comm Operator v4.5 

Comm Operator Screenshot

Here are the steps to add Python script to Auto Send Rule in Comm Operator.

1. Valid Python interpreter has to be installed. It can be downloaded from http://www.python.org/

2. Click menu [Tools]->[Options]->[Script] page to set the path of  Python script interpreter “python.exe”.

Options - Script

3. Click menu [Edit]->[Auto Send] to open the “Auto Send Rule Setting” dialog.

Auto Send Rule Setting

4. Click “New” button to show “Select Rule Type” dialog.

Select Rule Type - Python

5. Select “Python Script” and Click “OK”. The “Python Script Rule” dialog will be popup.

Python Script Rule Dialog 

6. Select the path of your Python script. Click “View” button will open the script with Windows Notepad.

Tip:

  • If the checkbox”Always” is selected, the script will be run again and again. Otherwise, it won’t be called once it is matched.
  • Comm Operator comes with a demo python script called “Sample.py”. The file can be find in Comm Operator’s install folder. The default folder is “C:\Program Files\Serial Port Tool\Comm Operator\” or “C:\Program Files(x86)\Serial Port Tool\Comm Operator\” for 64 bits OS.

7. Click OK to finish adding Python Script Rule.

How does it work?

Comm Operator process the “Auto Send” in a separate thread. The script will be called in every loop. The script handles the events and determine if there are data need to be sent in “Auto Send”. “Matched” means there is data come back from script and need to be sent.

When a script is called, Comm Operator write the event type as well as data of this event in STDIN. The event type could be one of the following.

“DataReceived”,  raised when data received in Comm Operator in current connection. This data received are followed after the event string in hex string format.

“Start”,  raised when the auto send starts.

“Pause”,  raised when the auto send pauses.

“Resume”,  raised when the auto send resumes.

“Loop”, raised in every loop of the auto send.

Only “DataReceived” has data, the data are hex strings separated by one space. Like “61 62 63 64 “ represents the data “ABCD”. The whole package write to STDIN in this case will be

DataReceived,61 62 63 64

For other event types, the package only has only the type name followed with a “,”.

The package ends with a CR. In python script, the following code will read the whole data package.

input=sys.stdin.readline()

As  the event and data are separated by “,”, it can be parsed easily by the following code.

data = input.split(",")

data[0] is the event type and data[1] contains the data related.

The data send back to Comm Operator is written to STDOUT, the data are formatted in hex string.

Here is the sample code that echo all data received.

if data[0] == "DataReceived" :
  print data[1]   

Here is the help function for creating hex string from ascii string.

##help function
##Convert each ASCII character to a two-digit hex number seperated with a space char
## like "ABCD" to "65 66 67 68 "
def ascii_to_hex(str):
  return ”.join([‘{0:2x} ‘.format(ord(s)) for s in str])

Here is the sample code to send string “Hello” to Comm Operator.

print ascii_to_hex("Hello")

Here is the help function for converting hex string to ascii string. It is useful to parse the data comes from Comm Operator.

##help function
##Convert each two-digit hex number back to an ASCII character.
## the string format like "A5 B3 ", there is a space char between two hex numbers
## like "65 66 67 68 " to "ABCD"
def hex_to_ascii(str):
  return ”.join([chr(int(s, 16)) for s in str])

Here is the code of “Sample.py”.

## To make this script run, please setup the Ruby path correctly in "Tools->Options"
## sample python script to Comm Operator
## Comm Operator will write data to STDIN
## Format
## Type, hex hex hex …
## Type: String that represent the type of event, can be 
##       "DataReceived"
##       "Start"
##       "Pause"        
##       "Resume"
## If the type is "DataReceived", it will follow byte "," and then the data in hex string.
## For example, if the Comm Operator recieved string "abcd", it will call script with data like below
##   DataReceived, 61 62 63 64

import sys
import time

##help function
##Convert each ASCII character to a two-digit hex number seperated with a space char
## like "ABCD" to "65 66 67 68 "
def ascii_to_hex(str):
  return ”.join([‘{0:2x} ‘.format(ord(s)) for s in str])

##help function
##Convert each two-digit hex number back to an ASCII character.
## the string format like "A5 B3 ", there is a space char between two hex numbers
## like "65 66 67 68 " to "ABCD"
def hex_to_ascii(str):
  return ”.join([chr(int(s, 16)) for s in str])

## Read data send from Comm Operator
input=sys.stdin.readline()

## Parse data, the data contains two parts. The first part is event type, the seond part are data in hex string
data = input.split(",")

## test if it is the "DataReceived" event
if data[0] == "DataReceived" :
  ## echo back the data received, replace it with you own code
  print data[1]       

## test if it is the "Start" event
if data[0] == "Start" :
  ## send string "Start", replace it with you own code
  print ascii_to_hex("Start")

## test if it is the "Pause" event
if data[0] == "Pause" :
  ## send string "Pause", replace it with you own code
  print ascii_to_hex("Pause")

## test if it is the "Resume" event
if data[0] == "Resume":
  ## send string "Resume", replace it with you own code
  print ascii_to_hex("Resume")

## test if it is the "Loop" event
#if data[0] == "Loop":
  ## process data here is not recommended.
  #time.sleep( 1)
  #print ascii_to_hex("Loop")

Run Perl Script in Auto Send

Run Plugin(.net Dll) in Auto Send

Run Ruby Script in Auto Send

Run User Exe in Auto Send