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

C# 条码打印 求助

最近想研究研究C#条码打印,打印机是斑马的S4M条码打印机。我看到网上有好多介绍利用ZPL控制条码打印的文章,但都是发送ZPL指令到LPT端口,可我的笔记本上没有LPT端口,用的是LPT转USB端口的数据线,这样一来发送ZPL指令到LPT端口就行不通了,哪位各位高手帮帮我,该怎么解决这个问题啊?万分感谢 --------------------编程问答-------------------- S4M条码机是支持USB的.
先安装驱动系统
通过API直接给打印器发送指令
p_PrintName 为打印机名称

      public static void WinApiPrintByte(string p_PrintName, byte[] p_Byte)
        {
            if (p_PrintName != null && p_PrintName.Length > 0)
            {
                IntPtr _PrintHandle;
                IntPtr _JobHandle = Marshal.AllocHGlobal(100);
                if (Win32API.OpenPrinter(p_PrintName, out _PrintHandle, IntPtr.Zero))
                {
                    ADDJOB_INFO_1 _JobInfo = new ADDJOB_INFO_1();
                    int _Size;
                    AddJob(_PrintHandle, 1, _JobHandle, 100, out _Size);
                    _JobInfo = (ADDJOB_INFO_1)Marshal.PtrToStructure(_JobHandle, typeof(ADDJOB_INFO_1));
                    System.IO.File.WriteAllBytes(p_PrintName, p_Byte);
                    ScheduleJob(_PrintHandle, _JobInfo.JobID);
                    ClosePrinter(_PrintHandle);
                    Marshal.FreeHGlobal(_JobHandle);
                }
            }
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct ADDJOB_INFO_1
        {
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpPath;
            public Int32 JobID;
        }
        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool AddJob(IntPtr ptrPrinter, Int32 iLevel, IntPtr ptrJob, Int32 iSize, out Int32 iCpSize);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool ScheduleJob(IntPtr ptrPrinter, Int32 JobID);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool ClosePrinter(IntPtr ptrPrinter);  --------------------编程问答-------------------- 應該是支持USB接口的吧··· --------------------编程问答--------------------
引用 1 楼 zgke 的回复:
S4M条码机是支持USB的.
先安装驱动系统
通过API直接给打印器发送指令
p_PrintName 为打印机名称

      public static void WinApiPrintByte(string p_PrintName, byte[] p_Byte)
        {
            if (p_PrintName != null && p_PrintName.Length > 0)
            {
                IntPtr _PrintHandle;
                IntPtr _JobHandle = Marshal.AllocHGlobal(100);
                if (Win32API.OpenPrinter(p_PrintName, out _PrintHandle, IntPtr.Zero))
                {
                    ADDJOB_INFO_1 _JobInfo = new ADDJOB_INFO_1();
                    int _Size;
                    AddJob(_PrintHandle, 1, _JobHandle, 100, out _Size);
                    _JobInfo = (ADDJOB_INFO_1)Marshal.PtrToStructure(_JobHandle, typeof(ADDJOB_INFO_1));
                    System.IO.File.WriteAllBytes(p_PrintName, p_Byte);
                    ScheduleJob(_PrintHandle, _JobInfo.JobID);
                    ClosePrinter(_PrintHandle);
                    Marshal.FreeHGlobal(_JobHandle);
                }
            }
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct ADDJOB_INFO_1
        {
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpPath;
            public Int32 JobID;
        }
        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool AddJob(IntPtr ptrPrinter, Int32 iLevel, IntPtr ptrJob, Int32 iSize, out Int32 iCpSize);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool ScheduleJob(IntPtr ptrPrinter, Int32 JobID);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool ClosePrinter(IntPtr ptrPrinter);

UP --------------------编程问答-------------------- 友情帮顶 --------------------编程问答-------------------- 条码打印要做好还真不容易……

总的来说,可以分为 无驱打印 和 有驱打印 两种,意思就是前者不用安装打印机驱动后者需要安装打印机驱动。

网上那些发送ZPL指令到LPT端口属于无驱打印的类型,LPT打印机和IP打印机做无驱很简单,给端口发指令即可,往但USB端口做无驱打印很麻烦(我曾经找资料试了几天,最后还是放弃了...)~

使用USB端口打印的话,还是交给Windows吧,直接用winspool的API,指令格式是一样的,使用方法如2楼,只需要知道安装后的打印机名称即可。

无驱打印的话还需要自己维护打印池,处理打印失败的问题;使用Windows驱动的打印机的话,就直接交给系统打印池管理了。

至于楼主要做无驱打印还是有驱打印,那就看你的业务需要求了。。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,