ZPL2Command.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. namespace PrintSolution.LabelPrinter
  7. {
  8. public class ZPL2Command
  9. {
  10. /******************* 标签位置 ***********************/
  11. int dotsMm = 12; // 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi
  12. string endFlag = "\r\n"; // 行指令结束符
  13. /******************* 字体 ***********************/
  14. protected char fontType; // 字体类型 A-Z,0-9(打印机的任何字体,包括下载字体,EPROM中储存的,当然这些字体必须用^CW来定义为A-Z,0-9)
  15. protected char fontOrientation; // // N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)
  16. protected int fontHeight; // 字体高,单位 dot
  17. protected int fontWidth; // 字体宽, 单位 dot
  18. /******************* 条码 ***********************/
  19. protected int barcodeHeight = 0; // 条码高度
  20. /******************* 网络属性 ***********************/
  21. string ipAddr = "10.10.76.210";// "10.10.76.212"; // IP 地址10.10.76.210
  22. Int32 port = 12000; // 端口
  23. /******************* 可变参数 ***********************/
  24. string strCmd = ""; // 指令
  25. int xPos = 0; // X坐标
  26. int yPos = 0; // Y坐标
  27. byte[] emptyCmd = Encoding.Default.GetBytes("^XA^XZ");
  28. /// <summary>
  29. /// 构造函数
  30. /// </summary>
  31. public ZPL2Command()
  32. {
  33. port = 9100;
  34. initCommand();
  35. }
  36. /// <summary>
  37. /// 构造函数
  38. /// </summary>
  39. /// <param name="addr">IP 地址</param>
  40. public ZPL2Command(string addr)
  41. {
  42. ipAddr = addr;
  43. initNet();
  44. initCommand();
  45. //connect();
  46. //if (thread != null) thread.Start();
  47. }
  48. /// <summary>
  49. /// 初始化网络设置
  50. /// </summary>
  51. protected void initNet()
  52. {
  53. port = 9100;
  54. }
  55. /// <summary>
  56. /// 变量初始化
  57. /// </summary>
  58. public void initCommand()
  59. {
  60. dotsMm = 12; // 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi
  61. endFlag = "\r\n";
  62. //
  63. strCmd = ""; // 指令
  64. xPos = 0; // X坐标
  65. yPos = 0; // Y坐标
  66. //
  67. barcodeHeight = 150; //
  68. // 缺省字体设置
  69. fontType = 'F';
  70. fontHeight = 66;
  71. fontWidth = 26;
  72. fontOrientation = 'B';
  73. }
  74. /// <summary>
  75. /// 打印机IP地址
  76. /// </summary>
  77. public string Addr
  78. {
  79. get{
  80. return ipAddr;
  81. }
  82. set
  83. {
  84. ipAddr = value;
  85. //connect();
  86. //if (thread != null) thread.Start();
  87. }
  88. }
  89. /// <summary>
  90. /// 设置和获取dpi属性
  91. /// 8/MM = 200 dpi, 12/MM = 300 dpi and 24/MM = 600 dpi
  92. /// </summary>
  93. public int dpi
  94. {
  95. get
  96. {
  97. int value;
  98. switch (dotsMm)
  99. {
  100. case 8:
  101. value = 200;
  102. break;
  103. case 12:
  104. value = 200;
  105. break;
  106. case 24:
  107. value = 200;
  108. break;
  109. default:
  110. value = 300;
  111. break;
  112. }
  113. return value;
  114. }
  115. set
  116. {
  117. switch (value)
  118. {
  119. case 200:
  120. dotsMm = 8;
  121. break;
  122. case 300:
  123. dotsMm = 12;
  124. break;
  125. case 600:
  126. dotsMm = 24;
  127. break;
  128. default:
  129. dotsMm = 12;
  130. break;
  131. }
  132. }
  133. }
  134. public char FontDirect
  135. {
  136. get { return fontOrientation; }
  137. set {fontOrientation = value;}
  138. }
  139. /// <summary>
  140. /// dotsMm 属性
  141. /// </summary>
  142. public int getDotMM()
  143. {
  144. return dotsMm;
  145. }
  146. /// <summary>
  147. /// 打印机端口
  148. /// </summary>
  149. public Int32 Port
  150. {
  151. get
  152. {
  153. return port;
  154. }
  155. set
  156. {
  157. port = value;
  158. }
  159. }
  160. public char Font
  161. {
  162. get
  163. {
  164. return fontType;
  165. }
  166. set
  167. {
  168. fontType = value;
  169. }
  170. }
  171. public int BarcodeHeight
  172. {
  173. get
  174. {
  175. return barcodeHeight;
  176. }
  177. set
  178. {
  179. barcodeHeight = value;
  180. }
  181. }
  182. /// <summary>
  183. /// 设置缺省字体
  184. /// </summary>
  185. /// <param name="font">字体类型A-Z 0-9 </param>
  186. /// <param name="height">高度; 单位mm</param>
  187. /// <param name="width">宽度; 单位mm</param>
  188. public void setDefaultFont(char font, int height, int width, char orientation)
  189. {
  190. fontType = font;
  191. fontHeight = height;
  192. fontWidth = width;
  193. fontOrientation = orientation;
  194. strCmd += "^CF" + fontType + "," + fontHeight + "," + fontWidth + endFlag; //'^CFA,50,24';
  195. }
  196. // 发送指令给打印机
  197. public void sendMessage(byte[] data)
  198. {
  199. try
  200. {
  201. TcpClient tcpClient = new TcpClient();
  202. tcpClient.Connect(IPAddress.Parse(ipAddr), port);
  203. NetworkStream stream = tcpClient.GetStream();
  204. stream.ReadTimeout = 5000;
  205. stream.WriteTimeout = 5000;
  206. stream.Write(data, 0, data.Length);
  207. stream.Close();
  208. tcpClient.Close();
  209. }
  210. catch (Exception ex)
  211. {
  212. string error = ex.Message;
  213. //m_alive = false;
  214. throw new Exception("数据发送失败"+ ex.Message); //showErrorMessage("数据发送失败!" + ex.Message);
  215. }
  216. }
  217. /// <summary>
  218. /// 打印开始指令
  219. /// </summary>
  220. protected string getBeginCmd()
  221. {
  222. string strCmd = "";
  223. strCmd += "^XA" + endFlag;
  224. return strCmd;
  225. }
  226. /// <summary>
  227. /// 打印结束指令
  228. /// </summary>
  229. protected string getEndCmd()
  230. {
  231. string strCmd = "";
  232. strCmd += "^XZ"+endFlag;
  233. return strCmd;
  234. }
  235. /// <summary>
  236. /// 重新尝试一次打印
  237. /// </summary>
  238. protected bool printAgain()
  239. {
  240. sendMessage(Encoding.Default.GetBytes(strCmd));
  241. return true;
  242. }
  243. /// <summary>
  244. /// 设置打印数据的位置
  245. /// </summary>
  246. /// <param name="xPos">横坐标位置</param>
  247. /// <param name="yPos">纵坐标位置</param>
  248. protected string getPositionCommand(int xPos, int yPos)
  249. {
  250. return "^FO" + xPos.ToString() + "," + yPos.ToString() + endFlag;
  251. }
  252. /// <summary>
  253. /// 设置数据项
  254. /// </summary>
  255. /// <param name="data">数据内容</param>
  256. protected string getFieldDataCommand(string data)
  257. {
  258. return "^FD" + data + "^FS" + endFlag;
  259. }
  260. /// <summary>
  261. /// 在指定坐标填充数据
  262. /// </summary>
  263. /// <param name="x">X坐标</param>
  264. /// <param name="y">Y坐标</param>
  265. /// <param name="data">填充的数据</param>
  266. /// <returns></returns>
  267. protected string fillDataCmd(int x, int y, string data)
  268. {
  269. return getPositionCommand(x, y) + getFieldDataCommand(data);
  270. }
  271. protected int getStringDots(string data)
  272. {
  273. int len = 0;
  274. // int level = data.Length/5;
  275. len = (data.Length + data.Length / 3) * fontWidth;
  276. return len;
  277. }
  278. /// <summary>
  279. /// 获取条码指令
  280. /// </summary>
  281. /// <param name="x">X坐标</param>
  282. /// <param name="y">Y坐标</param>
  283. /// <param name="orientation"> 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)</param>
  284. /// <param name="data">条码数据项</param>
  285. protected string getBarcodeCmd(int x, int y, string orientation, string data)
  286. {
  287. string strCmd = "";
  288. strCmd += getPositionCommand(x, y);
  289. strCmd += "^BC"+ orientation+ "," + barcodeHeight.ToString() + ",Y,N,N" + endFlag; // code 128
  290. strCmd += getFieldDataCommand(data);
  291. return strCmd;
  292. }
  293. /// <summary>
  294. /// 添加条码指令
  295. /// </summary>
  296. /// <param name="x">X坐标</param>
  297. /// <param name="y">Y坐标</param>
  298. /// <param name="orientation"> 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)</param>
  299. /// <param name="data">条码数据项</param>
  300. protected string setBarcodeCmd(int x, int y, string orientation, string data)
  301. {
  302. strCmd += getPositionCommand(xPos, yPos);
  303. strCmd += "^BC" + orientation + "," + barcodeHeight.ToString() + ",Y,N,N" + endFlag; // code 128
  304. strCmd += getFieldDataCommand(data);
  305. return strCmd;
  306. }
  307. /// <summary>
  308. /// 获取打印图片指令
  309. /// </summary>
  310. /// <param name="fileName">文件名</param>
  311. /// <param name="mx">横坐标</param>
  312. /// <param name="my">纵坐标</param>
  313. /// <returns>打印指令字符串</returns>
  314. protected string getImageCommand(string fileName, int mx, int my)
  315. {
  316. string strCmd = "";
  317. strCmd = "^XGR:" + fileName + "," + mx + "," + my + "^FS";
  318. return strCmd;
  319. }
  320. /// <summary>
  321. /// 设置打印图片指令
  322. /// </summary>
  323. /// <param name="fileName">文件名</param>
  324. /// <param name="mx">横坐标</param>
  325. /// <param name="my">纵坐标</param>
  326. /// <returns>打印指令字符串</returns>
  327. protected void setImageCommand(string fileName, int mx, int my)
  328. {
  329. strCmd += "^XGR:" + fileName + "," + mx + "," + my + "^FS";
  330. }
  331. /// <summary>
  332. /// 设置字体指令
  333. /// </summary>
  334. /// <returns>打印指令字符串</returns>
  335. protected string getDefaultFontCmd()
  336. {
  337. string strCmd = "";
  338. strCmd += "^CF" + fontType + "," + fontHeight + "," + fontWidth + endFlag; //'^CFA,50,24';
  339. return strCmd;
  340. }
  341. /// <summary>
  342. /// 获取可缩放/点阵字体指令
  343. /// </summary>
  344. /// <returns></returns>
  345. protected string getFontCmd()
  346. {
  347. string cmd = "^A" + fontType + fontOrientation + "," + fontHeight + "," + fontWidth + endFlag; //'^AFR,50,24';
  348. return cmd;
  349. }
  350. /// <summary>
  351. /// 设置字体浓度
  352. /// </summary>
  353. /// <param name="darkness"></param>
  354. protected void setDarkness(int darkness)
  355. {
  356. strCmd += "^CM" +darkness.ToString()+endFlag;
  357. }
  358. protected void sendCommand(string strCmd)
  359. {
  360. sendMessage(Encoding.Default.GetBytes(strCmd));
  361. }
  362. }
  363. }