c#调用刀片小票打印机

public static bool Print(int orderId, string orderTime)
        {
            bool b = true;

            string cut = ((char)29).ToString() + ((char)86).ToString() + ((char)1).ToString() + ((char)49).ToString();//发送切纸指令
            //byte[] byte_cut = System.Text.Encoding.Default.GetBytes(cut);
            //string font = ((char)1D).ToString() + ((char)21).ToString() + ((char)01).ToString()+ ((char)10).ToString() ;//文字大小

            byte[] fonts = new byte[] { 27, 82, 0 };//1D 21 选择字体
            byte[] temp = new byte[] { 29, 33, 17 };//1D 21 选择字体大小,0x01放大一倍

            byte[] bty_tmp = new byte[] { 29, 86, 1, 49 };//切纸
            byte[] Internal = System.Text.Encoding.Default.GetBytes(((char)27).ToString() + ((char)64).ToString());//初始化打印机


            System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
            System.Net.Sockets.NetworkStream stream = null;
            Int32 port = 9100;
            //string ipPrint = ConfigurationManager.ConnectionStrings["printIP"].ConnectionString;//获取打印机名称
            string ipPrint = "192.168.0.77";
            //string ipPrint =Common.Comm.readXml("/xml/setting.xml", "printIP");//读取xml的IP
            try
            {
                //client.Connect("192.168.1.77", port);
                client.Connect(ipPrint, port);
                stream = client.GetStream();
                //是否支持写入
                if (!stream.CanWrite)
                {
                    b = false;
                }
                Byte[] data;

                //按菜品类型和时间、单号、点菜员、订单点菜备注 分组查询
                string sql_query = "select tname,typeid,dname,uname,d.addtime,d.remark,count(fname)fnum"+
                " from   "+
                " p_orderDetailInfo d,p_orderinfo o, p_deskinfo desk, p_foodinfo f, p_foodType t,users u"+
                " where d.orderid=o.id and d.foodid=f.id and o.deskid=desk.id and f.typeid=t.id and u.id=d.userid"+
                " and o.id=" + orderId + " and d.addtime='" + orderTime + "' " +
                " group by tname,typeid,d.remark,dname,uname,d.addtime ";

                string sql_detail = "";


                DataTable dt = DbSqlHelper.ExecuteDataSet(CommandType.Text, sql_query).Tables[0];
                DataTable dt_detail = null;

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        //当前该订单的菜品信息
                        sql_detail = "select fname,count(fname) fnum,f.id ,userid,d.remark from "+
                        "p_orderDetailInfo d,p_foodinfo f "+
                        "where d.foodid=f.id and TypeID=" + dt.Rows[i]["typeid"] + " and d.addtime='" + orderTime + "' and orderid=" + orderId + "  group by fname,f.id ,userid,d.remark";

                        dt_detail = DbSqlHelper.ExecuteDataSet(CommandType.Text, sql_detail).Tables[0];
                        stream.Write(Internal, 0, Internal.Length);
                        stream.Write(fonts, 0, fonts.Length);//设置字体
                        stream.Write(temp, 0, temp.Length);//设置字体大小--关键

                        string outPut = "-" + dt.Rows[i]["tname"] + "-\n" + dt.Rows[i]["dname"] + "\t点菜员 - " + dt.Rows[i]["uname"] + "\n--------------------\n";

                        for (int j = 0; j < dt_detail.Rows.Count; j++)
                        {
                            outPut += "\n\n"+dt_detail.Rows[j]["fname"] + "\t" + dt_detail.Rows[j]["fnum"];
                            if (dt_detail.Rows[j]["remark"].ToString() != "")
                            {
                                outPut += "   * " + dt_detail.Rows[j]["remark"] + "\n\n";//菜品【口味】备注
                            }
                            UpdateStatus(orderId.ToString(), dt_detail.Rows[j]["id"].ToString(), orderTime);//修改该打印状态
                        }

                        outPut += "\n\n" + DateTime.Now + "\n\n\n\n\n\n";
                        data = data = System.Text.Encoding.Default.GetBytes(outPut);
                        stream.Write(data, 0, data.Length);//输出文字
                        outPut = ""; // 一个分类打印完成后清空。切纸

                        //stream.Write(Internal, 0, Internal.Length);
                        // data = System.Text.Encoding.Default.GetBytes(cut);
                        //stream.Write(data, 0, data.Length);

                        stream.Write(bty_tmp, 0, bty_tmp.Length);
                        //bty_tmp = new byte[] { 29, 86, 1, 49 };


                    }

                    //data = System.Text.Encoding.Default.GetBytes(cut);
                    //stream.Write(bty_tmp, 0, bty_tmp.Length);
                    //stream.Write(bty_tmp, 0, bty_tmp.Length);
                }
                //-----------------
            }
            catch
            {
                //打印机缺纸或者纸匣打开时,不会出现异常,不用特殊代码判断,数据不会丢失.
                b = false;
               
            }
            finally
            {
                stream.Close();
                stream.Dispose();
                client.Close();
            }
            return b;
        }

  

2年前的餐饮项目,在一些饭店已经运行了1年多了。

适用于热敏刀片打印机,

运用在超市、饭店 等各种需要小票打印的场合。

打印完后,会自动切纸。

 

这里有一个排版的问题,对齐不是很严格。

 

http://www.cnblogs.com/chenqilong/archive/2013/04/18/3028358.html

 

posted @ 2017-12-25 16:18  左正  阅读(629)  评论(0编辑  收藏  举报