C#編程實(shí)現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(tài)
更新時(shí)間:2015年06月16日 11:31:35 投稿:junjie
這篇文章主要介紹了C#編程實(shí)現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(tài),本文直接給出實(shí)例代碼,需要的朋友可以參考下
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace ParallelPort
{
public partial class Form1 : Form
{
const uint GENERIC_READ = 0x80000000;
const uint GENERIC_WRITE = 0x40000000;
const uint FILE_ATTRIBUTE_NORMAL = 0x80;
#region win32 API
[DllImport("kernel32.dll ")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
uint dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll ")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
int lpOverlapped
);
[DllImport("kernel32.dll ")]
private static extern bool DefineDosDevice(
int dwFlags,
string lpDeviceName,
string lpTargetPath);
[DllImport("kernel32.dll ")]
private static extern bool CloseHandle(
int hObject
);
[DllImport("kernel32.dll ")]
private static extern bool ReadFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
int lpOverlapped
);
#endregion
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int iHandle = -1;
try
{
int i = 0;
//創(chuàng)建實(shí)例
DefineDosDevice(0x00000001, "LptPortName",@"\Device\Parallel0");
iHandle = CreateFile(@"\\.\LptPortName",GENERIC_READ | GENERIC_WRITE, 0, 0, 3, FILE_ATTRIBUTE_NORMAL, 0);
if (iHandle !=-1)
{
byte[] mybyte = new byte[3]{ 0x12, 0x14, 0x14 };//要發(fā)送的命令(16進(jìn)制)
WriteFile(iHandle, mybyte, mybyte.Length, ref i, 0);
byte[] mybyte1 = new byte[3];
string content = String.Empty;
int j = 0;
ReadFile(iHandle, mybyte1, 3, ref j, 0);
if (mybyte1 != null)
{
foreach(var tempByte in mybyte1)
{
content += tempByte.ToString();
}
}
MessageBox.Show(content);//獲取的狀態(tài)值
}
else
{
MessageBox.Show("創(chuàng)建文件失敗!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (iHandle > 0)
{
CloseHandle(iHandle);
}
}
}
}
}
相關(guān)文章
WinForm DataGridView控件隔行變色的小例子
WinForm的DataGridView控件設(shè)置行的顏色2013-03-03
C#難點(diǎn)逐個(gè)擊破(6):C#數(shù)據(jù)類型與.net framework數(shù)據(jù)類型
最近開始看Illustrator C#2008,這真是一本好書,我讀計(jì)算機(jī)書籍這么多了,能讓我稱為好書的沒有多少。2010-02-02
c#泛型序列化對(duì)象為字節(jié)數(shù)組的示例
這篇文章主要介紹了c#泛型序列化對(duì)象為字節(jié)數(shù)組的示例,需要的朋友可以參考下2014-04-04
C#實(shí)現(xiàn)向函數(shù)傳遞不定參數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)向函數(shù)傳遞不定參數(shù)的方法,涉及C#操作函數(shù)參數(shù)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#設(shè)置或驗(yàn)證PDF文本域格式的方法詳解
PDF中的文本域可以通過設(shè)置不同格式,用于顯示數(shù)字、貨幣、日期、時(shí)間、郵政編碼、電話號(hào)碼和社保號(hào)等等。本文將介紹如何通過C#設(shè)置或驗(yàn)證PDF文本域格式,需要的可以參考一下2022-01-01
VS2015為console.readkey添加代碼片段的方法
這篇文章主要介紹了VS2015為console.readkey添加代碼片段的方法,需要的朋友可以參考下2016-12-12
通過C#實(shí)現(xiàn)發(fā)送自定義的html格式郵件
本篇文章主要介紹了通過C#實(shí)現(xiàn)發(fā)送自定義的html格式郵件,詳細(xì)的介紹了發(fā)送HTML格式郵件的方法,有興趣的可以了解一下。2017-02-02

