票据打印类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace Comm
{
    public class TicketPrinter
    {
        public const short FILE_ATTRIBUTE_NORMAL = 0x80;
        public const short INVALID_HANDLE_VALUE = -1;
        public const uint GENERIC_READ = 0x80000000;
        public const uint GENERIC_WRITE = 0x40000000;
        public const uint CREATE_NEW = 1;
        public const uint CREATE_ALWAYS = 2;
        public const uint OPEN_EXISTING = 3;

        [DllImport("inpout32.dll", EntryPoint = "Out32")]
        private static extern void Output(int adress, int value);

        [DllImport("inpout32.dll", EntryPoint = "Inp32")]
        private static extern int Input(int adress);  


        private SafeFileHandle TicketPrinterHandle = null;
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern SafeFileHandle CreateFile(
            string lpFileName, 
            uint dwDesiredAccess,
            uint dwShareMode, 
            IntPtr lpSecurityAttributes, 
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes, 
            IntPtr hTemplateFile);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool WriteFile(
         SafeFileHandle hFile,                  // handle to file
         char[] lpBuffer,                       // data buffer
         int nNumberOfBytesToWrite,             // number of bytes to write
         out int lpNumberOfBytesWritten,        // number of bytes written
         IntPtr lpOverlapped          // overlapped buffer
         );

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool CloseHandle(
         SafeFileHandle hObject   // handle to object
         );


        private string _PPort = "";
        public string Status = "正在检测...";

        public TicketPrinter()
        {

        }

        public void Create( string LPTName )
        {
            _PPort = "";
            TicketPrinterHandle = CreateFile(LPTName, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
            if (TicketPrinterHandle.IsInvalid)
            {
                MessageBox.Show("票据打印机口不能打开!");
                return;
            }
            _PPort = LPTName;
        }

        public void Close()
        {
            if (TicketPrinterHandle == null)
                return;
            if (TicketPrinterHandle.IsInvalid)
                return;
            CloseHandle(TicketPrinterHandle);
        }

        public void Print(string str)
        {
            Print(str, "left");
        }

        public void Print(string str, string align)
        {
            if (TicketPrinterHandle == null)
                return;
            if (TicketPrinterHandle.IsInvalid)
                return;
            int PadCount = 30;
            PadCount -= StrLen( str );
            if (align == "center")
                str = str.PadLeft(PadCount / 2 + str.Length, ' ');
            else if (align == "right")
                str = str.PadLeft(PadCount, ' ');

            //
            //  7     6      5      4       3       2       1       0
            // Busy  nAck Paper-Out Select nError   x       x       x
            //
            //check Parallel port Status registers 
            int RetryCount = 10;
            Retry:
            int sta = 0;
            if (_PPort == "LPT1")
                sta = Input(0x379);
            else if (_PPort == "LPT2")
                sta = Input(0x279);

            if( sta == 0x7F )
            {
                Status = "未连接";
                return;
            }
            if ((sta & 0x10) == 0)
            {
                Status = "未上电";
                return;
            }
            if ((sta & 0x80) == 0)      //Busy
            {
                Status = "打印机忙";
                RetryCount--;
                if (RetryCount <= 0)
                    return;
                goto Retry;
            }
            if ((sta & 0x20) != 0)      //Paper-Out
            {
                Status = "打印机缺纸";
                return;
            }

           
            int count = 0;
            WriteFile(TicketPrinterHandle, str.ToCharArray(), StrLen(str), out count, IntPtr.Zero);
            Status = "正常";
        }

        public void PrintLn(string str)
        {
            Print(str, "left");
            Print("/r/n", "left");
        }

        public void PrintLn(string str,string align)
        {
            Print(str, align);
            Print("/r/n","left");
        }

        private int StrLen(string str)
        {
            return System.Text.Encoding.Default.GetByteCount(str);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值