using LabelManager2; using System; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicForm; using UAS_MES_NEW.PublicMethod; using DEV_HDL = System.UInt32; using LABEL_TEMP_HDL = System.UInt32; namespace UAS_MES_NEW.Query { public partial class Query_RFIDPrint : Form { [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_Lib_Init(string pSzInitInfo, Int32 nInitInfoLen, StringBuilder pSzResult, ref int pResultLen); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_Lib_Clear(); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_EnumDev(Int32 nEnumType, byte[] szEnumList, ref int pDevSize, ref int pDevNum); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_ConnEnumeratedDev(string szDevName, ref DEV_HDL pDevHdl); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_DisconnDev(DEV_HDL ullDevHdl); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_SetPrnEmulation(DEV_HDL ullDevHdl, Int32 nEmulation); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_SetImgDpi(DEV_HDL ullDevHdl, Int32 nDpi); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_LoadLabelTmpl(string szFileName, ref LABEL_TEMP_HDL pLTHdl); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_PrintTmpl(DEV_HDL ullDevHdl, LABEL_TEMP_HDL ullLTHdl, byte[] szOutFile, ref int pOutFileSize, byte[] szOutRFID, ref int pOutRFIDSize); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_DeleteTmpl(LABEL_TEMP_HDL ullLTHdl); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_SetTmplPrnMode(LABEL_TEMP_HDL ullLTHdl, Int32 nPrnMode); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_SetTmplPrnData(LABEL_TEMP_HDL ullLTHdl, string szElemID, string szActualData); [DllImport("lib/Win32/libDSThermal.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern UInt32 DSTP2x_SetTmplRFIDData(LABEL_TEMP_HDL ullLTHdl, string szElemID, string pActualData, Int32 nActualDataSize); DataHelper dh = SystemInf.dh; ApplicationClass lbl; Document doc; Thread InitPrint; public Query_RFIDPrint() { InitializeComponent(); } private void Query_SpecialReport_Load(object sender, EventArgs e) { DEV_HDL uRet = 0; StringBuilder pSzResult = new StringBuilder(256); int pResultLen = 256; //1.Initialize the Library. uRet = DSTP2x_Lib_Init("", 0, pSzResult, ref pResultLen); if (uRet != 0) { Console.WriteLine("DSTP2x_Lib_Init,error code:[" + uRet.ToString() + "]"); return; } DEV_HDL dev_prt = 0; LABEL_TEMP_HDL tmp_prt = 0; byte[] pEnumList = new byte[1024]; int enumListLen = 1024; byte[] szOutFile = new byte[1024]; int pOutFileSize = 1024; byte[] szOutRFID = new byte[1024]; int pOutRFIDSize = 1024; int deviceNum = 0; string device = ""; string tmpPath = "../../samplecode/CSharp/TemplatePrint_RFID/templateRFID.dlt"; string writeEPCData = "ABC123"; string writeUSERData = "123ABC"; //2.Enumerate devices. 1-USB, 2-NET uRet = DSTP2x_EnumDev(1, pEnumList, ref enumListLen, ref deviceNum); //USB connection if (uRet != 0) { Console.WriteLine("Enumerate device error,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } if (deviceNum > 1) { string str = Encoding.UTF8.GetString(pEnumList, 0, enumListLen); string[] enumList = str.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); device = enumList[0]; } else if (deviceNum == 1) device = Encoding.UTF8.GetString(pEnumList, 0, enumListLen); else { Console.WriteLine("No device"); goto SAMPLE_END; } //3.Connect the device. uRet = DSTP2x_ConnEnumeratedDev(device, ref dev_prt); if (uRet != 0) { Console.WriteLine("Failed to connect device,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //Non essential interface. 1-ZPL, 2-TSPL, 3-ESCPOS uRet = DSTP2x_SetPrnEmulation(dev_prt, 1); //ZPL if (uRet != 0) { Console.WriteLine("Failed to set up print simulation,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //4.加在标签文件Load the template. uRet = DSTP2x_LoadLabelTmpl(tmpPath, ref tmp_prt); if (uRet != 0) { Console.WriteLine("Failed to load label template,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //5.设置打印模式 0-打印 Set the print mode. 0-print, 1-generate the prn file, 2-generate the preview image. uRet = DSTP2x_SetTmplPrnMode(tmp_prt, 0); //set print mode if (uRet != 0) { Console.WriteLine("Setting whether to print failed,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //6.设置打印数据 Set the print data. uRet = DSTP2x_SetTmplPrnData(tmp_prt, "Text-01", "56789VWXYZ"); //The data must be utf-8 if (uRet != 0) { Console.WriteLine("Failed to set someone data in template,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //7.设置RFID数据 Set the RFID data. uRet = DSTP2x_SetTmplRFIDData(tmp_prt, "EPC-01", writeEPCData, writeEPCData.Length); if (uRet != 0) { Console.WriteLine("Failed to set EPC data in template,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } uRet = DSTP2x_SetTmplRFIDData(tmp_prt, "USER-01", writeUSERData, writeUSERData.Length); if (uRet != 0) { Console.WriteLine("Failed to set USER data in template,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //8.Print template and get the RFID data. uRet = DSTP2x_PrintTmpl(dev_prt, tmp_prt, szOutFile, ref pOutFileSize, szOutRFID, ref pOutRFIDSize); if (uRet != 0) { Console.WriteLine("Print template failed,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } Console.WriteLine("The RFID data is " + Encoding.UTF8.GetString(szOutRFID, 0, pOutRFIDSize)); //9.Delete the handle of template. uRet = DSTP2x_DeleteTmpl(tmp_prt); if (uRet != 0) { Console.WriteLine("Delete handle of template failed,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } //10.Disconnect the device. uRet = DSTP2x_DisconnDev(dev_prt); if (uRet != 0) { Console.WriteLine("Delete handle of device failed,error code:[" + uRet.ToString() + "]"); goto SAMPLE_END; } Console.WriteLine("This example has been successfully demonstrated!"); SAMPLE_END: //11.DeInit Library. uRet = DSTP2x_Lib_Clear(); //Console.ReadKey(); } private void InPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(); } catch (Exception) { MessageBox.Show("未正确安装BarTender软件"); } } private void inoutno_TextChanged(object sender, EventArgs e) { } } }