Special_RePrintRFID.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using UAS_MES_NEW.DataOperate;
  9. using UAS_MES_NEW.Entity;
  10. using UAS_MES_NEW.PublicMethod;
  11. using DEV_HDL = System.UInt32;
  12. using LABEL_TEMP_HDL = System.UInt32;
  13. namespace UAS_MES_NEW.Special
  14. {
  15. public partial class Special_RePrintRFID : Form
  16. {
  17. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  18. public static extern UInt32 DSTP2x_Lib_Init(string pSzInitInfo, Int32 nInitInfoLen, StringBuilder pSzResult, ref int pResultLen);
  19. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  20. public static extern UInt32 DSTP2x_Lib_Clear();
  21. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  22. public static extern UInt32 DSTP2x_EnumDev(Int32 nEnumType, byte[] szEnumList, ref int pDevSize, ref int pDevNum);
  23. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  24. public static extern UInt32 DSTP2x_ConnEnumeratedDev(string szDevName, ref DEV_HDL pDevHdl);
  25. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  26. public static extern UInt32 DSTP2x_DisconnDev(DEV_HDL ullDevHdl);
  27. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  28. public static extern UInt32 DSTP2x_SetPrnEmulation(DEV_HDL ullDevHdl, Int32 nEmulation);
  29. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  30. public static extern UInt32 DSTP2x_SetImgDpi(DEV_HDL ullDevHdl, Int32 nDpi);
  31. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  32. public static extern UInt32 DSTP2x_LoadLabelTmpl(string szFileName, ref LABEL_TEMP_HDL pLTHdl);
  33. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  34. public static extern UInt32 DSTP2x_PrintTmpl(DEV_HDL ullDevHdl, LABEL_TEMP_HDL ullLTHdl, byte[] szOutFile, ref int pOutFileSize, byte[] szOutRFID, ref int pOutRFIDSize);
  35. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  36. public static extern UInt32 DSTP2x_DeleteTmpl(LABEL_TEMP_HDL ullLTHdl);
  37. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  38. public static extern UInt32 DSTP2x_SetTmplPrnMode(LABEL_TEMP_HDL ullLTHdl, Int32 nPrnMode);
  39. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  40. public static extern UInt32 DSTP2x_SetTmplPrnData(LABEL_TEMP_HDL ullLTHdl, string szElemID, string szActualData);
  41. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  42. public static extern UInt32 DSTP2x_SetTmplRFIDData(LABEL_TEMP_HDL ullLTHdl, string szElemID, string pActualData, Int32 nActualDataSize);
  43. DataHelper dh = SystemInf.dh;
  44. LogStringBuilder sql = new LogStringBuilder();
  45. DEV_HDL uRet = 0;
  46. DEV_HDL dev_prt = 0;
  47. LABEL_TEMP_HDL tmp_prt = 0;
  48. byte[] pEnumList = new byte[1024];
  49. int enumListLen = 1024;
  50. byte[] szOutFile = new byte[1024];
  51. int pOutFileSize = 1024;
  52. byte[] szOutRFID = new byte[1024];
  53. int pOutRFIDSize = 1024;
  54. int deviceNum = 0;
  55. string device = "";
  56. StringBuilder pSzResult = new StringBuilder(256);
  57. int pResultLen = 256;
  58. public Special_RePrintRFID()
  59. {
  60. InitializeComponent();
  61. }
  62. private void Clean_Click(object sender, EventArgs e)
  63. {
  64. OperateResult.Clear();
  65. }
  66. private void Special_CancelCollection_Load(object sender, EventArgs e)
  67. {
  68. }
  69. private void ms_sncode_KeyDown(object sender, KeyEventArgs e)
  70. {
  71. if (e.KeyCode == Keys.Enter)
  72. {
  73. DataTable dt = (DataTable)dh.ExecuteSql("select ms_sncode,ms_makecode,pr_code,pr_detail from makeserial left join product on pr_code=ms_prodcode where ms_sncode='" + ms_sncode.Text + "' order by ms_id desc", "select");
  74. if (dt.Rows.Count == 0)
  75. {
  76. OperateResult.AppendText("" + ms_sncode.Text + "不存在\n");
  77. return;
  78. }
  79. else
  80. {
  81. BaseUtil.SetFormValue(this.Controls, dt);
  82. }
  83. string tmpPath = PrintLabel.Text;
  84. uRet = DSTP2x_Lib_Init("", 0, pSzResult, ref pResultLen);
  85. if (uRet != 0)
  86. {
  87. OperateResult.AppendText("DSTP2x_Lib_Init,error code:[" + uRet.ToString() + "]");
  88. return;
  89. }
  90. uRet = DSTP2x_EnumDev(1, pEnumList, ref enumListLen, ref deviceNum); //USB connection
  91. if (uRet != 0)
  92. {
  93. OperateResult.AppendText("Enumerate device error,error code:[" + uRet.ToString() + "]");
  94. goto SAMPLE_END;
  95. }
  96. if (deviceNum > 1)
  97. {
  98. string str = Encoding.UTF8.GetString(pEnumList, 0, enumListLen);
  99. string[] enumList = str.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
  100. device = enumList[0];
  101. }
  102. else if (deviceNum == 1)
  103. device = Encoding.UTF8.GetString(pEnumList, 0, enumListLen);
  104. else
  105. {
  106. OperateResult.AppendText("No device");
  107. goto SAMPLE_END;
  108. }
  109. //3.Connect the device.
  110. uRet = DSTP2x_ConnEnumeratedDev(device, ref dev_prt);
  111. if (uRet != 0)
  112. {
  113. OperateResult.AppendText("Failed to connect device,error code:[" + uRet.ToString() + "]");
  114. goto SAMPLE_END;
  115. }
  116. //Non essential interface. 1-ZPL, 2-TSPL, 3-ESCPOS
  117. uRet = DSTP2x_SetPrnEmulation(dev_prt, 1); //ZPL
  118. if (uRet != 0)
  119. {
  120. OperateResult.AppendText("Failed to set up print simulation,error code:[" + uRet.ToString() + "]");
  121. goto SAMPLE_END;
  122. }
  123. //4.Load the template.
  124. uRet = DSTP2x_LoadLabelTmpl(tmpPath, ref tmp_prt);
  125. if (uRet != 0)
  126. {
  127. OperateResult.AppendText("Failed to load label template,error code:[" + uRet.ToString() + "]");
  128. goto SAMPLE_END;
  129. }
  130. //5.Set the print mode. 0-print, 1-generate the prn file, 2-generate the preview image.
  131. uRet = DSTP2x_SetTmplPrnMode(tmp_prt, 0); //set print mode
  132. if (uRet != 0)
  133. {
  134. OperateResult.AppendText("Setting whether to print failed,error code:[" + uRet.ToString() + "]");
  135. goto SAMPLE_END;
  136. }
  137. //6.Set the print data.
  138. uRet = DSTP2x_SetTmplPrnData(tmp_prt, "Text-01", ms_sncode.Text); //The data must be utf-8
  139. uRet = DSTP2x_SetTmplPrnData(tmp_prt, "Text-02", ms_sncode.Text); //The data must be utf-8
  140. if (uRet != 0)
  141. {
  142. OperateResult.AppendText("Failed to set someone data in template,error code:[" + uRet.ToString() + "]");
  143. goto SAMPLE_END;
  144. }
  145. //7.Set the RFID data.
  146. uRet = DSTP2x_SetTmplRFIDData(tmp_prt, "EPC-01", Hex.Checked ? AsciiToHex(ms_sncode.Text) : ms_sncode.Text, Hex.Checked ? AsciiToHex(ms_sncode.Text).Length : ms_sncode.Text.Length);
  147. if (uRet != 0)
  148. {
  149. OperateResult.AppendText("Failed to set EPC data in template,error code:[" + uRet.ToString() + "]");
  150. goto SAMPLE_END;
  151. }
  152. //uRet = DSTP2x_SetTmplRFIDData(tmp_prt, "USER-01", "123ABC", "123ABC".Length);
  153. //if (uRet != 0)
  154. //{
  155. // OperateResult.AppendText("Failed to set USER data in template,error code:[" + uRet.ToString() + "]");
  156. // goto SAMPLE_END;
  157. //}
  158. //8.Print template and get the RFID data.
  159. uRet = DSTP2x_PrintTmpl(dev_prt, tmp_prt, szOutFile, ref pOutFileSize, szOutRFID, ref pOutRFIDSize);
  160. if (uRet != 0)
  161. {
  162. OperateResult.AppendText("Print template failed,error code:[" + uRet.ToString() + "]");
  163. goto SAMPLE_END;
  164. }
  165. OperateResult.AppendText("The RFID data is " + Encoding.UTF8.GetString(szOutRFID, 0, pOutRFIDSize));
  166. //9.Delete the handle of template.
  167. uRet = DSTP2x_DeleteTmpl(tmp_prt);
  168. if (uRet != 0)
  169. {
  170. OperateResult.AppendText("Delete handle of template failed,error code:[" + uRet.ToString() + "]");
  171. goto SAMPLE_END;
  172. }
  173. //10.Disconnect the device.
  174. uRet = DSTP2x_DisconnDev(dev_prt);
  175. if (uRet != 0)
  176. {
  177. OperateResult.AppendText("Delete handle of device failed,error code:[" + uRet.ToString() + "]");
  178. goto SAMPLE_END;
  179. }
  180. SAMPLE_END:
  181. {
  182. uRet = DSTP2x_Lib_Clear();
  183. }
  184. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "RFID补打印", "RFID打印成功", ms_sncode.Text, "");
  185. ms_sncode.Clear();
  186. }
  187. }
  188. static string AsciiToHex(string asciiString)
  189. {
  190. byte[] bytes = Encoding.ASCII.GetBytes(asciiString);
  191. return BitConverter.ToString(bytes).Replace("-", "").ToLower();
  192. }
  193. private void pr_code_TextChanged(object sender, EventArgs e)
  194. {
  195. DataTable dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='RFID' and la_statuscode='AUDITED' order by la_isdefault", "select");
  196. if (dt.Rows.Count == 0)
  197. {
  198. dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode is null and la_templatetype='RFID' and la_statuscode='AUDITED' order by la_isdefault", "select");
  199. }
  200. PrintLabel.DataSource = dt;
  201. PrintLabel.DisplayMember = "la_url";
  202. PrintLabel.ValueMember = "la_id";
  203. }
  204. }
  205. }