Auto Run Your Own EXE in Comm Operator

Comm Operator v4.5 supports Exe to process data in “Auto Send” function. You can add you own EXE to Auto Send Rule.

Download Comm Operator v4.5 

Comm Operator Main Interface

Here are the steps to add your own EXE to Auto Send Rule in Comm Operator

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

Auto Send Rule Setting Dialog

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

Select Rule Type - EXE

3. Select “EXE (Console)” and Click “OK”. The “User Exe Rule” dialog will be shown.

User Exe Rule Dialog

4. Select your EXE file.

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 User Exe called “UserExeSample.exe”. 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.

5. Click OK to finish adding your EXE to Auto Send Rule.

How does it work?

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

When a user’s exe 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 “,”.

In user’s exe, the exe will read package from stdin, and write the data need sent to stdout.

Here is the code of UserExeSample. It is written in C#. Download it from here.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace UserExeSample
{
    class Program
    {
        /// <summary>
        ///  The user’s exe will be called in every auto send loop in Comm Operator
        ///  Comm Operator will write data to stdio. User’s exe read those data. Comm Operator will read the retun data from STDOUT and send those data
        /// 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
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                //  Read data send from Comm Operator
                string str = Console.ReadLine();
                // Parse data, the data contains two parts. The first part is event type, the seond part are data in hex string
                string[] strs = str.Split(‘,’);

                //test if it is the "DataReceived" event
                if (strs[0] == "DataReceived")
                {
                    Console.WriteLine(strs[1]);
                }

                // test if it is the "Start" event
                if (strs[0] == "Start")
                {
                    Console.WriteLine(ascii_to_hex("Start"));
                }

                // test if it is the "Pause" event
                if (strs[0] == "Resume")
                {
                    Console.WriteLine(ascii_to_hex("Resume"));
                }

                //  test if it is the "Resume" event
                if (strs[0] == "Pause")
                {
                    Console.WriteLine(ascii_to_hex("Pause"));
                }

                //  test if it is the "Loop" event
                if (strs[0] == "Loop")
                {
                    //System.Threading.Thread.Sleep(1000);
                    //Console.WriteLine(ascii_to_hex("Loop"));
                }

            }
            catch
            {

            }
        }

        // Help function, used to process data to Comm Operator
        // convert ascii string to hex string
        // for example "ABCD" –> "41 42 43 44 "
        static string ascii_to_hex(string str)
        {
            StringBuilder sb = new StringBuilder();
            Encoding coding = Encoding.GetEncoding("iso-8859-1");
            byte[] data = coding.GetBytes(str);
            foreach (byte b in data)
            {
                sb.AppendFormat("{0:X2} ", b);
            }
            return sb.ToString();
        }

        // Help function, used to process data from Comm Operator
        // convert hex string t ascii string
        // for example
        // "41 42 43 44 " –> "ABCD"
        static string hex_to_ascii(string str)
        {
            ArrayList list = new ArrayList();
            string[] strs = str.Split(new char[] { ‘ ‘ }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in strs)
            {
                list.Add(Convert.ToByte(s, 16));
            }
            Encoding coding = Encoding.GetEncoding("iso-8859-1");
            byte[] data = (byte[])list.ToArray(typeof(byte));
            return coding.GetString(data, 0, data.Length);

        }
    }
}

See Also

Run Perl Script in Auto Send

Run Python Script in Auto Send

Run Ruby Script in Auto Send

Run Plugin(.net Dll) in Auto Send