資源簡介
在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
- 上一篇:C# 讀取USB 信息
- 下一篇:C#創建word并保存
評論
共有 條評論