亲宝软件园·资讯

展开

C#实现EPL II格式打印

springsnow 人气:0

一、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
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格式打印与打印测试的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论