Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

PartnerTech CD-7220 POS Customer Display - .NET Class

0.00/5 (No votes)
24 Sep 2013 1  
PartnerTech provides a 32-bit DLL and a 64-bit DLL for communicating with the CD-7220 Series Customer Display in true USB mode. This tip demonstrates a .NET class that can be used to provide an easy way to access the PartnerTech-provided 32-bit DLL.

Introduction

This .NET class provides a simple way to communicate with the PartnerTech CD-7220 Customer Display (POS pole display) using the PartnerTech-provided 32-bit True USB cd722dusb.dll.

Background

This .NET class was developed to provide a simple way to access the 32-bit True USB cd722dusb.dll. PartnerTech provides a comprehensive VB6 example. That example was used to understand how to communicate with the cd722dusb.dll. Using that knowledge, I developed this simple .NET class to send commands and text to the CD-7220. I developed this class to ease the migration of an existing POS application from a serial port connection to a true USB connected CD-7220. This class handles the encoding of strings into Byte arrays (and vice-versa) so that the application developer doesn't have to worry about that step.

Using the Code

The source code for this class can be inserted into your Visual Studio project to create an internal class or it can be put into a new Visual Studio .NET Class Library project to create a DLL that can be distributed with your project. I named the class USB. Below are both a C# and VB version of the USB class and C# and VB examples of sending control commands and data to the CD-7220 device.

Within your Visual Studio, set the Target CPU of your POS application to x86. I could not add a reference to cd722dusb.dll because it is not a COM component.

The PartnerTech-provided 32-bit true USB cd722dusb.dll needs to be installed in the same directory as your POS application.

C# version of .NET Class

