C# 使用C/S模式操作小票機列印

來源:互聯網
上載者:User

標籤:

此方式適用於市場上大多數的小票機 佳博、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模式操作小票機列印

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.