Results 1 to 12 of 12

Thread: [RESOLVED] Open Cash Drawer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Resolved [RESOLVED] Open Cash Drawer

    Hello, I need help, can anyone help me?

    I want to open a cash drawer using VB.NET code, but I can't find the proper code. I use EPSON Printer TM-U220, and it's connected to a drawer; the printer itself is connected to the computer using COM port.

    I notice the following code works well in VB:
    Code:
        Printer.FontName = "control"
        Printer.Print "A"
        Printer.EndDoc
    Unfortunately, there's no Printer object in VS 2005, substituted by PrintDocument, but can't find some properties in it.

    I found this code, using VB.NET, but it's using LPT1, not COM1. I tried replacing LPT1 with COM1, but failed.
    Code:
            Dim intFileNo As Integer = FreeFile()
            FileOpen(1, "c:\escapes.txt", OpenMode.Output)
            PrintLine(1, Chr(27) & Chr(112) & Chr(0) & Chr(25) & Chr(250))
            FileClose(1)
            Shell("print /d:LPT1 c:\escapes.txt", vbNormalFocus)
    Any help would be appreciated.

    Thanx
    The only thing for the triumph of evil is for a good man to do nothing

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Open Cash Drawer

    Microsoft provide a library that allows you to print in essentially the same way as in VB6. I'm not sure if will work in the instance but it might.

    http://msdn2.microsoft.com/en-us/vbasic/bb219077.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: Open Cash Drawer

    I don't actually use .NET but it sounds to me like you are using the wrong method with print. In VB6 there is a very easy to use serial port object that can do this for you. I don't know if it exists in .NET but if you want me to look it up just give me a hoy.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Open Cash Drawer

    The download won't start.. Do u have one (the library file), or do u have any other downloadable links? Thanx.

    Quote Originally Posted by jmcilhinney
    Microsoft provide a library that allows you to print in essentially the same way as in VB6. I'm not sure if will work in the instance but it might.

    http://msdn2.microsoft.com/en-us/vbasic/bb219077.aspx
    The only thing for the triumph of evil is for a good man to do nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Open Cash Drawer

    I'll be grateful it u're willing to help me searching for it.. I've actually searched it for quite few days, but can't find the one that really works, either it's because the port(I'm using COM1), or the object / component used in VB.NET.


    Quote Originally Posted by AsmIscool
    I don't actually use .NET but it sounds to me like you are using the wrong method with print. In VB6 there is a very easy to use serial port object that can do this for you. I don't know if it exists in .NET but if you want me to look it up just give me a hoy.
    The only thing for the triumph of evil is for a good man to do nothing

  6. #6
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Open Cash Drawer

    I'm a bit confused as to whether you are using the COM port or the LPT port, as you mention both.

    If you are using a COM port, then you can use the SerialPort class provided by the framework.

    By the looks of things, you are only required to send a few bytes out to the com port to open the cash drawer, is that correct?

    If that's the case, then try this code:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         ' create an array to hold the command code bytes
    4.         Dim cashDrawerCmd() As Byte = {27, 112, 0, 25, 250}
    5.  
    6.         ' sets up comPort as SerialPort and opens the port
    7.         Using comPort As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
    8.  
    9.             ' setup the com port with the required parameters. (Set these to suit)
    10.             comPort.BaudRate = 9600
    11.             comPort.DataBits = 8
    12.             comPort.Parity = System.IO.Ports.Parity.None
    13.             comPort.StopBits = System.IO.Ports.StopBits.One
    14.             comPort.Handshake = System.IO.Ports.Handshake.RequestToSend
    15.  
    16.             ' writes the whole of the byte array containg the open drawer command codes to the port
    17.             comPort.Write(cashDrawerCmd, 0, cashDrawerCmd.Length)
    18.  
    19.         End Using
    20.  
    21.     End Sub
    Last edited by Andy_P; Aug 4th, 2007 at 07:49 AM.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Open Cash Drawer

    I'm using the port COM1..
    But as I installed the driver, the port selected is now not really COM1, it's "ESD_COM1". Now, that's the problem, I don't know how to open the port. I've also tried the code u posted above, an error occured on this line
    Code:
    Using comPort As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
    It said : "Access to the port COM1 is denied"

    When I replaced it with
    Code:
    Using comPort As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("ESD_COM1")
    It said : "The given port name does not start with COM/com or does not resolve to a valid serial port.
    Parameter name: portName"
    The only thing for the triumph of evil is for a good man to do nothing

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Open Cash Drawer

    Ok, I've found the way to open the cash drawer. I change the port to COM1, and use:
    Code:
       FileOpen(1, AppDomain.CurrentDomain.BaseDirectory & "open.txt", OpenMode.Output)
       PrintLine(1, Chr(27) & Chr(112) & Chr(0) & Chr(25) & Chr(250))
       FileClose(1)
    
       Shell("print /d:com1 open.txt", AppWinStyle.Hide)
    Thanx, jmcilhinney, AsmIscool, and Andy_P
    Last edited by steven_luck1; Aug 7th, 2007 at 03:32 AM.
    The only thing for the triumph of evil is for a good man to do nothing

  9. #9
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: [RESOLVED] Open Cash Drawer

    i have tried to code above but the poblem is it doesnt print.. the open.txt file is corrupt.. the content become like this

    Name:  Untitled2.png
Views: 13730
Size:  1.1 KB

  10. #10
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: [RESOLVED] Open Cash Drawer

    The problem with this code is when it prints the open.txt, in only open the cash drawer after printing.. can they both execute on the same time? How?

    Code:
       FileOpen(1, AppDomain.CurrentDomain.BaseDirectory & "open.txt", OpenMode.Output)
       PrintLine(1, Chr(27) & Chr(112) & Chr(0) & Chr(25) & Chr(250))
       FileClose(1)
    
       Shell("print /d:com1 open.txt", AppWinStyle.Hide)

  11. #11
    New Member
    Join Date
    May 2019
    Posts
    2

    Re: Open Cash Drawer

    Nice, this is in C#

    File.WriteAllBytes(fileName, new byte[]{ 27, 112, 0, 25, 250 });
    File.Copy(fileName, networkSharedPrinterPath);

  12. #12
    New Member
    Join Date
    May 2019
    Posts
    2

    Resolved Re: Open Cash Drawer

    Nice, this is in C#

    File.WriteAllBytes(fileName, new byte[]{ 27, 112, 0, 25, 250 });
    File.Copy(fileName, networkSharedPrinterPath);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width