using System.Runtime.InteropServices;
namespace YourNameSpaceName
{
    public class USB
    {
        [DllImport("cd722dusb.dll", 
        CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool opencd722usb();
        [DllImport("cd722dusb.dll", 
        CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
         public static extern int writecd722usb(ref byte dataoutput, int Length);
        [DllImport("cd722dusb.dll", 
        CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int readcd722usb(ref byte DataInput, int size);
         [DllImport("cd722dusb.dll", 
         CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool closecd722usb();
        private bool m_USB_is_Open = false;
        private bool m_Result = false;
        public USB()
        {
        }

        public void OpenPort()
        {
            if (m_USB_is_Open)
            {
                throw new System.IO.IOException("USB is already open");
             }
            m_Result = opencd722usb();
            if (m_Result)
            {
                m_USB_is_Open = true;
            }
            else
            {
                throw new System.IO.IOException("Unable to open USB");
            }
        }

        public void ClosePort()
         {
            if (!m_USB_is_Open)
            {
                throw new System.IO.IOException("USB is already closed");
            }
            m_Result = closecd722usb();
            if (m_Result)
            {
                m_USB_is_Open = false;
            }
             else
            {
                throw new System.IO.IOException("Unable to close USB");
            }
        }

        public void WritePort(string DataOutput)
        {
            System.Text.Encoding Encoding = System.Text.Encoding.UTF8;
            byte[] byteOutput = Encoding.GetBytes(DataOutput);
            int Result = writecd722usb(ref byteOutput[0], byteOutput.Length);
        }

        public string ReadPort()
        {
            System.Text.Encoding Encoding = System.Text.Encoding.UTF8;
            byte[] readBuffer = new byte[2049];
            int readLength = 0;
            int Result = readcd722usb(ref readBuffer[0], readLength);
            return new string(Encoding.GetChars(readBuffer, 0, readLength));
         }
    }
}

VB version of .NET Class

Namespace YourNameSpaceName
    Public Class USB
        Declare Function opencd722usb Lib "cd722dusb.dll" () As Boolean
        Declare Function writecd722usb Lib "cd722dusb.dll" _
        (ByRef dataoutput As Byte, ByVal Length As Integer) As Integer
        Declare Function readcd722usb Lib "cd722dusb.dll" _
        (ByRef DataInput As Byte, ByVal size As Integer) As Integer
        Declare Function closecd722usb Lib "cd722dusb.dll" () As Boolean
        Private m_USB_is_Open As Boolean = False
        Private m_Result As Boolean = Nothing

        Public Sub New()
        End Sub

        Public Sub OpenPort()
            If m_USB_is_Open Then
                Throw New System.IO.IOException("USB is already open")
            End If
            m_Result = opencd722usb()
            If m_Result Then
                m_USB_is_Open = True
            Else
                Throw New System.IO.IOException("Unable to open USB")
            End If
        End Sub

        Public Sub ClosePort()
            If Not m_USB_is_Open Then
                Throw New System.IO.IOException("USB is already closed")
            End If
            m_Result = closecd722usb()
            If m_Result Then
                m_USB_is_Open = False
            Else
                Throw New System.IO.IOException("Unable to close USB")
            End If
        End Sub

        Public Sub WritePort(ByVal DataOutput As String)
            Dim Encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Dim byteOutput() As Byte = Encoding.GetBytes(DataOutput)
            Dim Result As Integer = writecd722usb(byteOutput(0), byteOutput.Length)
        End Sub

        Public Function ReadPort() As String
            Dim Encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Dim readBuffer(2048) As Byte
            Dim readLength As Integer
            Dim Result As Integer = readcd722usb(readBuffer(0), readLength)
            ReadPort = New String(Encoding.GetChars(readBuffer, 0, readLength))
        End Function
    End Class
End Namespace

Example of How to Use the USB Class from C#

// If it is an external class, you need the using statement
using YourNameSpaceName;

...
 
// Declaration at beginning of program
YourNameSpaceName.USB cUSB = new YourNameSpaceName.USB();

public void WriteSomethingRedToPrinterThroughDisplay()
	{
	    cUSB.OpenPort(); // Open the USB Port
	    cUSB.WritePort(Strings.Chr(12));   // Clear pole display
	    cUSB.WritePort(Strings.Chr(27) + Strings.Chr(61) + 
	    Strings.Chr(1));   // Send print through pole display
	    cUSB.WritePort(Strings.Chr(27) + Strings.Chr(64));   // Initialize printer
	    cUSB.WritePort(Strings.Chr(27) + Strings.Chr(114) + 
	    Strings.Chr(1));   // Select Red color to print
	    cUSB.WritePort(string.Format("{0,-10}{1,7:-0.000}
	    {2,10:0.00}{3,13:-0.00}", tempitemid, tempunits, 
	    tempunitprice, tempsubtotal) + Strings.Chr(10));   // Print text and new line
	    cUSB.WritePort(Strings.Chr(27) + Strings.Chr(114) + 
	    Strings.Chr(0));   // Set color to default Black
	    cUSB.WritePort(Strings.Chr(27) + Strings.Chr(61) + 
	    Strings.Chr(2));   // De-select printer and enable pole display
	    cUSB.ClosePort();  // Close the USB Port
	}

Example of How to Use the USB Class from VB

' If it is an external class, you need the Imports statement
Imports CD7220.CD7220USB
...

Dim cUSB As YourNameSpaceName.USB = New YourNameSpaceName.USB

Sub WriteSomethingRedToPrinterThroughDisplay()
        cUSB.OpenPort()
        cUSB.WritePort(Chr(12)) ' Clear pole display
        cUSB.WritePort(Chr(27) & Chr(61) & _
        Chr(1)) ' Send print through pole display
        cUSB.WritePort(Chr(27) & Chr(64)) ' Initialize printer
        cUSB.WritePort(Chr(27) & Chr(114) & _
        Chr(1)) ' Select Red color to print
        cUSB.WritePort(String.Format("{0,-10}_
        {1,7:-0.000}{2,10:0.00}{3,13:-0.00}", _
            tempitemid, tempunits, tempunitprice, _
            tempsubtotal) & Chr(10)) ' Print text and new line
        cUSB.WritePort(Chr(27) & Chr(114) & Chr(0)) ' Set color to default Black
        cUSB.WritePort(Chr(27) & Chr(61) & _
        Chr(2)) ' De-select printer and enable pole display
        cUSB.ClosePort()
End Sub

History

  • Version 1 - 09/24/2013

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here