function wp_mail() {} {"id":550,"date":"2010-09-02T03:12:00","date_gmt":"2010-09-02T10:12:00","guid":{"rendered":"http:\/\/www.serialporttool.com\/sptblog\/?p=550"},"modified":"2012-05-31T11:55:59","modified_gmt":"2012-05-31T18:55:59","slug":"call-your-own-exe-in-auto-send-in-comm-operator","status":"publish","type":"post","link":"https:\/\/www.serialporttool.com\/sptblog\/?p=550","title":{"rendered":"Auto Run Your Own EXE in Comm Operator"},"content":{"rendered":"
Comm Operator v4.5 supports Exe to process data in \u201cAuto Send\u201d function. You can add you own EXE to Auto Send Rule.<\/p>\n
Download Comm Operator v4.5<\/u><\/a><\/font><\/strong> <\/p>\n Here are the steps to add your own EXE to Auto Send Rule in Comm Operator<\/strong><\/p>\n 1. Click menu [Edit]->[Auto Send] to open the \u201cAuto Send Rule Setting\u201d dialog.<\/p>\n 2. Click \u201cNew\u201d button to show \u201cSelect Rule Type\u201d dialog.<\/p>\n 3. Select \u201cEXE (Console)\u201d and Click \u201cOK\u201d. The \u201cUser Exe Rule\u201d dialog will be shown. <\/p>\n<\/p>\n 4. Select your EXE file. <\/p>\n Tip:<\/p>\n 5. Click OK to finish adding your EXE to Auto Send Rule.<\/p>\n How does it work?<\/strong><\/p>\n Comm Operator process the \u201cAuto Send\u201d 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 \u201cAuto Send\u201d. \u201cMatched\u201d means there is data come back from Exe and need to be sent.<\/p>\n When a user\u2019s 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.<\/p>\n \u201cDataReceived\u201d, raised when data received in Comm Operator in current connection. This data received are followed after the event string in hex string format.<\/p>\n \u201cStart\u201d, raised when the auto send starts.<\/p>\n \u201cPause\u201d, raised when the auto send pauses.<\/p>\n \u201cResume\u201d, raised when the auto send resumes.<\/p>\n \u201cLoop\u201d, raised in every loop of the auto send. <\/p>\n Only \u201cDataReceived\u201d has data, the data are hex strings separated by one space. Like \u201c61 62 63 64 \u201c represents the data \u201cABCD\u201d. The whole package write to STDIN in this case will be<\/p>\n DataReceived,61 62 63 64<\/p>\n For other event types, the package only has only the type name followed with a \u201c,\u201d.<\/p>\n In user\u2019s exe, the exe will read package from stdin, and write the data need sent to stdout. <\/p>\n Here is the code of UserExeSample. It is written in C#. Download it from here.<\/a><\/p>\n using System; namespace UserExeSample \/\/test if it is the "DataReceived" event \/\/ test if it is the "Start" event \/\/ test if it is the "Pause" event \/\/ test if it is the "Resume" event \/\/ test if it is the "Loop" event } } \/\/ Help function, used to process data to Comm Operator \/\/ Help function, used to process data from Comm Operator } See Also<\/strong><\/p>\n Run Perl Script in Auto Send<\/a><\/font><\/strong><\/p>\n Run Python Script in Auto Send<\/a><\/font><\/strong><\/p>\n<\/a> <\/p>\n
<\/a> <\/p>\n
<\/a> <\/p>\n
<\/a> <\/p>\n
\n
\n
using System.Collections.Generic;
using System.Collections;
using System.Text; <\/em><\/p>\n
{
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(‘,’); <\/em><\/p>\n
if (strs[0] == "DataReceived")
{
Console.WriteLine(strs[1]);
} <\/em><\/p>\n
if (strs[0] == "Start")
{
Console.WriteLine(ascii_to_hex("Start"));
} <\/em><\/p>\n
if (strs[0] == "Resume")
{
Console.WriteLine(ascii_to_hex("Resume"));
} <\/em><\/p>\n
if (strs[0] == "Pause")
{
Console.WriteLine(ascii_to_hex("Pause"));
} <\/em><\/p>\n
if (strs[0] == "Loop")
{
\/\/System.Threading.Thread.Sleep(1000);
\/\/Console.WriteLine(ascii_to_hex("Loop"));
} <\/em><\/p>\n
catch
{ <\/em><\/p>\n
} <\/em><\/p>\n
\/\/ 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();
} <\/em><\/p>\n
\/\/ 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); <\/em><\/p>\n
}
}<\/em><\/p>\n<\/blockquote>\n\n