最近在研究pos机打印,找了几个版本的...保存下来供学习研究。
对pos机打印无非就是向打印机端口发送相应的数据,一般情况下都以文件流的形式来处理。
一种:是FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
iHandle为IntPtr类型,不过这种是不安全的,是过时的,还有一种是iHandle为safeFileHandle类型。下面分别为相对应的代码。
下面这是第一种的打印部分代码:
namespace POS
{
/// <summary>
/// POSPrinter 的摘要说明。
/// </summary>
public class POSPrinter
{
const int OPEN_EXISTING = 3;
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);
public POSPrinter(string prnPort)
{
this.prnPort = prnPort;//打印机端口
}
public string PrintLine(string str)
{
IntPtr iHandle = CreateFile(prnPort, 0x50000000, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
Console .WriteLine ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2");
Console.WriteLine(iHandle.ToString());
return "没有连接打印机或者打印机端口不是LPT1";
}
else
{
Console.WriteLine(iHandle.ToString());
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine();
sw.WriteLine(str);
sw.WriteLine("-------------------------------");
sw.WriteLine("{0,-8}{1,2}{2,5}{3,5}","商品名称","数量","单价","小计");
sw.WriteLine("---------------------------");
sw.WriteLine("-------"+DateTime .Now+"--------");
sw.WriteLine("-------感谢您的光临!--------");
sw.WriteLine(); sw.WriteLine(); sw.WriteLine(); sw.WriteLine(); sw.WriteLine();
sw.Close();
//fs.Close();
return "打印机连接成功";
}
}
}
}
下面这是第二种的打印部分代码:
namespace POS
{
/// <summary>
/// POSPrinter 的摘要说明。
/// </summary>
public class POSPrinter
{
string prnPort = "LPT1";
[DllImport("Kernel32.dll")]
static extern IntPtr CreateFile(string filename,[MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,
[MarshalAs(UnmanagedType.U4)]FileShare fileshare,int securityattributes,
[MarshalAs(UnmanagedType.U4)]FileMode creationdisposition,int flags,IntPtr template);
[DllImport("Kernel32.dll")]
static extern bool CloseHandle(IntPtr hObject);
public POSPrinter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public POSPrinter(string prnPort)
{
this.prnPort = prnPort;//打印机端口
}
public string PrintLine(string str)
{
SafeFileHandle sfh = null;
IntPtr ptr = CreateFile(prnPort, FileAccess.Write, FileShare.Write, 0, FileMode.Open, 0, IntPtr.Zero);
if (ptr.ToInt32() == -1)
{
Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2");
Console.WriteLine(ptr.ToString());
return "没有连接打印机或者打印机端口不是LPT1";
}
else
{
sfh = new SafeFileHandle(ptr, true);
StreamWriter sw = new StreamWriter(new FileStream(sfh, FileAccess.Write), System.Text.Encoding.Default);
sw.WriteLine(str);
sw.WriteLine("-------------------------------");
sw.WriteLine("{0,-8}{1,2}{2,5}{3,5}","商品名称","数量","单价","小计");
sw.WriteLine("-------"+DateTime .Now+"--------");
sw.WriteLine("-------感谢您的光临!--------");
sw.WriteLine(); sw.WriteLine(); sw.WriteLine(); sw.WriteLine(); sw.WriteLine();
sw.Close();
CloseHandle(ptr);
return "打印机连接成功";
}
}
}
}
方法调用:
切纸:
string instruction = null;
instruction = Convert.ToChar(27) + instruction + Convert.ToChar(100) + instruction + Convert.ToChar(0);
sw.WriteLine(instruction);
在输出结束加上该代码就可以完成打印完切纸功能。
还有就是打印对齐格式的设置问题,发现在打印时汉字,数字,字母和空格所占的纸张长度是不一样的,要想得到很美观整齐的打印效果还需量身而做,暂时还没发现什么好的解决方法,若有好方法的还望指教!