C#实现EPL II格式打印与打印测试
作者:springsnow
这篇文章介绍了C#实现EPL II格式打印与打印测试的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
一、EPL II 格式及打印测试
注意N命令前的换行和最后P1后的换行。将此段代码复制到windows记事本里另存为Print.ext,文件名随便,后缀为ext。然后通过cmd控制命令行输入"copy c:\print.ext COM1”。然后就可以看到打印机开始打印了,效果不错。
N B300,5,0,1,2,3,40,N,"1234567891123" A160,55,0,8,1,1,N,"ALP-122244444444" b420,100,Q,"DATAdfdgdfgdf" P1
- 说明书上说N命令开始前要先输入一行空白行,
原文:Always send a Line Feed (LF) prior to the N command to ensure that previous data in the command buffer has cleared and the printer is in i tial ized and ready to ac cept com mands. - P命令之后也要加一个空白行,即回车,不然最后的P命令要打印的东西是不打印的,之前的P命令由于有后面的命令,间接等于换行了,所以会照打不误。
- 说明书上写P命令是Pp1, [p2],其参数p1是Number of label sets,而参数p2是Number of copies of each label (used in combination with counters to print multiple copies of the same label).
但我试验下来p1就是要打印的条码的重复次数,而p2无论我怎么改写都没有变换,始终打印最后一个条码。应该是我哪里写错了。但是多用几次P1,每次打一条还是可以起到同样的打印多条条码的效果的。
下面,给出我打印的条码的例子:
N B60,5,0,1,2,3,40,N,"1234567891123" A60,55,0,8,1,1,N,"ALP-1" P1 B60,5,0,1,2,3,40,N,"1234567890123" A60,55,0,8,1,1,N,"ALP-2" P1 B60,5,0,1,2,3,40,N,"1234567890123" A60,55,0,8,1,1,N,"ALP-3" P1
发送到打印机测试:
NET USE LPT1 /DELETE NET USE LPT1 \\192.168.2.166\zetl /persistent:yes copy c:\1.txt lpt1 copy c:\1.txt \\192.168.2.167\zetl
二、使用API发送内容进行打印:
private void button1_Click_1(object sender, EventArgs e) { string txt = textBox1.Text; LPTControl lpt = new LPTControl(); //string cmd = System.IO.File.ReadAllText("zplII.txt");//zplII.txt里写了条码机的命令 string cmd = Environment.NewLine + "N" + Environment.NewLine + "B60,5,0,1,2,3,40,N,\"1234567891123\"" + Environment.NewLine + "A60,55,0,8,1,1,N,\"ALP-5\"" + Environment.NewLine + "P1" + Environment.NewLine + "B60,5,0,1,2,3,40,N,\"1234567891123\"" + Environment.NewLine + "A60,55,0,8,1,1,N,\"ALP-6\"" + Environment.NewLine + "P1" + Environment.NewLine + "B60,5,0,1,2,3,40,N,\"1234567891123\"" + Environment.NewLine + "A60,55,0,8,1,1,N,\"ALP-7\"" + Environment.NewLine + "P1" + Environment.NewLine; if (!lpt.Open()) { throw new Exception("未能连接打印机,请确认打印机是否安装正确并接通电源。"); } lpt.Write(cmd); if (!lpt.Close()) { if (!lpt.Open()) { throw new Exception("未能连接打印机,请确认打印机是否安装正确并接通电源。"); } } } public class LPTControl { [StructLayout(LayoutKind.Sequential)] private struct OVERLAPPED { int Internal; int InternalHigh; int Offset; int OffSetHigh; int hEvent; } [DllImport("kernel32.dll")] private static extern int CreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile ); [DllImport("kernel32.dll")] private static extern bool WriteFile( int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped ); [DllImport("kernel32.dll")] private static extern bool CloseHandle( int hObject ); private int iHandle; public bool Open() { iHandle = CreateFile("lpt1:", 0x40000000, 0, 0, 3, 0, 0); if (iHandle != -1) { return true; } else { return false; } } public bool Write(String Mystring) { if (iHandle != -1) { int i; OVERLAPPED x; byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring); return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x); } else { throw new Exception("端口未打开!"); } } public bool Close() { return CloseHandle(iHandle); } }
到此这篇关于C#实现EPL II格式打印与打印测试的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。