ZPL2Command.cs 15 KB

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