码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2021-05-24 05:37:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:vat   access   extern   收银   get   html   字体   int   buffer   

 

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#端口连接热敏打印机发送指令

标签:vat   access   extern   收银   get   html   字体   int   buffer   

原文地址:https://www.cnblogs.com/fps2tao/p/14756026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!