POS打印机系列之 => 并口

并口是什么

并行接口:一次传输多个bit。常见25针接口。

并口通信

    /// <summary>
    /// 并口通信
    /// </summary>
    public interface IParallelContact : IPortContact
    {
        ActionResult Open(string port);
    }

    public class ParallelContact : IParallelContact
    {
        public bool Opened { get; set; }
        private string _port = string.Empty;

        public const short INVALID_HANDLE_VALUE = -1;
        public const uint GENERIC_READ = 0x80000000;
        public const uint GENERIC_WRITE = 0x40000000;
        public const int OPEN_EXISTING = 3;

        private int m_lpt = INVALID_HANDLE_VALUE;

        [StructLayout(LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }
        [DllImport("kernel32.dll")]
        private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
        [DllImport("kernel32.dll")]
        private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped);
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(int hObject); //C#LPT端口打印类的操作 

        /// <summary>
        /// Closes this instance.关闭端口句柄
        /// </summary>
        public void Close()
        {
            if (m_lpt != INVALID_HANDLE_VALUE)
            {
                CloseHandle(m_lpt);
                m_lpt = INVALID_HANDLE_VALUE;
                Opened = false;
            }
        }

        /// <summary>
        /// Writes the specified data.写入文本数据给打印机输出
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public int Write(string data)
        {
            if (Opened)
            {

                byte[] byteData = System.Text.Encoding.Default.GetBytes(data);

                return this.Write(byteData);
            }
            return 0;
        }

        /// <summary>
        /// Writes the specified data.写入控制指令或文本给打印机输出
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public int Write(byte[] data)
        {
            int BytesWritten = 0;

            if (data.Length == 0) return BytesWritten;

            if (Opened)
            {
                OVERLAPPED overlap = new OVERLAPPED();

                Thread.Sleep(40);
                if (WriteFile(m_lpt, data, data.Length, ref BytesWritten, ref overlap))
                {
                    Thread.Sleep(40);
                }
            }

            return BytesWritten;
        }

        /// <summary>
        /// Opens the specified port.打开指定的并口
        /// </summary>
        /// <param name="port">The port.</param>
        /// <returns></returns>
        public ActionResult Open(string port)
        {
            _port = port;
            m_lpt = CreateFile(port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); //GENERIC_READ | GENERIC_WRITE

            if (m_lpt == INVALID_HANDLE_VALUE) return new ActionResult(false, "打开端口[" + port + "]失败!");

            Opened = true;

            return new ActionResult(true);
        }
    }
View Code

并口打印
并口打印仿照串口打印的实现方式,只需传入命令

本人原创,欢迎转载,声明原载

 

posted @ 2013-07-17 11:22  zzq417  阅读(423)  评论(0编辑  收藏  举报