{"id":2397,"date":"2024-09-09T13:39:27","date_gmt":"2024-09-09T05:39:27","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=2397"},"modified":"2024-09-09T13:39:27","modified_gmt":"2024-09-09T05:39:27","slug":"%e7%ae%80%e5%8d%95%e6%89%93%e5%8d%b0%e6%9c%ba%e9%9b%86%e6%88%90%e5%b9%b6%e5%ae%9e%e7%8e%b0%e7%9b%91%e5%90%ac%e6%89%93%e5%8d%b0%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2024\/09\/09\/%e7%ae%80%e5%8d%95%e6%89%93%e5%8d%b0%e6%9c%ba%e9%9b%86%e6%88%90%e5%b9%b6%e5%ae%9e%e7%8e%b0%e7%9b%91%e5%90%ac%e6%89%93%e5%8d%b0%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u7b80\u5355\u6253\u5370\u673a\u96c6\u6210\u5e76\u5b9e\u73b0\u76d1\u542c\u6253\u5370\u529f\u80fd"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u76d1\u542c\u90e8\u5206<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>private PrintService printService = new PrintService();\nprivate bool isCollectionChangeListening = true; \/\/ \u7528\u4e8e\u63a7\u5236\u662f\u5426\u76d1\u542c\u96c6\u5408\u53d8\u5316\n\n\/\/\/ &lt;summary>\n\/\/\/ \u5b9e\u9a8c\u7ed3\u679c\u5217\u8868\n\/\/\/ &lt;\/summary>\npublic ObservableCollection&lt;ExpViewUtil> ExperimentDataItems {\n    get { return experimentDataItems; }\n    set {\n        if (experimentDataItems != null) {\n            experimentDataItems.CollectionChanged -= OnExperimentDataItemsChanged;\n        }\n        experimentDataItems = value;\n        if (experimentDataItems != null &amp;&amp; isCollectionChangeListening) {\n            experimentDataItems.CollectionChanged += OnExperimentDataItemsChanged;\n        }\n    }\n}\n\n\/\/\/ &lt;summary>\n\/\/\/ \u8bbe\u7f6e\u662f\u5426\u76d1\u542c\u96c6\u5408\u53d8\u5316\n\/\/\/ &lt;\/summary>\npublic void SetCollectionChangeListening(bool isListening) {\n    isCollectionChangeListening = isListening;\n    if (experimentDataItems != null) {\n        if (isListening) {\n            experimentDataItems.CollectionChanged += OnExperimentDataItemsChanged;\n        } else {\n            experimentDataItems.CollectionChanged -= OnExperimentDataItemsChanged;\n        }\n    }\n}\n\n\/\/\/ &lt;summary>\n\/\/\/ \u5904\u7406\u96c6\u5408\u66f4\u6539\u4e8b\u4ef6\n\/\/\/ &lt;\/summary>\nprivate void OnExperimentDataItemsChanged(object sender, NotifyCollectionChangedEventArgs e) {\n    if (!isCollectionChangeListening) return;\n    if (e.Action == NotifyCollectionChangedAction.Add) {\n        foreach (ExpViewUtil newItem in e.NewItems) {\n            printService.PrintExpData(newItem);\n        }\n    }\n}\n\n      \n\/\/\/ &lt;summary>\n\/\/\/ \u6a21\u62df\u7684\u6253\u5370\u65b9\u6cd5\n\/\/\/ &lt;\/summary>\nprivate void PrintExpData2(ExpViewUtil expData) {\n    Console.WriteLine($\"\u5b9e\u9a8c\u540d\u79f0: {expData.ExperimentName}, \u6837\u672c\u540d\u79f0: {expData.SampleName}, \u7ec6\u80de\u7c7b\u578b: {expData.CellType}\");\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6253\u5370\u90e8\u5206<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code> public class TSPL_SDK {\n     public const CharSet charSet = CharSet.Ansi;\n\n     public IntPtr printer;\n     public string MachineType = \"HT300\";\n     public string IOSettings = \"USB\";\n     public int labelWidth = 80;\n     public int labelHeight = 60;\n     public int speed = 2;\n     public int density = 15;\n     public int type = 1;\n     public int gap = 0;\n     public int offset = 0;\n     public int Copies = 1;\n     public int xPos = 0;\n     public int yPos = 0;\n     public int iPrintImageMode = 0;\n     public int iBrightness = 0;\n     public int iContrast = 0;\n     public int iHtMode = 0;\n     public int iTagNum = 1;\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int PrinterCreator(ref IntPtr printer, string model);\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int PortOpen(IntPtr printer, string portSetting);\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int PortClose(IntPtr printer);\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int PrinterDestroy(IntPtr printer);\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int TSPL_Setup(IntPtr printer, int labelWidth, int labelHeight, int speed, int density, int type, int gap, int offset);\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int TSPL_TextPrint(IntPtr printer, string chText, int iTextLenth, int iXPos, int iYPos, int iXImage, int iYImage, int iFontSize);\n\n     &#91;DllImport(\"TSPL_SDK\", CharSet = charSet)]\n     public static extern int TSPL_StartPrint(IntPtr printer, int iXPos, int iYPos, int iPrintImageMode, int iBrightness, int iContrast, int iHtMode, int iTagNum, int iCopies);\n }\n\n public class PrintService {\n     public TSPL_SDK sdk = new TSPL_SDK();\n\n     public void PrintExpData(ExpViewUtil expData) {\n         string text =\n             $\"Sample ID: {expData.ExpUniqueId},\" +\n             $\"Sample Date: {expData.ExpTime}, \" +\n             $\"Results:\";\n         try {\n             int result = TSPL_SDK.PrinterCreator(ref sdk.printer, sdk.MachineType);\n             if (result != 0) throw new Exception(\"Failed to create printer.\");\n\n             result = TSPL_SDK.PortOpen(sdk.printer, sdk.IOSettings);\n             if (result != 0) throw new Exception(\"Failed to open port.\");\n\n             result = TSPL_SDK.TSPL_Setup(sdk.printer, sdk.labelWidth, sdk.labelHeight, sdk.speed, sdk.density, sdk.type, sdk.gap, sdk.offset);\n             if (result != 0) throw new Exception(\"Failed to setup printer.\");\n\n             result = TSPL_SDK.TSPL_TextPrint(sdk.printer, text, text.Length, sdk.xPos, sdk.yPos, 0, 0, 24);\n             if (result != 0) throw new Exception(\"Failed to print text.\");\n\n             result = TSPL_SDK.TSPL_StartPrint(sdk.printer, sdk.xPos, sdk.yPos, sdk.iPrintImageMode, sdk.iBrightness, sdk.iContrast, sdk.iHtMode, sdk.iTagNum, sdk.Copies);\n             if (result != 0) throw new Exception(\"Failed to start print.\");\n\n         } catch (Exception ex) {\n             Console.WriteLine($\"\u6253\u5370\u5931\u8d25: {ex.Message}\");\n             Console.WriteLine($\"\u5f53\u524d\u5b9e\u9a8c: {text}\");\n         } finally {\n             if (sdk.printer != IntPtr.Zero) {\n                 TSPL_SDK.PortClose(sdk.printer);\n                 TSPL_SDK.PrinterDestroy(sdk.printer);\n                 sdk.printer = IntPtr.Zero;\n             }\n         }\n     }\n }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76d1\u542c\u90e8\u5206 \u6253\u5370\u90e8\u5206<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2397","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=2397"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2397\/revisions"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=2397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=2397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=2397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}