-
C#教程之C#教程之C#编程实现向并口设备发送指令、获取并
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
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;
//创建实例
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 };
//要发送的命令(16进制)
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);
//获取的状态值
}
else
{
MessageBox.Show(
"创建文件失败!"
);
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if
(iHandle > 0)
{
CloseHandle(iHandle);
}
}
}
}
}
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能