ZPL并口打印

  1. using  System;   
  2. using  System.Collections.Generic;   
  3. using  System.Text;   
  4. using  System.Runtime.InteropServices;   
  5.     
  6. namespace  Barcode_Print   
  7. {
  8.   /// <summary>
  9.   /// 本类使用说明:
  10.   /// 将一行ZPL指令作为string参数传给write函数即可
  11.   /// </summary>
  12.   class LPTControl
  13.   {
  14.       [StructLayout(LayoutKind.Sequential)]
  15.       private struct OVERLAPPED
  16.       {
  17.           int Internal;
  18.           int InternalHigh;
  19.           int Offset;
  20.           int OffSetHigh;
  21.           int hEvent;
  22.       }
  23.       [DllImport("kernel32.dll")]
  24.       private static extern int CreateFile(
  25.       string lpFileName,
  26.       uint dwDesiredAccess,
  27.       int dwShareMode,
  28.       int lpSecurityAttributes,
  29.       int dwCreationDisposition,
  30.       int dwFlagsAndAttributes,
  31.       int hTemplateFile
  32.       );
  33.       [DllImport("kernel32.dll")]
  34.       private static extern bool WriteFile(
  35.       int hFile,
  36.       byte[] lpBuffer,
  37.       int nNumberOfBytesToWrite,
  38.       out   int lpNumberOfBytesWritten,
  39.       out   OVERLAPPED lpOverlapped
  40.       );
  41.       [DllImport("kernel32.dll")]
  42.       private static extern bool CloseHandle(
  43.       int hObject
  44.       );
  45.  
  46.       private int iHandle;
  47.       public bool Open()
  48.       {
  49.           iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
  50.           if (iHandle != -1)
  51.           {
  52.               return true;
  53.           }
  54.           else
  55.           {
  56.               return false;
  57.           }
  58.       }
  59.       public bool Write(String Mystring)
  60.       {
  61.           if (iHandle != -1)
  62.           {
  63.               int i;
  64.               OVERLAPPED x;
  65.               byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
  66.               return WriteFile(iHandle, mybyte, mybyte.Length, out   i, out   x);
  67.           }
  68.           else
  69.           {
  70.               throw new Exception("端口未打开!");
  71.           }
  72.       }
  73.       public bool Close()
  74.       {
  75.           return CloseHandle(iHandle);
  76.       }
  77.   }
  78. }  
  79.       [DllImport("kernel32.dll")]
  80.       private static extern int CreateFile(
  81.       string lpFileName,
  82.       uint dwDesiredAccess,
  83.       int dwShareMode,
  84.       int lpSecurityAttributes,
  85.       int dwCreationDisposition,
  86.       int dwFlagsAndAttributes,
  87.       int hTemplateFile
  88.       );
  89.       [DllImport("kernel32.dll")]
  90.       private static extern bool WriteFile(
  91.       int hFile,
  92.       byte[] lpBuffer,
  93.       int nNumberOfBytesToWrite,
  94.       out   int lpNumberOfBytesWritten,
  95.       out   OVERLAPPED lpOverlapped
  96.       );
  97.       [DllImport("kernel32.dll")]
  98.       private static extern bool CloseHandle(
  99.       int hObject
  100.       );
  101.  
  102.       private int iHandle;
  103.       public bool Open()
  104.       {
  105.           iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
  106.           if (iHandle != -1)
  107.           {
  108.               return true;
  109.           }
  110.           else
  111.           {
  112.               return false;
  113.           }
  114.       }
  115.       public bool Write(String Mystring)
  116.       {
  117.           if (iHandle != -1)
  118.           {
  119.               int i;
  120.               OVERLAPPED x;
  121.               byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
  122.               return WriteFile(iHandle, mybyte, mybyte.Length, out   i, out   x);
  123.           }
  124.           else
  125.           {
  126.               throw new Exception("端口未打开!");
  127.           }
  128.       }
  129.       public bool Close()
  130.       {
  131.           return CloseHandle(iHandle);
  132.       }
  133.   }
  134. }  
  135. 暂时无法判断打印机状态,如缺纸,打印机未开。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值