473,883 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I open a cash drawer? C#

97 New Member
Hi, I need some assistance. I'ved been searching the web for days now and can't find an example that really helps me. I have an EPSON TM-T88IV printer through which I want to open a cash drawer. I have this class in my project:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace PrintDataGrid
  6.     /*    For a standard cash drawer, there's a physical cable that runs from the cash drawer and plugs into the receipt printer. 
  7. •    To open the drawer, you issue a command to the printer. The printer will send a signal to the cash drawer that kicks open the drawer. 
  8. •    For a standard epson receipt printer such as the Epson TM-T88III receipt printer, the command is:
  9. fwrite($handle, chr(27). chr(112). chr(0). chr(100). chr(250)); 
  10. •    For other receipt printers, you should be able to find the command in their user's manual. (Try the above code first. Most probably it will work. Receipt printers have been in existence for a long time. They have more or less adopted the same standard.) 
  11. •    A standard receipt printer such as the Epson TM-T88III receipt printer uses the parallel port. 
  12. •    To print to the parallel-port receipt printer, you print through port PRN (exactly the same as printing from DOS prompt). 
  13. •    From within PHP-GTK2, you need to first establish the connection with the printer by using 
  14. $handle = fopen("PRN", "w"); 
  15. •    Thereafter, to print anything to the printer, you just "write" to it like the file handle: fwrite($handle, 'text to printer'); 
  16. •    There are newer receipt printer that uses USB. I believe you should be able to print to such printers through PRN too. 
  17. */
  18. {
  19.     public class RawPrinterHelper
  20.     {
  21.     // Structure and API declarions:
  22.         [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
  23.         public class DOCINFOA
  24.             {
  25.             [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
  26.             [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
  27.             [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
  28.             }
  29.         [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true,CharSet=CharSet.Ansi, ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
  30.         public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] 
  31.         string szPrinter, out IntPtr hPrinter, long pd);
  32.         [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
  33.         public static extern bool ClosePrinter(IntPtr hPrinter);
  34.         [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true,CharSet=CharSet.Ansi, ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
  35.         public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level,[In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  36.         [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
  37.         public static extern bool EndDocPrinter(IntPtr hPrinter);
  38.         [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
  39.         public static extern bool StartPagePrinter(IntPtr hPrinter);
  40.         [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
  41.         public static extern bool EndPagePrinter(IntPtr hPrinter);
  42.         [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
  43.         public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
  44.         [DllImport("kernel32.dll", EntryPoint="GetLastError", SetLastError=false, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
  45.         public static extern Int32 GetLastError();
  46.         public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
  47.         {
  48.             Int32 dwError = 0, dwWritten = 0;
  49.             IntPtr hPrinter = new IntPtr(0);
  50.             DOCINFOA di = new DOCINFOA();
  51.             bool bSuccess = false; // Assume failure unless you specifically succeed.
  52.             di.pDocName = "My C#.NET RAW Document";
  53.             di.pDataType = "RAW";
  54.             if( OpenPrinter( szPrinterName, out hPrinter, 0 ) )
  55.             {
  56.                 if( StartDocPrinter(hPrinter, 1, di) )
  57.                     {
  58.                     if( StartPagePrinter(hPrinter) )
  59.                         {
  60.                         bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  61.                         EndPagePrinter(hPrinter);}
  62.                     EndDocPrinter(hPrinter);
  63.                     }
  64.                 ClosePrinter(hPrinter);
  65.                 }
  66.             if( bSuccess == false )
  67.                 {
  68.                 dwError = GetLastError();
  69.                 }
  70.             return bSuccess;
  71.             }
  72.         public static bool SendFileToPrinter( string szPrinterName, string szFileName )
  73.             {
  74.             FileStream fs = new FileStream(szFileName, FileMode.Open);
  75.             BinaryReader br = new BinaryReader(fs);
  76.             Byte []bytes = new Byte[fs.Length];
  77.             bool bSuccess = false;
  78.             IntPtr pUnmanagedBytes = new IntPtr(0);
  79.             int nLength;
  80.             nLength = Convert.ToInt32(fs.Length);
  81.             bytes = br.ReadBytes( nLength );
  82.             pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  83.             Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
  84.             bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
  85.             Marshal.FreeCoTaskMem(pUnmanagedBytes);
  86.             return bSuccess;
  87.             }
  88.         public static bool SendStringToPrinter( string szPrinterName, string szString )
  89.             {
  90.             IntPtr pBytes;
  91.             Int32 dwCount;
  92.             dwCount = szString.Length;
  93.             pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  94.             SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  95.             Marshal.FreeCoTaskMem(pBytes);
  96.             return true;
  97.             }
  98.         public static bool OpenCashDrawer1( string szPrinterName)
  99.             {
  100.                 //27,112,48,55,121
  101.             Int32 dwError = 0, dwWritten = 0;
  102.             IntPtr hPrinter = new IntPtr(0);
  103.             DOCINFOA di = new DOCINFOA();            
  104.             bool bSuccess = false;
  105.             di.pDocName = "OpenDrawer";
  106.             di.pDataType = "RAW";
  107.             if( OpenPrinter( szPrinterName, out hPrinter, 0 ) )
  108.                 {
  109.                 if( StartDocPrinter(hPrinter, 1, di) )
  110.                     {
  111.                     if( StartPagePrinter(hPrinter) )
  112.                         {
  113.                         int nLength;
  114.                         byte[] DrawerOpen = new byte[] { 07 };
  115.                         nLength = DrawerOpen.Length;
  116.                         IntPtr p = Marshal.AllocCoTaskMem(nLength);
  117.                         Marshal.Copy(DrawerOpen, 0, p, nLength);
  118.                         bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten);
  119.                         EndPagePrinter(hPrinter);
  120.                         Marshal.FreeCoTaskMem(p);
  121.                         }
  122.                     EndDocPrinter(hPrinter);
  123.                     }
  124.                 ClosePrinter(hPrinter);
  125.                 }
  126.             if( bSuccess == false )
  127.                 {
  128.                 dwError = GetLastError();
  129.                 }
  130.             return bSuccess;
  131.             }
  132.         public static bool FullCut( string szPrinterName)
  133.             {
  134.                 //27, 109
  135.             Int32 dwError = 0, dwWritten = 0;
  136.             IntPtr hPrinter = new IntPtr(0);
  137.             DOCINFOA di = new DOCINFOA();
  138.             bool bSuccess = false;
  139.             di.pDocName = "FullCut";
  140.             di.pDataType = "RAW";
  141.             if( OpenPrinter( szPrinterName, out hPrinter, 0 ) )
  142.                 {
  143.                 if( StartDocPrinter(hPrinter, 1, di) )
  144.                     {
  145.                     if( StartPagePrinter(hPrinter) )
  146.                         {
  147.                         int nLength;
  148.                         byte[] DrawerOpen = new byte[] { 27, 100, 51 };
  149.                         nLength = DrawerOpen.Length;
  150.                         IntPtr p = Marshal.AllocCoTaskMem(nLength);
  151.                         Marshal.Copy(DrawerOpen, 0, p, nLength);
  152.                         bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten);
  153.                         EndPagePrinter(hPrinter);
  154.                         Marshal.FreeCoTaskMem(p);
  155.                         }
  156.                     EndDocPrinter(hPrinter);
  157.                     }
  158.                 ClosePrinter(hPrinter);
  159.                 }
  160.             if( bSuccess == false )
  161.                 {
  162.                 dwError = GetLastError();
  163.                 }
  164.             return bSuccess;
  165.             }
  166.         }
  167.     }
  168.  
then in my windows form I have a button through which I want when a user clicks on it it opens the cash drawer...my code for that button is :
Expand|Select|Wrap|Line Numbers
  1. RawPrinterHelper.OpenCashDrawer1("EPSON TM-T88IV");
  2.  
But that doesn't do anything whatsoever...si mply returns bSuccess as false...On this link: http://pages.prodigy.n et/daleharris/popopen.htm i found the code for my printer which is : Epson TM-88IV Drawer codes: 27,112,48,55,12 1 Cutter codes: 27, 109. I even implemented that in the full cut function of the class but still doesn't work.

Any input will be greatly appreciated. My printer works perfectly, the cash drawer simply does not open....please shed some light on the matter...maybe im missing something else since in vb you must convert it into a string using this BASIC code...

POPOPEN$ = CHR$(27) + CHR$(112) + CHR$(48) + CHR$(55) + CHR$(121)


Now all you have to do to open your cash drawer is to send the string to the printer using...

LPRINT POPOPEN$

But i need the code in C#.

Thanks...
Jul 7 '08 #1
3 28612
Plater
7,872 Recognized Expert Expert
Assuming your printer writing functions are correct...your code never shows you sending the 127 112 48 55 121.
I see 07 for open and 27 100 51 for full cut, but never your drawer opener sequence?
Jul 8 '08 #2
gggram2000
97 New Member
Assuming your printer writing functions are correct...your code never shows you sending the 127 112 48 55 121.
I see 07 for open and 27 100 51 for full cut, but never your drawer opener sequence?
Yeah I tried it including the codes but still nothing happens.

this is the comment I got on the code im using:
Expand|Select|Wrap|Line Numbers
  1. /*    For a standard cash drawer, there's a physical cable that runs from the cash drawer and plugs into the receipt printer. 
  2. •    To open the drawer, you issue a command to the printer. The printer will send a signal to the cash drawer that kicks open the drawer. 
  3. •    For a standard epson receipt printer such as the Epson TM-T88III receipt printer, the command is:
  4. fwrite($handle, chr(27). chr(112). chr(0). chr(100). chr(250)); 
  5. •    For other receipt printers, you should be able to find the command in their user's manual. (Try the above code first. Most probably it will work. Receipt printers have been in existence for a long time. They have more or less adopted the same standard.) 
  6. •    A standard receipt printer such as the Epson TM-T88III receipt printer uses the parallel port. 
  7. •    To print to the parallel-port receipt printer, you print through port PRN (exactly the same as printing from DOS prompt). 
  8. •    From within PHP-GTK2, you need to first establish the connection with the printer by using 
  9. $handle = fopen("PRN", "w"); 
  10. •    Thereafter, to print anything to the printer, you just "write" to it like the file handle: fwrite($handle, 'text to printer'); 
  11. •    There are newer receipt printer that uses USB. I believe you should be able to print to such printers through PRN too. 
  12. */
  13.  
I am using a USB connection to the printer, but i see the code in this comment is PHP. I would need to know how to use it using C#. Any ideas?
Jul 8 '08 #3
Plater
7,872 Recognized Expert Expert
Well wait, it's in a USB port?
All the code you were writing was for talking on a parallel port though?
Jul 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6326
by: Scott A. Keen | last post by:
Hi, I'm getting tasked with writing an ASP application deployed throughout the company's intranet where all the workstations will be running Internet Explorer 6.0. It's a retail business, and some of the workstations are Point-Of-Sale and will need to have cash drawers and bar-code scanners. The project managers want this as a web-application because of the ease of
6
7590
by: PW | last post by:
I have an electronic cash drawer as part of a point-of-sale installation. You're supposed to be able to automatically open the cash drawer with some printer commands (drawer is connected to the receipt printer) but I can't get it to open automatically. I want it to open when the customers receipt is printed. How do you send printer escape sequences to a windows printer via ASP ? The cash drawer model is ... Posiflex CR-4100 Cash Drawer
2
27169
by: Tor Inge Schulstad | last post by:
Hi I'm developing a POS software using an Epson TM-T88iii printer with a cash drawer connected to the printer. The printer is connected via a usb interface to the pc. Can anyone tell me how I can open the cash drawer from my VB.NET application? The printer driver requires me to use a soft font named "control" in the printer, and print the letter "A", but if I'm right, the .NET framework does not support using soft fonts...
0
1847
by: samozluk | last post by:
I am using Ms Access 2003 to produce a customer receipt, the printer is the Posiflex PP-7000 II Usb thermal printer and it is meant to open the cash drawer. Can someone please give me the code to add to the report to open the cash draw. Thanks in advance.
3
3592
by: fidamon | last post by:
hi if any body can help me with that question i will be thankfull How to open a cash drawer connected to serial comm it has no name no user manual and nothing but a serial port connection and a power supply connection Fidamon
0
1715
by: CyberKnight | last post by:
Hello Friends, What function that enable the cash drawer in a POS, using visual basic coding. Thanks.
1
3745
by: danishce | last post by:
Hi, I've been trying to configure the cash drawer connected directly to the CPU of the IBM Surepos PC. The cash drawer IBM P/N is 74F6178, IBM FRU P/N is 93F1901. The cash drawer port is connected to the 3A port on the CPU. Does anybody know how to configure this to open the cash drawer because i have no manual and documentation for the cash drawer? Regards: Danish Majid
1
3181
by: imlambo | last post by:
I am designing a point of sale and i am stuck on how to open a cash drawer. Since my system is going to be used by many clients i need help on a design which can open all cash drawers. Or just for EPSON cash drawer. I need VB.net 2003 source code i any one has it. Help me I am stuck. rgds
4
5419
by: ARC | last post by:
I have a user asking if I could put in a code that will open a register drawer. My understanding is the receipt printer will normally send the code the cash drawer. Is there a vba function that someone can share that will open a drawer without printing? Or does it depend on the drawer? I suppose if it's just a sequence of characters, then it could be made customizable to work for many different users. Any ideas? Thanks! Andy
0
9945
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10763
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.