当前位置:编程学习 > C#/ASP.NET >>

急求,C#,打印程序中LPT1可以打印,而USB001无法打印。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
using System.Data.OleDb;
using Business;
//using Database;

namespace CBG_Grille
{
    class PrintCBG
    {
        ArrayList arr = new ArrayList();
        IncomeBLL income = new IncomeBLL();
     //   CBGDataInfo cbgserver = new CBGDataInfo();
        PrintCBGBLL pCBGbll = new PrintCBGBLL();
        private FileStream fs = null;
        private const int GENERIC_READ =  - 2147483648;
        private const int GENERIC_WRITE = 0x40000000;
        private const int OPEN_EXISTING = 3;
       // Font f = new Font("宋体", 16,FontStyle.Bold);
       // StringFormat sf = new StringFormat(StringFormatFlags.LineLimit);
     
        [DllImport("kernel32", EntryPoint = "CreateFileA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
        private ArrayList DBTableToString(DataTable tb)
        {
            StringBuilder sb = new StringBuilder();
            ArrayList list = new ArrayList();
            StringBuilder builder = new StringBuilder();
            list.Add("商品名称   |单 价|数量| 金 额");
            int num = arr.Count;
            int length = 0;
            string gridid = arr[length].ToString();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                builder.Append(tb.Rows[i]["MCD_NAME"].ToString().PadRight(10));
                builder.Append(tb.Rows[i]["STO_PRICE"].ToString().PadRight(7));
                builder.Append(tb.Rows[i]["STO_STOCK_NUM"].ToString().PadRight(5));
                builder.Append(tb.Rows[i]["sumPrice"].ToString().PadRight(8));

                list.Add(builder.ToString());
                builder.Remove(0, builder.Length);
                builder.Append(tb.Rows[i]["GRID_CARD"].ToString() + "  格   ");
                builder.Append("厂家:" + tb.Rows[i]["GRG_NAME"].ToString() + "\r\n");
                if (tb.Rows[i]["GRG_TEL"].ToString() != "" && tb.Rows[i]["GRG_PHONE"].ToString() != "")
                {
                    builder.Append("         电话:" + tb.Rows[i]["GRG_PHONE"].ToString() + "\r\n");
                }
                else
                {
                    if (tb.Rows[i]["GRG_TEL"].ToString() != "" && tb.Rows[i]["GRG_PHONE"].ToString() == "")
                    {
                        builder.Append("         电话:" + tb.Rows[i]["GRG_TEL"].ToString() + "\r\n");
                    }
                    if (tb.Rows[i]["GRG_TEL"].ToString() == "" && tb.Rows[i]["GRG_PHONE"].ToString() != "")
                    {
                        builder.Append("         电话:" + tb.Rows[i]["GRG_PHONE"].ToString() + "\r\n");
                    }
                }
                builder.Append("-------------------------------");
                list.Add(builder.ToString());
                builder.Remove(0, builder.Length);
             }
                    
            arr.Clear();
            return list;
        }

      public void PrintTicket(ArrayList array,DataTable MerTb, double yins, double fk,string cbgid)
        //public void PrintTicket(ArrayList array, DataTable MerTb, double yins, double fk, string cbgid, System.Drawing.Printing.PrintPageEventArgs pe)
        {
            this.arr = array;
            try
            {
                IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
               // System.Drawing.Printing.PrintPageEventArgs g = pe;
               
                if (handle.ToInt32() != -1)
                {
                    fs = new FileStream(handle, FileAccess.ReadWrite);
                    StreamReader reader = new StreamReader(fs);
                    StreamWriter writer = new StreamWriter(fs, Encoding.Default);
                    ArrayList list = DBTableToString(MerTb);
                    ShowTotalBLL show = new ShowTotalBLL();
                    DataSet ds = show.getSPname(frmLogin.shopId.ToString());
                    string sshopname = ds.Tables[0].Rows[0][2].ToString();

                    //string sopid = " select CBG_USERS.USER_NUMBER from CBG_USERS ,CBG_SELL where CBG_USERS.USER_ID = CBG_SELL.USER_ID and CBG_SELL.USER_ID = " + "'frmLogin.opId'";
                    //DataSet rs = cbgserver.GetDataSet(sopid);
                    string sopid = pCBGbll.getOPNumber(frmLogin.opId);
                    //g.Graphics.DrawString(sshopname, f, Brushes.Black, 10,20,sf); 
                
                    writer.WriteLine("-----------------------------");
                    writer.WriteLine();
                    writer.WriteLine("          " + sshopname);
                    
                    writer.WriteLine("单据号:" + cbgid);
                    writer.WriteLine("操作员:" + sopid + "        单位(元)");
                   // writer.WriteLine("操作员:" + frmLogin.opId + "");
                    writer.WriteLine("********************************");
                    writer.WriteLine();
                    for (int i = 0; i < list.Count; i++)
                    {
                        writer.Write(list[i]);
                        writer.WriteLine();
                    }
                    double num2 = fk - yins;
                    writer.WriteLine("应收:" + yins.ToString("F2") + "     付款:" + fk.ToString("F2"));
                    writer.WriteLine("找零:" + num2.ToString("F2"));
                    writer.WriteLine();
                    writer.WriteLine("********************************");
                    writer.WriteLine("GSP兽药王超市连锁,您的管理好帮手");
                    writer.WriteLine("          此票为购物凭证!");
                    writer.WriteLine(" 谢谢惠顾!");
                    writer.WriteLine();
                    writer.WriteLine("    店家电话:" + ds.Tables[0].Rows[0][8].ToString());
                    writer.WriteLine();
                    writer.WriteLine("             " + DateTime.Now.ToString());
                    writer.WriteLine("-------------------------------");
                    writer.WriteLine();
                    writer.WriteLine();
                    writer.WriteLine();
                    writer.Flush();
                    fs.Flush();
                    writer.Close();
                    fs.Close();

                }
                else
                    MessageBox.Show("没有打印机!");

            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }
        }

        public void PrintTicketBack(ArrayList array, DataTable MerTb, double yins, string cbgid, string oldcbgid)
        {
            this.arr = array;
            try
            {
                IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
                if (handle.ToInt32() != -1)
                {
                    fs = new FileStream(handle, FileAccess.ReadWrite);
                    StreamReader reader = new StreamReader(fs);
                    StreamWriter writer = new StreamWriter(fs, Encoding.Default);
                    ArrayList list = DBTableToString(MerTb);
                    //frmShowTotal frmshowtota = new frmShowTotal();
                    ShowTotalBLL show = new ShowTotalBLL();
                    DataSet ds = show.getSPname(frmLogin.shopId.ToString());
                    //string sopid = " select CBG_USERS.USER_NUMBER from CBG_USERS ,CBG_SELL where CBG_USERS.USER_ID = CBG_SELL.USER_ID and CBG_SELL.USER_ID = " + "'frmLogin.opId'";
                    //DataSet rs = cbgserver.GetDataSet(sopid);
                    string sopid = pCBGbll.getOPNumber(frmLogin.opId);
                    writer.WriteLine("-------------------------------");
                    writer.WriteLine();
                    writer.WriteLine("         " + ds.Tables[0].Rows[0][2].ToString());
                    writer.WriteLine("单号:" + cbgid + "");
                    writer.WriteLine("操作员:" +sopid);
                    writer.WriteLine("********************************");
                    writer.WriteLine();
                    for (int i = 0; i < list.Count; i++)
                    {
                        writer.Write(list[i]);
                        writer.WriteLine();
                    }
                    writer.WriteLine("总计:" + yins.ToString("F2"));
                    writer.WriteLine("原票号:" + oldcbgid + "作废 转本票");
                    writer.WriteLine();
                    writer.WriteLine("********************************");
                    writer.WriteLine("GSP兽药王超市连锁,您的管理好帮手");
                    writer.WriteLine("          此票为购物凭证!");
                    writer.WriteLine(" 谢谢惠顾!");
                    writer.WriteLine();
                    writer.WriteLine("    店家电话:" + ds.Tables[0].Rows[0][8].ToString());
                    writer.WriteLine();
                    writer.WriteLine("             " + DateTime.Now.ToString());
                    writer.WriteLine("-------------------------------");
                    writer.WriteLine();
                    writer.WriteLine();
                    writer.WriteLine();
                    writer.Flush();
                    fs.Flush();
                    writer.Close();
                    fs.Close();
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }
        }
        
    }
}
--------------------编程问答-------------------- USB001不能打印报什么错?“该文档未能打印”吗

是不是USB001端口被别的USB设备占用了? --------------------编程问答-------------------- 先自己顶一下,这是LPT1端口POSS小票打印程序,现在要换成USB001端口的小票打印程序,请各位高手指点,谢谢 --------------------编程问答-------------------- 只要有一台打印机,USB001没有被占用,就是打不出内容 --------------------编程问答-------------------- IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
运行代码,报没有找到打印机! --------------------编程问答--------------------

肯定是USB端口被占用了,点击打印机的属性---端口 逐个设置USB端口,进行测试; --------------------编程问答-------------------- USB端口没有被占用,电脑上只有一台打印机,打印机属性里只有USB001处于勾选状态,而且测试页也可以打印。
--------------------编程问答-------------------- IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
这不是从LPT1端口打印的吗 --------------------编程问答-------------------- IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
 
是啊,这是从,LPT1打印的,现在要把程序改成,USB001,总是报没有找到打印机,

IntPtr handle = CreateFile("\\\\.\\USB001", -1073741824, 0, 0, 3, 0, 0); --------------------编程问答-------------------- 用.NET的PrintDocument类打印或通过第三方的XtraReport打印,都不需要在代码中指定端口类型的。 --------------------编程问答-------------------- 现在poss小票都是USB接口的,LPT1接口的都很少用了,现在需要把程序改成,USB001接口的打印。改了很久程序总是报,没有打印机。 --------------------编程问答-------------------- 上面是该打印文件全部代码,请高手跟我调一下,看是否可以通过USB001打印 --------------------编程问答-------------------- USB打印可能需要安装USB驱动程序,你看看你的设备管理器中的通用串行总线控制器中有没有识别出打印机的USB端口。如果没有识别,比如显示unknown device,需要禁用USB2.0的设备才可以识别出打印机的USB端口。 --------------------编程问答-------------------- USB001小票打印驱动装好了,测试页打印也正常,现在估计就是程序中没有写好 --------------------编程问答-------------------- http://topic.csdn.net/u/20070315/15/10302bca-0836-4a26-9b19-49d71339466a.html
http://topic.csdn.net/u/20110917/13/ccda9e28-c6d6-47d0-b74f-c8a524693e13.html
希望对你有用 --------------------编程问答-------------------- LZ你是不能直接操作USB001这个设备的,如果要打印,方法有二:
1,见打印信息存在一个txt文本里或一个字符串里,直接打印文件或字符串,还要注意要把打印机设置为默认打印机
2,安装环路网卡,windows自带有,看下面链接
http://www.yt-pos.com/ask/question.php?id=263
这两种方法都是我验证过的 --------------------编程问答-------------------- 我记得以前说过
http://topic.csdn.net/u/20110131/12/8f8b585e-fa4d-494b-8a83-66d1ed0f07af.html --------------------编程问答-------------------- 谢谢,楼上你能帮我把帖子中的代码调整一下吗,现在是LPT接口打印机可以打印,现在我想调成USB001,也就是USB接口的打印机也能打印,驱动安装好了,默认端口USB001,打印测试页也可以打印出来,驱动和端口都没有问题,主要是程序没有调用USB001接口,没有找到打印机 --------------------编程问答-------------------- 把打印机设置为默认打印机,就像把“你好”打印在A4纸上一样的操作就好了,不要用CreateFile
上面的连接里有代码啊 --------------------编程问答-------------------- IntPtr handle = CreateFile("LPT1", -1073741824, 0, 0, 3, 0, 0);
  if (handle.ToInt32() != -1)
  {
把这几行拿掉,就可以了吧。 --------------------编程问答-------------------- 楼上,你说把这行拿掉,你试过没有。 --------------------编程问答-------------------- 注释掉你说的那几行后,好像不行 --------------------编程问答-------------------- 我也遇到了相同的问题.. #15  只是打开打印设备吧? 而不是直接打印信息出来 --------------------编程问答--------------------
引用 21 楼 longzeji 的回复:
注释掉你说的那几行后,好像不行


会不会是这样子:你程序里用USB1口打印出内容,但是你打印机接电脑的USB口不是1号口,可能
是2号、3号、4号口,所以程序就找不到打印机了!也就打出内容了,你可以逐个USB口都试试看!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,