IDTech Windows SDK Guide  1.00.029
API reference for UniPay
Sample Project Tutorial

Using Visual Studio 2015, we will create a C# sample project that will interface with the UniPay and will perform the following activities:

  • Get firmware version
  • Display ICC Status (card inserted/removed, ICC power on/off)
  • Turn on/off ICC reader
  • Start/Cancel MSR reading
  • Show log of all data going to/from UniPay

Step 1: Create New Project

Create a new Windows Form Application in Visual Studio. In our example, we use project name UniPay_Simple_Demo

newProject.png

Step 2: Import Libraries

Import the necessary libraries

Step 3: Design Interface

Use the Form Designer to layout buttons/fields
Open your form designer and add items so it contains the following buttons/fields:

  • Add buttons to execute the following functions:
    • Show Firmware
    • Show ICC Status
    • Turn ON ICC Reader
    • Turn OFF ICC Reader
    • Start MSR Swipe
    • Cancel MSR Swipe
  • Add a text box to communicate data from the UniPay.
  • Add a text box for the log of UniPay.
form.png

Step 4: Configure the project file

In the start of the class file, perform the following:

  • Add using statements to utilize library
  • set callback method in the class initializer. Reference: Initialize UniPay
  • Implement the button methods Click handlers for button press code execution
    private void btnPowerOnICC_Click(object sender, EventArgs e)
    {
    byte[] ATR = null;
    RETURN_CODE rt = IDT_UniPay.SharedController.icc_powerOnICC(ref ATR);
    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
    {
    tbOutput.AppendText("ATR:" + string.Concat(ATR.ToArray().Select(b => b.ToString("X2")).ToArray()) + "\r\n");
    tbOutput.AppendText("ICC Powered On successfully\r\n");
    }
    else
    {
    tbOutput.AppendText("ICC Powered On failed Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt) + ": " + IDTechSDK.errorCode.getErrorString(rt) + "\r\n");
    }
    }
    private void btnPowerOffICC_Click(object sender, EventArgs e)
    {
    RETURN_CODE rt = IDT_UniPay.SharedController.icc_powerOffICC();
    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
    {
    tbOutput.AppendText("ICC Powered Off successfully\r\n");
    System.Diagnostics.Debug.WriteLine("ICC powered Off successfully");
    }
    else
    {
    tbOutput.AppendText("ICC Powered Off failed Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt) + ": " + IDTechSDK.errorCode.getErrorString(rt) + "\r\n");
    System.Diagnostics.Debug.WriteLine("ICC powered Off failed Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt));
    }
    }
    private void btnFirmwareVersion_Click(object sender, EventArgs e)
    {
    string firmwareVersion = "";
    byte[] reData = { };
    RETURN_CODE rt = IDT_UniPay.SharedController.device_getFirmwareVersion(ref firmwareVersion);
    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
    {
    tbOutput.AppendText("Firmware Ver: " + firmwareVersion + "\r\n");
    }
    else
    {
    tbOutput.AppendText("Get Firmware Fail Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt) + ": " + IDTechSDK.errorCode.getErrorString(rt) + "\r\n");
    }
    }
    private void btnICCStatus_Click(object sender, EventArgs e)
    {
    byte status = 0;
    RETURN_CODE rt = IDT_UniPay.SharedController.icc_getICCReaderStatus(ref status);
    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
    {
    byte bit0 = (byte)(status & 0x01);
    byte bit1 = (byte)((status & 0x02) >> 1);
    tbOutput.AppendText("ICC Reader Status : " + (bit0 == 1 ? "[ICC Powered] " : "[ICC Power Not Ready] ") + (bit1 == 1 ? "[Card Seated]" : "[Card Not Seated]") + "\r\n");
    }
    else
    {
    tbOutput.AppendText("Get ICC Reader Status Failed Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt) + ": " + IDTechSDK.errorCode.getErrorString(rt) + "\r\n");
    }
    }
    private void btnStartMSRTransaction_Click(object sender, EventArgs e)
    {
    RETURN_CODE rt = IDT_UniPay.SharedController.msr_startMSRSwipe(60);
    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
    {
    //EY: MSR BLUE LED BLINK
    tbOutput.AppendText("MSR Turned On successfully; Ready to swipe\r\n");
    tbOutput.ReadOnly = false;
    tbOutput.Focus();
    System.Diagnostics.Debug.WriteLine("MSR Turned On successfully; Ready to swipe");
    }
    }
    private void btnCancelMSRTransaction_Click(object sender, EventArgs e)
    {
    RETURN_CODE rt = IDT_UniPay.SharedController.msr_cancelMSRSwipe();
    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
    {
    tbOutput.AppendText("MSR Turned Off successfully\r\n");
    System.Diagnostics.Debug.WriteLine("MSR Turned Off successfully");
    }
    else
    {
    tbOutput.AppendText("MSR Turned Off failed Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt) + ": " + IDTechSDK.errorCode.getErrorString(rt) + "\r\n");
    System.Diagnostics.Debug.WriteLine("MSR Turned Off failed Error Code: " + "0x" + String.Format("{0:X}", (ushort)rt));
    }
    }

Step 5: Configure callback to receive important SDK data (messages,log info and transaction results)

delegate void SetTextCallback(string text);
delegate void SetTextCallbackWithTextAlign(string text, bool withAlign = false);
public static String GetTimestamp()
{
DateTime value = DateTime.Now;
return value.ToString("HH:mm:ss.fff");
}
private void MessageCallBack(IDTechSDK.IDT_DEVICE_Types type, DeviceState state, byte[] data, IDTTransactionData cardData, EMV_Callback emvCallback, RETURN_CODE transactionResultCode)
{
IDTechComm comm = Profile.getComm(type, DEVICE_INTERFACE_Types.DEVICE_INTERFACE_USB);
if (comm == null) comm = Profile.getComm(type, DEVICE_INTERFACE_Types.DEVICE_INTERFACE_SERIAL);
if (comm == null) comm = Profile.getComm(type, DEVICE_INTERFACE_Types.DEVICE_INTERFACE_BT);
if (comm == null) comm = Profile.getComm(type, DEVICE_INTERFACE_Types.DEVICE_INTERFACE_AUDIO_JACK);
DEVICE_INTERFACE_Types connect = DEVICE_INTERFACE_Types.DEVICE_INTERFACE_UNKNOWN;
DEVICE_PROTOCOL_Types protocol = DEVICE_PROTOCOL_Types.DEVICE_PROTOCOL_UNKNOWN;
if (comm != null)
{
connect = comm.getDeviceConnection();
protocol = comm.getDeviceProtocol();
}
switch (state)
{
case DeviceState.ToConnect:
SetOutputText("To connect\n");
break;
case DeviceState.DefaultDeviceTypeChange:
SetOutputText("\nSDK Default Device = " + IDTechSDK.Profile.IDT_DEVICE_String(type, connect));
SetTitleText("Universal SDK Demo: " + IDTechSDK.Profile.IDT_DEVICE_String(type, connect));
break;
case DeviceState.Connected:
SetOutputText("\nConnected " + IDTechSDK.Profile.IDT_DEVICE_String(type, connect));
break;
case DeviceState.Disconnected:
SetOutputText("\nDisconnected " + IDTechSDK.Profile.IDT_DEVICE_String(type, connect));
break;
case DeviceState.ConnectionFailed:
SetOutputText("\nConnection Failed\n");
break;
case DeviceState.TransactionData:
if (cardData == null) break;
//output parsed card data
SetOutputText("Return Code MSR: " + transactionResultCode.ToString() + "\r\n");
displayCardData(cardData);
break;
case DeviceState.DataReceived:
SetOutputTextLog(GetTimestamp() + " IN: " + Common.getHexStringFromBytes(data));
break;
case DeviceState.DataSent:
SetOutputTextLog(GetTimestamp() + " OUT: " + Common.getHexStringFromBytes(data));
break;
case DeviceState.CommandTimeout:
SetOutputText("Command Timeout\n");
break;
case DeviceState.ToSwipe:
SetOutputText("To Swipe\n");
break;
case DeviceState.MSRDecodeError:
SetOutputText("MSR Decode Error\n");
break;
case DeviceState.ToTap:
SetOutputText("To Tap\n");
break;
case DeviceState.SwipeTimeout:
SetOutputText("Swipe Timeout\n");
break;
case DeviceState.TransactionCancelled:
SetOutputText("Transaction Cancelled\n");
break;
case DeviceState.DeviceTimeout:
SetOutputText("Device Timeout\n");
break;
case DeviceState.TransactionFailed:
SetOutputText("Transaction Failed: " + IDTechSDK.errorCode.getErrorString(transactionResultCode) + "\r\n");
break;
case DeviceState.EMVCallback:
SetOutputText("EMV Callback\n");
break;
default:
break;
}
}