C#實現(xiàn)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],其參數(shù)p1是Number of label sets,而參數(shù)p2是Number of copies of each label (used in combination with counters to print multiple copies of the same label).
但我試驗下來p1就是要打印的條碼的重復次數(shù),而p2無論我怎么改寫都沒有變換,始終打印最后一個條碼。應(yīng)該是我哪里寫錯了。但是多用幾次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
發(fā)送到打印機測試:
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發(fā)送內(nèi)容進行打?。?/h2>
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);
}
}
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); } }
到此這篇關(guān)于C#實現(xiàn)EPL II格式打印與打印測試的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#把數(shù)組中的某個元素取出來放到第一個位置的實現(xiàn)方法
這篇文章主要介紹了C#把數(shù)組中的某個元素取出來放到第一個位置的實現(xiàn)方法,涉及C#針對數(shù)組的常見操作技巧,非常具有實用價值,需要的朋友可以參考下2014-12-12C#如何給新建的winform程序添加資源文件夾Resources
這篇文章主要介紹了C#如何給新建的winform程序添加資源文件夾Resources,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09在C#中如何使用正式表達式獲取匹配所需數(shù)據(jù)
本文給大家分享C#中如何使用正式表達式獲取匹配所需數(shù)據(jù) ,非常實用,對正則表達式獲取匹配相關(guān)知識感興趣的朋友一起學習吧2016-03-03C#使用Socket實現(xiàn)服務(wù)器與多個客戶端通信(簡單的聊天系統(tǒng))
這篇文章主要介紹了C#使用Socket實現(xiàn)服務(wù)器與多個客戶端通信(簡單的聊天系統(tǒng)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02