473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I open a cash drawer? C#

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...simply returns bSuccess as false...On this link: http://pages.prodigy.net/daleharris/popopen.htm i found the code for my printer which is : Epson TM-88IV Drawer codes: 27,112,48,55,121 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 28290
Plater
7,872 Expert 4TB
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
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 Expert 4TB
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
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...
6
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...
2
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...
0
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...
3
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...
0
by: CyberKnight | last post by:
Hello Friends, What function that enable the cash drawer in a POS, using visual basic coding. Thanks.
1
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...
1
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.