C# 使用EPL语言通过并口打印条码

通用并口打印类
 public class BarCodePrint
    {
        //并口打印

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern int CreateFile(
        string lpFileName,
        uint dwDesiredAccess,
        int dwShareMode,
        int lpSecurityAttributes,
        int dwCreationDisposition,
        int dwFlagsAndAttributes,
        int hTemplateFile
        );

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool WriteFile(
        int hFile,
        byte[] lpBuffer,
        int nNumberOfBytesToWrite,
        out   int lpNumberOfBytesWritten,
        out   OVERLAPPED lpOverlapped
        );

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool CloseHandle(
        int hObject
        );

        private int iHandle;

        public bool Open()
        {
            iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 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 tdbgArriveNoticeDetail_ButtonClick(object sender, ColEventArgs e)
        {
            if (e.Column.Name == "条码打印")
            {
                string strBarCode = tdbgArriveNoticeDetail.Columns["BarCode"].Text;
                string strMNo = tdbgArriveNoticeDetail.Columns["MNo"].Text;
                decimal decQty = Math.Round(Convert.ToDecimal(tdbgArriveNoticeDetail.Columns["Qty"].Text), 2);
                string strQty = decQty.ToString();
                string cmd = "N" + "\r\n" +
                    "B10,20,0,1,2,2,100,B,\"" + strBarCode + "\"" + "\r\n" +
                    "A10,160,0,8,1,1,N,\"料号:\"" + "\r\n" +
                    "A100,170,0,4,1,1,N,\"" + strMNo + "\"" + "\r\n" +
                    "A10,200,0,8,1,1,N,\"数量:\"" + "\r\n" +
                    "A100,210,0,4,1,1,N,\"" + strQty + "\"" + "\r\n" +
                    "P1" + "\r\n";

                BarCodePrint printer = new BarCodePrint();
                if (!printer.Open())
                {
                    MessageBox.Show("未能连接打印机,请确认打印机是否安装正确并接通电源。");
                    return;
                }
                printer.Write(cmd);
                if (!printer.Close())
                {
                    MessageBox.Show("未能关闭与打印机之间的连接,这可能意味着严重的错误,请重启电脑及打印机。");
                    return;
                }

            }
        }

 cmd就是用EPL语言写的打印字符串

你可能感兴趣的:(exception,String,cmd,C#,语言,byte)