using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace ITE.Windows.Common { public class BarCodePrint { //并口打印 [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); } private string BaseBarCodeString() { StringBuilder BarSb = new StringBuilder(); BarSb.Append("Q240,019");//设置标签长度和间距, 长度=240dots=30mm,间距=24dots=3mm BarSb.Append("\n\r"); BarSb.Append("q1020");//使用此命令设置可打印区域的宽度,该命令带参数 参数为正整数 BarSb.Append("\n\r"); BarSb.Append("rN"); BarSb.Append("\n\r"); BarSb.Append("S4");//使用此命令来设置打印速度,该命令带参数 参数一般0~6 BarSb.Append("\n\r"); BarSb.Append("D15");//使用此命令来设定打印浓度,该命令带参数 参数一般0~15 BarSb.Append("\n\r"); BarSb.Append("ZB");//ZB命令调整打印方向从左上开始,ZT打印方向从右下开始 BarSb.Append("\n\r"); BarSb.Append("JB"); BarSb.Append("\n\r"); BarSb.Append("O"); BarSb.Append("\n\r"); BarSb.Append("R16,0"); BarSb.Append("\n\r"); BarSb.Append("N"); BarSb.Append("\n\r"); BarSb.Append("{0}"); BarSb.Append("\n\r"); BarSb.Append("P1");//P1<回车> 打印,1为打印数量 BarSb.Append("\n\r"); return BarSb.ToString(); } public string GetStorageNoBarCodeCommandString(string stockCode,string productCode,string wight) { StringBuilder BarSb = new StringBuilder(); BarSb.Append("A60,45,0,2,2,1,N,\"" + stockCode + "\""); BarSb.Append("\n\r"); BarSb.Append("A100,165,0,4,1,1,N,\"" + productCode + "\""); BarSb.Append("\n\r"); BarSb.Append("A150,190,0,4,1,1,N,\"" + wight + "\"");//打印文本内容 BarSb.Append("\n\r"); BarSb.Append("B240,80,0,3,3,6,90,N,\"" + stockCode + "\"");//打印条码 /* B240,80,0,E30,3,6,90,B,"123456789012"<回车> * 例范围为1-9;参数N为普通打印,如为R则为反转打印;“1234567890123”为打印内容 打印条码,240,80为X,Y坐标点; 0为条码方向正向; E30为EAN13码; 3,6为条码的细条宽度,粗条宽度; 90为条码高度;B表示打印条码下方字符;“123456789012”为打印内容,注意EAN13码最后一位自动生成 */ return string.Format(BaseBarCodeString(), BarSb.ToString()); } } }
如果打印机用的是USB口的,要将lpt口映射成usb的,
NET USE LPT1 \\192.168.1.111\Printer /Persistent:YES
192.168.1.111---目标机器IP
Printer----目标机器打印机共享名
EPL2命令说明
EPL2严格区分大小写
A
语法 Ap1,p2,p3,p4,p5,p6,p7,“DATA”
参数 p1=以象素点为单位的水平起始位置
p2=以象素点为单位的垂直起始位置
p3=旋转
Value Description
0 No rotation
1 90 degrees
2 180 degrees
3 270 degrees
4 No rotation
5 90 degrees
6 180 degrees
7 270 degrees
4-7是针对亚洲字体的设置
p4=字体选择
Value Description
203 dpi 300 dpi
1 20.3 cpi, 6 pts, 25 cpi, 4 pts,
(8 x 12 dots) (12 x 20 dots)
2 16.9 cpi, 7 pts, 18.75 cpi, 6 pts,
(10 x 16 dots) (16 x 28 dots)
3 14.5 cpi, 10 pts, 15 cpi, 8 pts,
(12 x 20 dots) (20 x 36 dots)
4 12.7 cpi, 12 pts, 12.5 cpi, 10 pts,
(14 x 24 dots) (24 x 44 dots)
5 5.6 cpi, 24 pts, 6.25 cpi, 21 pts,
(32 x 48 dots) (48 x 80 dots)
6 Numeric Only Numeric Only
(14 x 19 dots) (14 x 19 dots)
7 Numeric Only Numeric Only
(14 x 19 dots) (14 x 19 dots)
亚洲打印机
Simplified Chinese, Japanese, Korean
8 203 dpi fonts : 24 x 24 dots
300 dpi Double-byte fonts : 36 x 36 dots
300 dpi Single-byte fonts : 24 x 36 dots
Traditional Chinese, Japanese
9 300 dpi Double-byte fonts: 36 x 36 dots
300 dpi Single-byte fonts : 24 x 36 dots
Korean - Reserved
p5=水平膨胀,值:1-6&8
p6=垂直膨胀,值:1-9
p7=N代表普通,R表示反转图象
"DATA"=数据
B
用于打印标准条码-----上面的例子是根据这个标准来的
语法:Bp1,p2,p3,p4,p5,p6,p7,p8,“DATA”
参数 p1=以象素点为单位的水平起始位置
p2=以象素点为单位的垂直起始位置
p3=旋转
Value Description
0 No rotation
1 90 degrees
2 180 degrees
3 270 degrees
p4=条码选择,参见条码表
p5=窄条的象数宽度,参见条码表
p6=宽条的象数宽度,参见条码表
p7=条码的象素高度
p8=打印可读代码,值 B=是,N=否
"DATA"=数据
条码表
参考