编程语言
首页 > 编程语言> > C#端口连接热敏打印机发送指令

C#端口连接热敏打印机发送指令

作者:互联网

 

class LptControl
{
    private string LptStr = "lpt1";
    public LptControl(string l_LPT_Str)
    {

        LptStr = l_LPT_Str;
    }
    [StructLayout(LayoutKind.Sequential)]
    private struct OVERLAPPED
    {
        int Internal;
        int InternalHigh;
        int Offset;
        int OffSetHigh;
        int hEvent;
    }


    //调用DLL.  
    [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);
    private int iHandle;


    /// <summary>  
    /// 打开端口  
    /// </summary>  
    /// <returns></returns>  
    public bool Open()
    {
        iHandle = CreateFile(LptStr, 0x40000000, 0, 0, 3, 0, 0);
        // iHandle = CreateFile(LptStr, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);  

        if (iHandle != -1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>  
    /// 打印字符串,通过调用该方法可以打印需要的字符串  
    /// </summary>  
    /// <param name="Mystring"></param>  
    /// <returns></returns>  
    public bool Write(String Mystring)
    {
        //如果端口为打开,则提示,打开,则打印  
        if (iHandle != -1)
        {
            OVERLAPPED x = new OVERLAPPED();
            int i = 0;
            //byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);  
            byte[] mybyte = Encoding.GetEncoding("GB2312").GetBytes(Mystring);
            bool b = WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
            return b;
        }
        else
        {
            throw new Exception("不能连接到打印机!");
        }
    }
    /// <summary>  
    /// 打印命令,通过参数,可以打印小票打印机的一些命令,比如换行,行间距,打印位图等。  
    /// </summary>  
    /// <param name="mybyte"></param>  
    /// <returns></returns>  
    public bool Write(byte[] mybyte)
    {
        //如果端口为打开,则提示,打开,则打印  
        if (iHandle != -1)
        {
            OVERLAPPED x = new OVERLAPPED();
            int i = 0;
            return WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
        }
        else
        {
            throw new Exception("不能连接到打印机!");
        }
    }

    /// <summary>  
    /// 关闭端口  
    /// </summary>  
    /// <returns></returns>  
    public bool Close()
    {
        return CloseHandle(iHandle);
    }

}

 

 

             Byte[] data1 = new byte[] { 0x1D, 0x56, 0x30 };//切纸指令
             Byte[] data2 = new byte[] { 0x1B, 0x70, 0x00, 0x3C, 0xFF };//弹钱箱指令
             Byte[] data3 = new byte[] { 0x1D, 0x21, 0x24, 0x24 };//字体大小36
             Byte[] data4 = new byte[] { 0x1D, 0x21, 0x0C, 0x0C };//字体大小12
             Byte[] data5 = new byte[] { 0x1B, 0x45, 0x01 };//字体加粗开始
             Byte[] data6 = new byte[] { 0x1B, 0x45, 0x00 };//字体加粗结束

             LptControl lc = new LptControl("lpt1");//端口
             lc.Open();
             lc.Write(data3);//打印指令byte数组

             lc.Write("文字文字");//打印文字
             lc.Write("\n\n\n\n");
             lc.Close();

 

 

 

 

转 : https://www.cnblogs.com/rinack/p/4837993.html

 

标签:C#,热敏,端口,iHandle,int,bool,mybyte,new,byte
来源: https://www.cnblogs.com/fps2tao/p/14756026.html