Difference between revisions of "Scripting"

From DXLog.net
Jump to navigation Jump to search
(Introduction to C#)
(C# Script Structure)
(3 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
Microsoft have a free fully functional version of Visual Studio called Community, available from https://www.visualstudio.com/en-us/products/visual-studio-community-vs
 
Microsoft have a free fully functional version of Visual Studio called Community, available from https://www.visualstudio.com/en-us/products/visual-studio-community-vs
 +
 +
==C# Script Structure==
 +
 +
Scripts must start with references to the relevant objects<br>
 +
eg.<br>
 +
//INCLUDE_ASSEMBLY System.dll<br>
 +
//INCLUDE_ASSEMBLY System.Windows.Forms.dll<br>
 +
//INCLUDE_ASSEMBLY CWKeyer.dll<br><br>
 +
Each script needs to implement three methods, as shown in the example:<br>
 +
- Initialize<br>
 +
- Deinitialize<br>
 +
- Main<br>
 +
<br>'''Initialize''' is called when the script is compiled and the object created. This is used to hook on events or initialize additional properties etc.
 +
<br><br>'''Deinitialize''' is called when the object is going to be closed, for example when we exit DXLog.net.
 +
<br><br>'''Main''' is called each time the script is called with a defined shortcut or from a macro message.
 +
<br><br>To enable the script to communicate with the main DXLog.net code, references to the object are required so the command can be passed into the methods.<br><br>
 +
They are:<br>
 +
FrmMain - this is a reference to the main form window - from this you can access any other public object in main code and you can access public properties, methods, hook on events etc. This object has access to everything within in the main DXLog.net code.<br><br>
 +
ContestData - this object contains all the properties and methods to handle the currently opened log.<br><br>
 +
COMMain - this is the main communication object which is used to access everything that is configured in the "Configure interfaces" window.
 +
Here you can access radio 1 and 2 directly or any other enabled serial or parallel port object.<br><br>
 +
 +
Important: the bespoke C# code script must be included as source code, DO NOT COMPILE<br>
 +
Script must be added using the scripts manager tool in DXLog.net.<br><br>
  
 
==Example Scripts==
 
==Example Scripts==
  
There are example scripts in the download directory http://dxlog.net/sw/download/script_examples/
+
Example scripts are available in the download directory http://dxlog.net/sw/download/script_examples/
  
 
The following script is an example of a C# script which keeps the Elecraft K3 keyer speed in sync with the CW speed set in DXLog.net<br>
 
The following script is an example of a C# script which keeps the Elecraft K3 keyer speed in sync with the CW speed set in DXLog.net<br>

Revision as of 08:46, 12 April 2015

Introduction to C#

DXLog.net has been written in such a way that bespoke C# scripts can be written by the end user and will be compiled into DXLog.net at runtime.

This makes DXLog.net very versatile and with a little C# knowledge a very powerful contest logging tool.

To learn the basics about C# there are lots of free on-line help like http://www.homeandlearn.co.uk/csharp/csharp.html

Microsoft have a free fully functional version of Visual Studio called Community, available from https://www.visualstudio.com/en-us/products/visual-studio-community-vs

C# Script Structure

Scripts must start with references to the relevant objects
eg.
//INCLUDE_ASSEMBLY System.dll
//INCLUDE_ASSEMBLY System.Windows.Forms.dll
//INCLUDE_ASSEMBLY CWKeyer.dll

Each script needs to implement three methods, as shown in the example:
- Initialize
- Deinitialize
- Main

Initialize is called when the script is compiled and the object created. This is used to hook on events or initialize additional properties etc.

Deinitialize is called when the object is going to be closed, for example when we exit DXLog.net.

Main is called each time the script is called with a defined shortcut or from a macro message.

To enable the script to communicate with the main DXLog.net code, references to the object are required so the command can be passed into the methods.

They are:
FrmMain - this is a reference to the main form window - from this you can access any other public object in main code and you can access public properties, methods, hook on events etc. This object has access to everything within in the main DXLog.net code.

ContestData - this object contains all the properties and methods to handle the currently opened log.

COMMain - this is the main communication object which is used to access everything that is configured in the "Configure interfaces" window. Here you can access radio 1 and 2 directly or any other enabled serial or parallel port object.

Important: the bespoke C# code script must be included as source code, DO NOT COMPILE
Script must be added using the scripts manager tool in DXLog.net.

Example Scripts

Example scripts are available in the download directory http://dxlog.net/sw/download/script_examples/

The following script is an example of a C# script which keeps the Elecraft K3 keyer speed in sync with the CW speed set in DXLog.net

 //INCLUDE_ASSEMBLY System.dll
 //INCLUDE_ASSEMBLY System.Windows.Forms.dll
 //INCLUDE_ASSEMBLY CWKeyer.dll
 
 using System;
 using System.Windows.Forms;
 using CWKeyer;
 
 namespace DXLog.net
 {
     public class Script : ScriptClass
     {
         FrmMain mainForm;
 	    public void Initialize(FrmMain main)
 	    {
             mainForm = main;
             if (mainForm._cwKeyer != null)
                 mainForm._cwKeyer.CWSpeedChange += new CWKey.CWSpeedChangeDelegate(handleCWSpeedChange);
 	    }
 	    public void Deinitialize()
 	      {
               if (mainForm._cwKeyer != null)
                   mainForm._cwKeyer.CWSpeedChange -= handleCWSpeedChange;
   	  }
 
         public void Main(FrmMain main, ContestData cdata, COMMain comMain)
         {
         }
 
         private void handleCWSpeedChange(int radioNumber, int newSpeed)
         {
             CATCommon radioObject = mainForm.COMMainProvider.RadioObject(radioNumber);
             if (radioObject == null)
             {
                 mainForm.SetMainStatusText(String.Format("CAT object for radio {0} isn't available!", radioNumber));
                 return;
             }
             radioObject.SendCustomCommand(String.Format("ks0{0};", newSpeed));
             mainForm.SetMainStatusText(String.Format("Radio {0} CW speed changed to {1} wpm!", radioNumber, newSpeed));
         }
     }
 }