ZPL2Command.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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;
  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="data">数据内容</param>
  330. protected string getFieldDataCommandBQ(string data)
  331. {
  332. //return "^FD" + data + "^FS" + endFlag;
  333. return "^BQ,2,10^FDLA," + data + "^FS" + endFlag;
  334. }
  335. /// <summary>
  336. /// 在指定坐标填充数据
  337. /// </summary>
  338. /// <param name="x">X坐标</param>
  339. /// <param name="y">Y坐标</param>
  340. /// <param name="data">填充的数据</param>
  341. /// <returns></returns>
  342. protected string fillDataCmd(int x, int y, string data)
  343. {
  344. return getPositionCommand(x, y) + getFieldDataCommand(data);
  345. }
  346. /// <summary>
  347. /// 在指定坐标填充二维码数据
  348. /// </summary>
  349. /// <param name="x">X坐标</param>
  350. /// <param name="y">Y坐标</param>
  351. /// <param name="data">填充的数据</param>
  352. /// <returns></returns>
  353. protected string fillBQDataCmd(int x, int y, string data)
  354. {
  355. return getPositionCommand(x, y) + getFieldDataCommandBQ(data);
  356. }
  357. protected int getStringDots(string data)
  358. {
  359. int len = 0;
  360. // int level = data.Length/5;
  361. len = (data.Length + data.Length / 3) * fontWidth;
  362. return len;
  363. }
  364. /// <summary>
  365. /// 获取条码指令
  366. /// </summary>
  367. /// <param name="x">X坐标</param>
  368. /// <param name="y">Y坐标</param>
  369. /// <param name="orientation"> 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)</param>
  370. /// <param name="data">条码数据项</param>
  371. protected string getBarcodeCmd(int x, int y, string orientation, string data)
  372. {
  373. string strCmd = "";
  374. strCmd += getPositionCommand(x, y);
  375. strCmd += "^BC"+ orientation+ "," + barcodeHeight.ToString() + ",Y,N,N" + endFlag; // code 128
  376. strCmd += getFieldDataCommand(data);
  377. return strCmd;
  378. }
  379. /// <summary>
  380. /// 添加条码指令
  381. /// </summary>
  382. /// <param name="x">X坐标</param>
  383. /// <param name="y">Y坐标</param>
  384. /// <param name="orientation"> 方向: N = 正常 (Normal);R = 顺时针旋转90度(Roated);I = 顺时针旋转180度(Inverted);B = 顺时针旋转270度 (Bottom)</param>
  385. /// <param name="data">条码数据项</param>
  386. protected string setBarcodeCmd(int x, int y, string orientation, string data)
  387. {
  388. strCmd += getPositionCommand(xPos, yPos);
  389. strCmd += "^BC" + orientation + "," + barcodeHeight.ToString() + ",Y,N,N" + endFlag; // code 128
  390. strCmd += getFieldDataCommand(data);
  391. return strCmd;
  392. }
  393. /// <summary>
  394. /// 获取打印图片指令
  395. /// </summary>
  396. /// <param name="fileName">文件名</param>
  397. /// <param name="mx">横坐标</param>
  398. /// <param name="my">纵坐标</param>
  399. /// <returns>打印指令字符串</returns>
  400. protected string getImageCommand(string fileName, int mx, int my)
  401. {
  402. string strCmd = "";
  403. strCmd = "^XGR:" + fileName + "," + mx + "," + my + "^FS";
  404. return strCmd;
  405. }
  406. /// <summary>
  407. /// 设置打印图片指令
  408. /// </summary>
  409. /// <param name="fileName">文件名</param>
  410. /// <param name="mx">横坐标</param>
  411. /// <param name="my">纵坐标</param>
  412. /// <returns>打印指令字符串</returns>
  413. protected void setImageCommand(string fileName, int mx, int my)
  414. {
  415. strCmd += "^XGR:" + fileName + "," + mx + "," + my + "^FS";
  416. }
  417. /// <summary>
  418. /// 设置字体指令
  419. /// </summary>
  420. /// <returns>打印指令字符串</returns>
  421. protected string getDefaultFontCmd()
  422. {
  423. string strCmd = "";
  424. strCmd += "^CF" + fontType + "," + fontHeight + "," + fontWidth + endFlag; //'^CFA,50,24';
  425. return strCmd;
  426. }
  427. /// <summary>
  428. /// 获取可缩放/点阵字体指令
  429. /// </summary>
  430. /// <returns></returns>
  431. protected string getFontCmd()
  432. {
  433. string cmd = "^A" + fontType + fontOrientation + "," + fontHeight + "," + fontWidth + endFlag; //'^AFR,50,24';
  434. return cmd;
  435. }
  436. /// <summary>
  437. /// 设置字体浓度
  438. /// </summary>
  439. /// <param name="darkness"></param>
  440. protected void setDarkness(int darkness)
  441. {
  442. strCmd += "^CM" +darkness.ToString()+endFlag;
  443. }
  444. protected void sendCommand(string strCmd)
  445. {
  446. sendMessage(Encoding.Default.GetBytes(strCmd));
  447. }
  448. }
  449. }