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#
using YourNameSpaceName;
...
YourNameSpaceName.USB cUSB = new YourNameSpaceName.USB();
public void WriteSomethingRedToPrinterThroughDisplay()
{
cUSB.OpenPort();
cUSB.WritePort(Strings.Chr(12));
cUSB.WritePort(Strings.Chr(27) + Strings.Chr(61) +
Strings.Chr(1));
cUSB.WritePort(Strings.Chr(27) + Strings.Chr(64));
cUSB.WritePort(Strings.Chr(27) + Strings.Chr(114) +
Strings.Chr(1));
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));
cUSB.WritePort(Strings.Chr(27) + Strings.Chr(114) +
Strings.Chr(0));
cUSB.WritePort(Strings.Chr(27) + Strings.Chr(61) +
Strings.Chr(2));
cUSB.ClosePort();
}
Example of How to Use the USB Class from VB
Imports CD7220.CD7220USB
...
Dim cUSB As YourNameSpaceName.USB = New YourNameSpaceName.USB
Sub WriteSomethingRedToPrinterThroughDisplay()
cUSB.OpenPort()
cUSB.WritePort(Chr(12))
cUSB.WritePort(Chr(27) & Chr(61) & _
Chr(1))
cUSB.WritePort(Chr(27) & Chr(64))
cUSB.WritePort(Chr(27) & Chr(114) & _
Chr(1))
cUSB.WritePort(String.Format("{0,-10}_
{1,7:-0.000}{2,10:0.00}{3,13:-0.00}", _
tempitemid, tempunits, tempunitprice, _
tempsubtotal) & Chr(10))
cUSB.WritePort(Chr(27) & Chr(114) & Chr(0))
cUSB.WritePort(Chr(27) & Chr(61) & _
Chr(2))
cUSB.ClosePort()
End Sub
History
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.