Click here to Skip to main content
15,889,216 members
Articles / Desktop Programming / Win32
Tip/Trick

PartnerTech CD-7220 POS Customer Display - .NET Class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
24 Sep 2013CPOL1 min read 31.7K   5   7
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

C#
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

VB.NET
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#

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

VB.NET
' 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United States United States
I’m retired. When I started my career, programming projects consisted of plugging wires into plug boards to create punch card processing applications to be run on electrical accounting machine like the IBM 402, 407, 085, 088, 514, 519, etc. From there, I moved to writing SPS and Autocoder applications on an IBM 1401 with 4K of memory eventually upgraded to 16K of memory. After many years of migrating my skills to various languages on various hardware platforms, I became an Information Technology Director where I didn’t need to program anymore. So, starting in 1996, I volunteered my time with a local community cable television organization and built some applications to help them run their operations. Originally in Clipper Summer 1987 and later Clipper 5.2, I migrated and enhanced those applications to VB .NET 2003 in 2003. I retired from my full-time job in 2010. Since then, I have continued to support the local community cable tv organization's applications. In 2013, I migrated the VB .NET 2003 Solution to VB .NET 2012 so that it can run on 64-bit computers and interact with Microsoft Office 2010. The upgrade went smoothly. In mid 2013, I developed a VB .NET 2012 application for them to download election results data from the Secretary of State's web site, format the results and send them to a VizRT character generator for on-air display.

Comments and Discussions

 
Questioncan help me Pin
ahmed hniesh6-Jun-22 23:46
ahmed hniesh6-Jun-22 23:46 
Questioncd722dusb.dll download link Pin
sandeshmms16-Aug-19 19:21
sandeshmms16-Aug-19 19:21 
Questionhow to check status USB port to open poledisplay Pin
mighe31-Dec-18 18:59
mighe31-Dec-18 18:59 
QuestionPort could not be opened Pin
vuamitom13-Jul-15 4:29
vuamitom13-Jul-15 4:29 
AnswerRe: Port could not be opened Pin
Mike Meinz13-Jul-15 11:59
Mike Meinz13-Jul-15 11:59 
QuestionThis works great and add few modifications Pin
PasinduM3-Sep-14 20:37
PasinduM3-Sep-14 20:37 
Generalworks great! Pin
JMMS Karunarathne25-Sep-13 0:27
JMMS Karunarathne25-Sep-13 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.