using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; namespace PrintSolution.LabelPrinter { public class ZPL2Command { /******************* 标签位置 ***********************/ int dotsMm = 12; // 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi string endFlag = "\r\n"; // 行指令结束符 /******************* 字体 ***********************/ protected char fontType; // 字体类型 A-Z,0-9(打印机的任何字体,包括下载字体,EPROM中储存的,当然这些字体必须用^CW来定义为A-Z,0-9) protected char fontOrientation; // // N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom) protected int fontHeight; // 字体高,单位 dot protected int fontWidth; // 字体宽, 单位 dot /******************* 条码 ***********************/ protected int barcodeHeight = 0; // 条码高度 /******************* 网络属性 ***********************/ string ipAddr = "10.10.76.210";// "10.10.76.212"; // IP 地址10.10.76.210 Int32 port = 12000; // 端口 /******************* 可变参数 ***********************/ string strCmd = ""; // 指令 int xPos = 0; // X坐标 int yPos = 0; // Y坐标 byte[] emptyCmd = Encoding.Default.GetBytes("^XA^XZ"); /// /// 构造函数 /// public ZPL2Command() { port = 9100; initCommand(); } /// /// 构造函数 /// /// IP 地址 public ZPL2Command(string addr) { ipAddr = addr; initNet(); initCommand(); //connect(); //if (thread != null) thread.Start(); } /// /// 初始化网络设置 /// protected void initNet() { port = 9100; } /// /// 变量初始化 /// public void initCommand() { dotsMm = 12; // 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi endFlag = "\r\n"; // strCmd = ""; // 指令 xPos = 0; // X坐标 yPos = 0; // Y坐标 // barcodeHeight = 150; // // 缺省字体设置 fontType = 'F'; fontHeight = 66; fontWidth = 26; fontOrientation = 'B'; } /// /// 打印机IP地址 /// public string Addr { get{ return ipAddr; } set { ipAddr = value; //connect(); //if (thread != null) thread.Start(); } } /// /// 设置和获取dpi属性 /// 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi /// public int dpi { get { int value; switch (dotsMm) { case 8: value = 200; break; case 12: value = 200; break; case 24: value = 200; break; default: value = 300; break; } return value; } set { switch (value) { case 200: dotsMm = 8; break; case 300: dotsMm = 12; break; case 600: dotsMm = 24; break; default: dotsMm = 12; break; } } } public char FontDirect { get { return fontOrientation; } set {fontOrientation = value;} } /// /// dotsMm 属性 /// public int getDotMM() { return dotsMm; } /// /// 打印机端口 /// public Int32 Port { get { return port; } set { port = value; } } public char Font { get { return fontType; } set { fontType = value; } } public int BarcodeHeight { get { return barcodeHeight; } set { barcodeHeight = value; } } /// /// 设置缺省字体 /// /// 字体类型A-Z 0-9 /// 高度; 单位mm /// 宽度; 单位mm public void setDefaultFont(char font, int height, int width, char orientation) { fontType = font; fontHeight = height; fontWidth = width; fontOrientation = orientation; strCmd += "^CF" + fontType + "," + fontHeight + "," + fontWidth + endFlag; //'^CFA,50,24'; } // 发送指令给打印机 public void sendMessage(byte[] data) { try { TcpClient tcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Parse(ipAddr), port); NetworkStream stream = tcpClient.GetStream(); stream.ReadTimeout = 5000; stream.WriteTimeout = 5000; stream.Write(data, 0, data.Length); stream.Close(); tcpClient.Close(); } catch (Exception ex) { string error = ex.Message; //m_alive = false; throw new Exception("数据发送失败"+ ex.Message); //showErrorMessage("数据发送失败!" + ex.Message); } } /// /// 打印开始指令 /// protected string getBeginCmd() { string strCmd = ""; strCmd += "^XA" + endFlag; return strCmd; } /// /// 打印结束指令 /// protected string getEndCmd() { string strCmd = ""; strCmd += "^XZ"+endFlag; return strCmd; } /// /// 重新尝试一次打印 /// protected bool printAgain() { sendMessage(Encoding.Default.GetBytes(strCmd)); return true; } /// /// 设置打印数据的位置 /// /// 横坐标位置 /// 纵坐标位置 protected string getPositionCommand(int xPos, int yPos) { return "^FO" + xPos.ToString() + "," + yPos.ToString() + endFlag; } /// /// 设置数据项 /// /// 数据内容 protected string getFieldDataCommand(string data) { return "^FD" + data + "^FS" + endFlag; } /// /// 在指定坐标填充数据 /// /// X坐标 /// Y坐标 /// 填充的数据 /// protected string fillDataCmd(int x, int y, string data) { return getPositionCommand(x, y) + getFieldDataCommand(data); } protected int getStringDots(string data) { int len = 0; // int level = data.Length/5; len = (data.Length + data.Length / 3) * fontWidth; return len; } /// /// 获取条码指令 /// /// X坐标 /// Y坐标 /// 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom) /// 条码数据项 protected string getBarcodeCmd(int x, int y, string orientation, string data) { string strCmd = ""; strCmd += getPositionCommand(x, y); strCmd += "^BC"+ orientation+ "," + barcodeHeight.ToString() + ",Y,N,N" + endFlag; // code 128 strCmd += getFieldDataCommand(data); return strCmd; } /// /// 添加条码指令 /// /// X坐标 /// Y坐标 /// 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom) /// 条码数据项 protected string setBarcodeCmd(int x, int y, string orientation, string data) { strCmd += getPositionCommand(xPos, yPos); strCmd += "^BC" + orientation + "," + barcodeHeight.ToString() + ",Y,N,N" + endFlag; // code 128 strCmd += getFieldDataCommand(data); return strCmd; } /// /// 获取打印图片指令 /// /// 文件名 /// 横坐标 /// 纵坐标 /// 打印指令字符串 protected string getImageCommand(string fileName, int mx, int my) { string strCmd = ""; strCmd = "^XGR:" + fileName + "," + mx + "," + my + "^FS"; return strCmd; } /// /// 设置打印图片指令 /// /// 文件名 /// 横坐标 /// 纵坐标 /// 打印指令字符串 protected void setImageCommand(string fileName, int mx, int my) { strCmd += "^XGR:" + fileName + "," + mx + "," + my + "^FS"; } /// /// 设置字体指令 /// /// 打印指令字符串 protected string getDefaultFontCmd() { string strCmd = ""; strCmd += "^CF" + fontType + "," + fontHeight + "," + fontWidth + endFlag; //'^CFA,50,24'; return strCmd; } /// /// 获取可缩放/点阵字体指令 /// /// protected string getFontCmd() { string cmd = "^A" + fontType + fontOrientation + "," + fontHeight + "," + fontWidth + endFlag; //'^AFR,50,24'; return cmd; } /// /// 设置字体浓度 /// /// protected void setDarkness(int darkness) { strCmd += "^CM" +darkness.ToString()+endFlag; } protected void sendCommand(string strCmd) { sendMessage(Encoding.Default.GetBytes(strCmd)); } } }