C#Lpt端口打印類(lèi)的操作淺析
作者:peak-weng
C#LPT端口打印類(lèi)的操作是什么呢?C#LPT端口打印類(lèi)的操作實(shí)例具體的實(shí)現(xiàn)是什么呢?那么本文就向你介紹具體的內(nèi)容。
C#LPT端口打印類(lèi)的操作是什么呢?首先讓我們看看什么是LPT端口(打印機(jī)專(zhuān)用)?LPT端口是一種增強(qiáng)了的雙向并行傳輸接口,在USB接口出現(xiàn)以前是掃描儀,打印機(jī)最常用的接口。***傳輸速度為1.5Mbps,設(shè)備容易安裝及使用,但是速度比較慢,下面是C#LPT端口打印類(lèi)的操作具體實(shí)例:
- using System;
- using System.Runtime.InteropServices;
- using System.IO;
- namespace printBarcode
- {
- /// <summary>
- /// LPTControl 的摘要說(shuō)明,C#LPT端口打印類(lèi)的操作
- /// </summary>
- public class LPTControl
- {
- public 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
- ); //C#LPT端口打印類(lèi)的操作
- 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)
- {
- int i=0;
- OVERLAPPED x=new OVERLAPPED();
- byte[]mybyte=
- System.Text.Encoding.Default.GetBytes(Mystring);
- return WriteFile(
- iHandle,mybyte,mybyte.Length,ref i,ref x);
- }
- else
- {
- throw new Exception("端口未打開(kāi)!");
- }
- }
- public bool Close()
- {
- return CloseHandle(iHandle);
- }
- }//C#LPT端口打印類(lèi)的操作
- }
C#LPT端口打印類(lèi)的操作的具體步驟就向你介紹到這里,希望對(duì)你了解C#LPT端口打印類(lèi)有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來(lái)源:
博客園