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);
}
}
}