• 大小: 12.63 KB
    文件类型: .rar
    金币: 1
    下载: 1 次
    发布日期: 2024-10-21
  • 语言: C#
  • 标签: C#  并口  封装  打印  

资源简介

在C#中操作并口,已经封装,直接调用该DLL就可以直接使用,使用时,直接使用 Write 就可以往并口发送数据,如果并口连接的是打印机,只要发送的字符中打印机指令,可以直接打印;如果是其它的设备,也是相同的功能。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


//namespace LPTControl
//{
    public class LPTControl
    {
        [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
          );
        private int iHandle;
        public bool Open()
        {
            iHandle = CreateFile(“lpt1 “ 0x40000000 0 0 3 0 0);
            if (iHandle != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Write(String Mystring)
        {
            if (iHandle != -1)
            {
                OVERLAPPED x = new OVERLAPPED();
                int i = 0;

                byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                bool b = WriteFile(iHandle mybyte mybyte.Length ref   i ref   x);
                return b;
            }
            else
            {
                throw new Exception(“不能连接到打印机! “);
            }
        }
        public bool Write(byte[] mybyte)
        {
            if (iHandle != -1)
            {
                OVERLAPPED x = new OVERLAPPED();
                int i = 0;
                WriteFile(iHandle mybyte mybyte.Length
                  ref   i ref   x);
                return true;
            }
            else
            {
                throw new Exception(“不能连接到打印机! “);
            }
        }
        public bool Close()
        {
            return CloseHandle(iHandle);
        }
    }
//}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      16384  2009-10-22 13:03  LPTControl\bin\Debug\LPTControl.dll

     文件      11776  2009-10-22 13:03  LPTControl\bin\Debug\LPTControl.pdb

     文件       2544  2009-10-22 13:03  LPTControl\Class1.cs

     文件       1957  2009-10-22 12:33  LPTControl\LPTControl.csproj

     文件        908  2009-10-22 12:33  LPTControl\LPTControl.sln

    ..A..H.      9728  2009-10-22 18:12  LPTControl\LPTControl.suo

     文件      16384  2009-10-22 13:03  LPTControl\obj\Debug\LPTControl.dll

     文件      11776  2009-10-22 13:03  LPTControl\obj\Debug\LPTControl.pdb

     文件        146  2009-10-22 13:03  LPTControl\obj\LPTControl.csproj.FileList.txt

     文件       1339  2009-10-22 12:33  LPTControl\Properties\AssemblyInfo.cs

     目录          0  2009-10-22 12:33  LPTControl\obj\Debug\TempPE

     目录          0  2009-10-22 13:03  LPTControl\bin\Debug

     目录          0  2009-10-22 13:03  LPTControl\obj\Debug

     目录          0  2009-10-22 12:33  LPTControl\bin

     目录          0  2009-10-22 13:03  LPTControl\obj

     目录          0  2009-10-22 12:33  LPTControl\Properties

     目录          0  2009-10-22 13:03  LPTControl

----------- ---------  ---------- -----  ----

                72942                    17


评论

共有 条评论