標籤:
此方式適用於市場上大多數的小票機 佳博、POS58 等,不適用於有些標籤印表機 比如斑馬印表機等
直接貼代碼:
private FileStream fs = null; [DllImport("kernel32.dll", CharSet = CharSet.Auto)]//調用系統API列印函數 public static extern IntPtr CreateFile ( string FileName, // file name uint DesiredAccess, // access mode uint ShareMode, // share mode uint SecurityAttributes, // Security Attributes uint CreationDisposition, // how to create uint FlagsAndAttributes, // file attributes string hTemplateFile // handle to template file ); /// <summary> /// 列印小票 /// </summary> /// <param name="msg"></param> /// <returns></returns> public string PrintMsg(string PosPort, string msg, bool isOpenMoneyBox) { try { IntPtr iHandle = CreateFile(PosPort, 0x40000000, 0, 0, 3, 0, null); if (iHandle.ToInt32() == -1) { return "票機串連失敗.或者連接埠號碼錯誤.請測試印表機是否正常!"; } else { try { fs = new FileStream(iHandle, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //寫資料 sw.WriteLine(msg); //開啟錢箱 if (isOpenMoneyBox) { sw.Write(((char)27).ToString() + "p" + ((char)0).ToString() + ((char)60).ToString() + ((char)255).ToString()); } sw.Flush(); sw.Close(); fs.Close(); return ""; } catch (Exception ex) { return ex.InnerException == null ? ex.Message : ex.InnerException.Message; } } } catch (Exception exp) { return "TPL或者COM口串連失敗!"; } }
View Code
C# 使用C/S模式操作小票機列印