Программирование
- 2 ответа
- 0 вопросов
1
Вклад в тег
using System;
using System.Runtime.InteropServices;
namespace PortAccess {
public class ParallelPort {
[DllImport("inpout32.dll")]
private static extern UInt32 IsInpOutDriverOpen();
[DllImport("inpout32.dll")]
private static extern void Out32(short portAddress, short data);
[DllImport("inpout32.dll")]
private static extern char Inp32(short portAddress);
[DllImport("inpout32.dll")]
private static extern void DlPortWritePortUshort(short portAddress, ushort data);
[DllImport("inpout32.dll")]
private static extern ushort DlPortReadPortUshort(short portAddress);
[DllImport("inpout32.dll")]
private static extern void DlPortWritePortUlong(int portAddress, uint data);
[DllImport("inpout32.dll")]
private static extern uint DlPortReadPortUlong(int portAddress);
[DllImport("inpoutx64.dll")]
private static extern bool GetPhysLong(ref int portAddress, ref uint data);
[DllImport("inpoutx64.dll")]
private static extern bool SetPhysLong(ref int portAddress, ref uint data);
[DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
private static extern UInt32 IsInpOutDriverOpenmode();
[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
private static extern void Out32mode(short portAddress, short data);
[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
private static extern char Inp32mode(short portAddress);
[DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUshort")]
private static extern void DlPortWritePortUshortmode(short portAddress, ushort data);
[DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUshort")]
private static extern ushort DlPortReadPortUshortmode(short portAddress);
[DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUlong")]
private static extern void DlPortWritePortUlongmode(int portAddress, uint data);
[DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUlong")]
private static extern uint DlPortReadPortUlongmode(int portAddress);
[DllImport("inpoutx64.dll", EntryPoint = "GetPhysLong")]
private static extern bool GetPhysLongmode(ref int portAddress, ref uint data);
[DllImport("inpoutx64.dll", EntryPoint = "SetPhysLong")]
private static extern bool SetPhysLongmode(ref int portAddress, ref uint data);
private bool mode;
private short portAddress;
public ParallelPort(short portAddress) {
mode = false;
this.portAddress = portAddress;
try {
uint nResult = 0;
try {
nResult = IsInpOutDriverOpen();
}
catch (BadImageFormatException) {
nResult = IsInpOutDriverOpenmode();
if (nResult != 0) {
mode = true;
}
}
if (nResult == 0 ) {
throw new ArgumentException("Unable to open InpOut32 driver");
}
}
catch (DllNotFoundException) {
throw new ArgumentException("Unable to find InpOut32.dll");
}
}
public void Write(short data) {
if (mode) {
Out32mode(portAddress, data);
}
else {
Out32(portAddress, data);
}
}
public byte Read() {
if (mode) {
return (byte)Inp32mode(portAddress);
}
else {
return (byte)Inp32(portAddress);
}
}
}
}