Query_RFIDPrint.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using LabelManager2;
  2. using System;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using UAS_MES_NEW.DataOperate;
  9. using UAS_MES_NEW.Entity;
  10. using UAS_MES_NEW.PublicForm;
  11. using UAS_MES_NEW.PublicMethod;
  12. using DEV_HDL = System.UInt32;
  13. using LABEL_TEMP_HDL = System.UInt32;
  14. namespace UAS_MES_NEW.Query
  15. {
  16. public partial class Query_RFIDPrint : Form
  17. {
  18. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  19. public static extern UInt32 DSTP2x_Lib_Init(string pSzInitInfo, Int32 nInitInfoLen, StringBuilder pSzResult, ref int pResultLen);
  20. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  21. public static extern UInt32 DSTP2x_Lib_Clear();
  22. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  23. public static extern UInt32 DSTP2x_EnumDev(Int32 nEnumType, byte[] szEnumList, ref int pDevSize, ref int pDevNum);
  24. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  25. public static extern UInt32 DSTP2x_ConnEnumeratedDev(string szDevName, ref DEV_HDL pDevHdl);
  26. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  27. public static extern UInt32 DSTP2x_DisconnDev(DEV_HDL ullDevHdl);
  28. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  29. public static extern UInt32 DSTP2x_SetPrnEmulation(DEV_HDL ullDevHdl, Int32 nEmulation);
  30. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  31. public static extern UInt32 DSTP2x_SetImgDpi(DEV_HDL ullDevHdl, Int32 nDpi);
  32. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  33. public static extern UInt32 DSTP2x_LoadLabelTmpl(string szFileName, ref LABEL_TEMP_HDL pLTHdl);
  34. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  35. public static extern UInt32 DSTP2x_PrintTmpl(DEV_HDL ullDevHdl, LABEL_TEMP_HDL ullLTHdl, byte[] szOutFile, ref int pOutFileSize, byte[] szOutRFID, ref int pOutRFIDSize);
  36. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  37. public static extern UInt32 DSTP2x_DeleteTmpl(LABEL_TEMP_HDL ullLTHdl);
  38. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  39. public static extern UInt32 DSTP2x_SetTmplPrnMode(LABEL_TEMP_HDL ullLTHdl, Int32 nPrnMode);
  40. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  41. public static extern UInt32 DSTP2x_SetTmplPrnData(LABEL_TEMP_HDL ullLTHdl, string szElemID, string szActualData);
  42. [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  43. public static extern UInt32 DSTP2x_SetTmplRFIDData(LABEL_TEMP_HDL ullLTHdl, string szElemID, string pActualData, Int32 nActualDataSize);
  44. DataHelper dh = SystemInf.dh;
  45. ApplicationClass lbl;
  46. Document doc;
  47. Thread InitPrint;
  48. public Query_RFIDPrint()
  49. {
  50. InitializeComponent();
  51. }
  52. private void Query_SpecialReport_Load(object sender, EventArgs e)
  53. {
  54. DEV_HDL uRet = 0;
  55. StringBuilder pSzResult = new StringBuilder(256);
  56. int pResultLen = 256;
  57. //1.Initialize the Library.
  58. uRet = DSTP2x_Lib_Init("", 0, pSzResult, ref pResultLen);
  59. if (uRet != 0)
  60. {
  61. Console.WriteLine("DSTP2x_Lib_Init,error code:[" + uRet.ToString() + "]");
  62. return;
  63. }
  64. DEV_HDL dev_prt = 0;
  65. LABEL_TEMP_HDL tmp_prt = 0;
  66. byte[] pEnumList = new byte[1024];
  67. int enumListLen = 1024;
  68. byte[] szOutFile = new byte[1024];
  69. int pOutFileSize = 1024;
  70. byte[] szOutRFID = new byte[1024];
  71. int pOutRFIDSize = 1024;
  72. int deviceNum = 0;
  73. string device = "";
  74. string tmpPath = "../../samplecode/CSharp/TemplatePrint_RFID/templateRFID.dlt";
  75. string writeEPCData = "ABC123";
  76. string writeUSERData = "123ABC";
  77. //2.Enumerate devices. 1-USB, 2-NET
  78. uRet = DSTP2x_EnumDev(1, pEnumList, ref enumListLen, ref deviceNum); //USB connection
  79. if (uRet != 0)
  80. {
  81. Console.WriteLine("Enumerate device error,error code:[" + uRet.ToString() + "]");
  82. goto SAMPLE_END;
  83. }
  84. if (deviceNum > 1)
  85. {
  86. string str = Encoding.UTF8.GetString(pEnumList, 0, enumListLen);
  87. string[] enumList = str.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
  88. device = enumList[0];
  89. }
  90. else if (deviceNum == 1)
  91. device = Encoding.UTF8.GetString(pEnumList, 0, enumListLen);
  92. else
  93. {
  94. Console.WriteLine("No device");
  95. goto SAMPLE_END;
  96. }
  97. //3.Connect the device.
  98. uRet = DSTP2x_ConnEnumeratedDev(device, ref dev_prt);
  99. if (uRet != 0)
  100. {
  101. Console.WriteLine("Failed to connect device,error code:[" + uRet.ToString() + "]");
  102. goto SAMPLE_END;
  103. }
  104. //Non essential interface. 1-ZPL, 2-TSPL, 3-ESCPOS
  105. uRet = DSTP2x_SetPrnEmulation(dev_prt, 1); //ZPL
  106. if (uRet != 0)
  107. {
  108. Console.WriteLine("Failed to set up print simulation,error code:[" + uRet.ToString() + "]");
  109. goto SAMPLE_END;
  110. }
  111. //4.加在标签文件Load the template.
  112. uRet = DSTP2x_LoadLabelTmpl(tmpPath, ref tmp_prt);
  113. if (uRet != 0)
  114. {
  115. Console.WriteLine("Failed to load label template,error code:[" + uRet.ToString() + "]");
  116. goto SAMPLE_END;
  117. }
  118. //5.设置打印模式 0-打印 Set the print mode. 0-print, 1-generate the prn file, 2-generate the preview image.
  119. uRet = DSTP2x_SetTmplPrnMode(tmp_prt, 0); //set print mode
  120. if (uRet != 0)
  121. {
  122. Console.WriteLine("Setting whether to print failed,error code:[" + uRet.ToString() + "]");
  123. goto SAMPLE_END;
  124. }
  125. //6.设置打印数据 Set the print data.
  126. uRet = DSTP2x_SetTmplPrnData(tmp_prt, "Text-01", "56789VWXYZ"); //The data must be utf-8
  127. if (uRet != 0)
  128. {
  129. Console.WriteLine("Failed to set someone data in template,error code:[" + uRet.ToString() + "]");
  130. goto SAMPLE_END;
  131. }
  132. //7.设置RFID数据 Set the RFID data.
  133. uRet = DSTP2x_SetTmplRFIDData(tmp_prt, "EPC-01", writeEPCData, writeEPCData.Length);
  134. if (uRet != 0)
  135. {
  136. Console.WriteLine("Failed to set EPC data in template,error code:[" + uRet.ToString() + "]");
  137. goto SAMPLE_END;
  138. }
  139. uRet = DSTP2x_SetTmplRFIDData(tmp_prt, "USER-01", writeUSERData, writeUSERData.Length);
  140. if (uRet != 0)
  141. {
  142. Console.WriteLine("Failed to set USER data in template,error code:[" + uRet.ToString() + "]");
  143. goto SAMPLE_END;
  144. }
  145. //8.Print template and get the RFID data.
  146. uRet = DSTP2x_PrintTmpl(dev_prt, tmp_prt, szOutFile, ref pOutFileSize, szOutRFID, ref pOutRFIDSize);
  147. if (uRet != 0)
  148. {
  149. Console.WriteLine("Print template failed,error code:[" + uRet.ToString() + "]");
  150. goto SAMPLE_END;
  151. }
  152. Console.WriteLine("The RFID data is " + Encoding.UTF8.GetString(szOutRFID, 0, pOutRFIDSize));
  153. //9.Delete the handle of template.
  154. uRet = DSTP2x_DeleteTmpl(tmp_prt);
  155. if (uRet != 0)
  156. {
  157. Console.WriteLine("Delete handle of template failed,error code:[" + uRet.ToString() + "]");
  158. goto SAMPLE_END;
  159. }
  160. //10.Disconnect the device.
  161. uRet = DSTP2x_DisconnDev(dev_prt);
  162. if (uRet != 0)
  163. {
  164. Console.WriteLine("Delete handle of device failed,error code:[" + uRet.ToString() + "]");
  165. goto SAMPLE_END;
  166. }
  167. Console.WriteLine("This example has been successfully demonstrated!");
  168. SAMPLE_END:
  169. //11.DeInit Library.
  170. uRet = DSTP2x_Lib_Clear();
  171. //Console.ReadKey();
  172. }
  173. private void InPrint()
  174. {
  175. try
  176. {
  177. lbl = new ApplicationClass();
  178. BaseUtil.WriteLbl();
  179. }
  180. catch (Exception)
  181. {
  182. MessageBox.Show("未正确安装BarTender软件");
  183. }
  184. }
  185. private void inoutno_TextChanged(object sender, EventArgs e)
  186. {
  187. }
  188. }
  189. }