公司在做一项目,需要用到POS打印,找了一个类,可以用,但是某些POS机自带的打印机没法用。打印没反映,或者提示没有发现打印机,可是明明安装这打印机,用的类是下边这个, string prnPort = "LPT1";
//[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
//private static extern IntPtr CreateFile(string lpFileName,
//int dwDesiredAccess,
//int dwShareMode,
//int lpSecurityAttributes,
//int dwCreationDisposition,
//int dwFlagsAndAttributes,
//int hTemplateFile);
[DllImport("kernel32.dll")] private static extern IntPtr CreateFile( String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile );
[DllImport("Kernel32.dll")] private static extern bool ReadFile( IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, IntPtr lpOverlapped );
[DllImport("Kernel32.dll")] private static extern bool WriteFile( IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, ref uint lpNumberOfBytesWritten, IntPtr lpOverlapped );
[DllImport("kernel32.dll")] private static extern bool CloseHandle( IntPtr hObject );
private const UInt32 GENERIC_READ = 0x80000000; private const UInt32 GENERIC_WRITE = 0x40000000; private const UInt32 OPEN_EXISTING = 3; private const Int32 INVALID_HANDLE_VALUE = -1; private const int USB_WRITENUM = 8; private const int USB_READNUM = 8;
public string PrintLine(string str)
{
IntPtr iHandle = CreateFile(prnPort, GENERIC_WRITE, 0, IntPtr.Zero,OPEN_EXISTING,0,IntPtr.Zero);
iHandle = CreateFile(prnPort, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (iHandle.ToInt32() == -1)
{
return "没有连接打印机或者打印机端口设置不正确。";
}
else
{ FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
sw.WriteLine(str);
//开钱箱
//sw.WriteLine(Chr(&H1B) & Chr(70) & Chr(0) & Chr(20) & Chr(&HA0))
sw.Close();
fs.Close();
return "";
}
}我想找个通用的,尽量能兼容更多版本的类,但是一直没找到,希望大哥大姐们能帮忙。小弟不胜感激。在网上也找了一个是北洋的Sdk改的,但是小弟试了半天还是不能用。http://blog.csdn.net/afeng124/archive/2010/05/20/5610614.aspx
//[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
//private static extern IntPtr CreateFile(string lpFileName,
//int dwDesiredAccess,
//int dwShareMode,
//int lpSecurityAttributes,
//int dwCreationDisposition,
//int dwFlagsAndAttributes,
//int hTemplateFile);
[DllImport("kernel32.dll")] private static extern IntPtr CreateFile( String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile );
[DllImport("Kernel32.dll")] private static extern bool ReadFile( IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, IntPtr lpOverlapped );
[DllImport("Kernel32.dll")] private static extern bool WriteFile( IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, ref uint lpNumberOfBytesWritten, IntPtr lpOverlapped );
[DllImport("kernel32.dll")] private static extern bool CloseHandle( IntPtr hObject );
private const UInt32 GENERIC_READ = 0x80000000; private const UInt32 GENERIC_WRITE = 0x40000000; private const UInt32 OPEN_EXISTING = 3; private const Int32 INVALID_HANDLE_VALUE = -1; private const int USB_WRITENUM = 8; private const int USB_READNUM = 8;
public string PrintLine(string str)
{
IntPtr iHandle = CreateFile(prnPort, GENERIC_WRITE, 0, IntPtr.Zero,OPEN_EXISTING,0,IntPtr.Zero);
iHandle = CreateFile(prnPort, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (iHandle.ToInt32() == -1)
{
return "没有连接打印机或者打印机端口设置不正确。";
}
else
{ FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
sw.WriteLine(str);
//开钱箱
//sw.WriteLine(Chr(&H1B) & Chr(70) & Chr(0) & Chr(20) & Chr(&HA0))
sw.Close();
fs.Close();
return "";
}
}我想找个通用的,尽量能兼容更多版本的类,但是一直没找到,希望大哥大姐们能帮忙。小弟不胜感激。在网上也找了一个是北洋的Sdk改的,但是小弟试了半天还是不能用。http://blog.csdn.net/afeng124/archive/2010/05/20/5610614.aspx
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货