Run Perl Script Automatically in Comm Operator

Comm Operator v4.5 supports Perl script in “Auto Send” function. Per Script will be run automatically.

Download Comm Operator v4.5 

Comm Operator Screenshot

Here are the steps to add Perl Script to Auto Send Rule in Comm Operator.

1. Valid Perl interpreter need to be installed. It can be downloaded from http://www.perl.org

2. Click menu [Tools]->[Options] to open Options dialog. The path of Perl interpreter “Perl.exe”  has to be setup correctly on Script page.

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 - Perl

5. Select “Perl Script” and Click “OK”. The “Perl Script Rule” dialog will be shown.

Per Script Rule Dialog

6. Choose your Perl 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 Perl script called “Sample.pl”. 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 Perl 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. The 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 perl script, the following code will read the whole data package.

$input=<STDIN>

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

@data = split(/,/, $input);

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] eq "DataReceived"){ 
    print $data[1];
}

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

##Convert each ASCII character to a two-digit hex number separated with a space char
## like "A5 B3 "
sub ascii_to_hex ($)
{
    (my $str = shift) =~ s/(.|\n)/sprintf("%2lx ", ord $1)/eg;
    return $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.

##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
sub hex_to_ascii ($)
{
    (my $str = shift) =~ s/([a-fA-F0-9 ]{3})/chr(hex $1)/eg;
    return $str;
}

Here is the code of “Sample.pl”.

## To make this script run, please setup the Perl path correctly in "Tools->Options"
## sample ruby 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

## Read data send from Comm Operator
$input=<STDIN>;

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

## test if it is the "DataReceived" event
if(@data[0] eq "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] eq  "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] eq  "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] eq  "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] eq "Loop") {
  ## process data here is not recommended.
  #sleep 1
  #print ascii_to_hex("Loop")
}

##Convert each ASCII character to a two-digit hex number separated with a space char
## like "A5 B3 "
sub ascii_to_hex ($)
{
    (my $str = shift) =~ s/(.|\n)/sprintf("%2lx ", ord $1)/eg;
    return $str;
}

##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
sub hex_to_ascii ($)
{
    (my $str = shift) =~ s/([a-fA-F0-9 ]{3})/chr(hex $1)/eg;
    return $str;
}

See Also

Run User Exe in Auto Send

Run Python Script in Auto Send

Run Ruby Script in Auto Send

Run Plugin(.net Dll) in Auto Send

This entry was posted in Comm Operator, News and tagged , . Bookmark the permalink.