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"=數據
條碼錶