Cross Point


Event mechanism example, scanning readers asynchronously

This JScript examples opens and connects to COM port and starts scanning this network asynchronously. During this scan process events are fired when a specific address is scanned and when a reader is found. When a reader is found, this example displays the name of the found reader in a messagebox. Scanning asynchronously is a more elegant solution than scanning synchronously, because progress information can be shown to the user during asynchronous scanning. Since this example displays all its information in messageboxes, it is not useful to show progress information (the user has to click the 'Ok' button for all progress messages).

To execute this sample, copy it to a empty file and name the file "Example4.js". Now execute the sample by simply double-clicking on it in Windows explorer. When you have a reader attached on the correct network, running on the correct baudrate and with the correct password, you will see a messagebox showing the name of every found reader.

// Variables
var iComPort = 1; // Modify this value if your network is located on another COM port
var iBaudRate = 19200; // Modify this value if your readers operate at a different baudrate
var iInstallerPassword = 123456789; // Modify this value if your reader has another installer level password

// Since we will use callback/event methods in this example, it is essental that
// the application and network object will not go out of scope (will not
// be destroyed) when we leave the Main function.
var g_oApp;
var g_oNetwork; 
var g_fFinishedScanning = false;


// Start the main function
Main();

// Since the network is now scanning asynchronously for readers, we can do some other work here 
// while waiting for the scan to complete...
while (!g_fFinishedScanning)
{
        // Simulate some extra work...
        WScript.Sleep(100);
}

WScript.Echo("Script now ready, terminating script.");


// Event callback, called for every found reader during the ScanReaders process
function OnProcessInfo(eProcessType, oBaseObject, eSubProcessType, oInfo)
{
        if (eProcessType == 0x0210) // ptScanningReaders
        {
                if (eSubProcessType == 0x0000) // sptProgress
                {
                        // Uncomment the following line to display progress information
                        // WScript.Echo("Scanning progress: '" + oInfo);                
                }
                if (eSubProcessType == 0x0004) // sptItemFound
                {
                        // Yes, found a reader
                        // Here we can logon to the reader, perform actions etc.
                        // For now, we will simply display the found reader
                        WScript.Echo("Found reader: '" + oInfo.Name + "' at address " + oInfo.NetworkAddress + ", with serial number: " + oInfo.SerialNumber.toString(16));             
                }
        }
}


// Event callback, called when the ScanReaders method has finished
function OnProcessFinished(eProcessType, oBaseObject, hrResultCode)
{
        if (eProcessType == 0x0210) // ptScanningReaders
        {
                // We do not want to receive anymore events, so detach:
                g_oNetwork.DetachEvent(2, OnProcessFinished); // 2 = DISPID_OnProcessFinished
                g_oNetwork.DetachEvent(3, OnProcessInfo); // 3 = DISPID_OnProcessInfo

                // Finished scanning readers, close the network..
                g_oNetwork.Disconnect();

                WScript.Echo("Now finished scanning.");
                g_fFinishedScanning = true;
        }
}


// All functionality is placed in a function to increase readability
function Main()
{
        // Create the main API application object
        g_oApp = new ActiveXObject("TalosAPI.Application");
        if (g_oApp)
        {
                // Create/find a network
                g_oNetwork  = g_oApp.FindReaderNetwork(iComPort);
                if (g_oNetwork)
                {
                        // Connect/open the network
                        if (g_oNetwork.Connect(iBaudRate))
                        {
                                // In JScript we will need to let the API know which event-callback functions 
                                // it should call.
                                g_oNetwork.AttachEvent(2, OnProcessFinished); // 2 = DISPID_OnProcessFinished
                                g_oNetwork.AttachEvent(3, OnProcessInfo); // 3 = DISPID_OnProcessInfo

                                // Not ready scanning!
                                g_fFinishedScanning = false;

                                // Scan all readers from address 0 to 32
                                // We scan asynchronously here!
                                WScript.Echo("Now starting to scan...");
                                g_oNetwork.ScanReaders(true, 0, 32);
                        }
                        else
                        {
                                WScript.Echo("Error, failed to connect to the network!");       
                        }
                }
                else
                {
                        WScript.Echo("Error, no network found on COM1, specify another COM port in the script!");       
                }
        }
        else
        {
                WScript.Echo("Error, failed to create the application object! Is the TalosAPI registered?");
        }
}

© Copyright 2001 - 2006 Cross Point. Generated on Mon Mar 12 16:29:52 2007 Cross Point