| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- 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");
- /// <summary>
- /// 构造函数
- /// </summary>
- public ZPL2Command()
- {
- port = 9100;
- initCommand();
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="addr">IP 地址</param>
- public ZPL2Command(string addr)
- {
- ipAddr = addr;
- initNet();
- initCommand();
- //connect();
- //if (thread != null) thread.Start();
- }
- /// <summary>
- /// 初始化网络设置
- /// </summary>
- protected void initNet()
- {
- port = 9100;
- }
-
-
- /// <summary>
- /// 变量初始化
- /// </summary>
- 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';
- }
- /// <summary>
- /// 打印机IP地址
- /// </summary>
- public string Addr
- {
- get{
- return ipAddr;
- }
- set
- {
- ipAddr = value;
- //connect();
- //if (thread != null) thread.Start();
- }
- }
- /// <summary>
- /// 设置和获取dpi属性
- /// 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi
- /// </summary>
- 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;}
- }
- /// <summary>
- /// dotsMm 属性
- /// </summary>
- public int getDotMM()
- {
- return dotsMm;
- }
- /// <summary>
- /// 打印机端口
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 设置缺省字体
- /// </summary>
- /// <param name="font">字体类型A-Z 0-9 </param>
- /// <param name="height">高度; 单位mm</param>
- /// <param name="width">宽度; 单位mm</param>
- 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);
- }
- }
- /// <summary>
- /// 打印开始指令
- /// </summary>
- protected string getBeginCmd()
- {
- string strCmd = "";
- strCmd += "^XA" + endFlag;
- return strCmd;
- }
- /// <summary>
- /// 打印结束指令
- /// </summary>
- protected string getEndCmd()
- {
- string strCmd = "";
- strCmd += "^XZ"+endFlag;
- return strCmd;
- }
- /// <summary>
- /// 重新尝试一次打印
- /// </summary>
- protected bool printAgain()
- {
- sendMessage(Encoding.Default.GetBytes(strCmd));
- return true;
- }
- /// <summary>
- /// 设置打印数据的位置
- /// </summary>
- /// <param name="xPos">横坐标位置</param>
- /// <param name="yPos">纵坐标位置</param>
- protected string getPositionCommand(int xPos, int yPos)
- {
- return "^FO" + xPos.ToString() + "," + yPos.ToString() + endFlag;
- }
- /// <summary>
- /// 设置数据项
- /// </summary>
- /// <param name="data">数据内容</param>
- protected string getFieldDataCommand(string data)
- {
- return "^FD" + data + "^FS" + endFlag;
- }
- /// <summary>
- /// 在指定坐标填充数据
- /// </summary>
- /// <param name="x">X坐标</param>
- /// <param name="y">Y坐标</param>
- /// <param name="data">填充的数据</param>
- /// <returns></returns>
- 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;
-
- }
-
- /// <summary>
- /// 获取条码指令
- /// </summary>
- /// <param name="x">X坐标</param>
- /// <param name="y">Y坐标</param>
- /// <param name="orientation"> 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)</param>
- /// <param name="data">条码数据项</param>
- 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;
- }
- /// <summary>
- /// 添加条码指令
- /// </summary>
- /// <param name="x">X坐标</param>
- /// <param name="y">Y坐标</param>
- /// <param name="orientation"> 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)</param>
- /// <param name="data">条码数据项</param>
- 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;
- }
- /// <summary>
- /// 获取打印图片指令
- /// </summary>
- /// <param name="fileName">文件名</param>
- /// <param name="mx">横坐标</param>
- /// <param name="my">纵坐标</param>
- /// <returns>打印指令字符串</returns>
- protected string getImageCommand(string fileName, int mx, int my)
- {
- string strCmd = "";
- strCmd = "^XGR:" + fileName + "," + mx + "," + my + "^FS";
- return strCmd;
- }
- /// <summary>
- /// 设置打印图片指令
- /// </summary>
- /// <param name="fileName">文件名</param>
- /// <param name="mx">横坐标</param>
- /// <param name="my">纵坐标</param>
- /// <returns>打印指令字符串</returns>
- protected void setImageCommand(string fileName, int mx, int my)
- {
- strCmd += "^XGR:" + fileName + "," + mx + "," + my + "^FS";
- }
- /// <summary>
- /// 设置字体指令
- /// </summary>
- /// <returns>打印指令字符串</returns>
- protected string getDefaultFontCmd()
- {
- string strCmd = "";
- strCmd += "^CF" + fontType + "," + fontHeight + "," + fontWidth + endFlag; //'^CFA,50,24';
- return strCmd;
- }
- /// <summary>
- /// 获取可缩放/点阵字体指令
- /// </summary>
- /// <returns></returns>
- protected string getFontCmd()
- {
- string cmd = "^A" + fontType + fontOrientation + "," + fontHeight + "," + fontWidth + endFlag; //'^AFR,50,24';
- return cmd;
- }
- /// <summary>
- /// 设置字体浓度
- /// </summary>
- /// <param name="darkness"></param>
- protected void setDarkness(int darkness)
- {
- strCmd += "^CM" +darkness.ToString()+endFlag;
- }
- protected void sendCommand(string strCmd)
- {
- sendMessage(Encoding.Default.GetBytes(strCmd));
- }
-
- }
- }
|