简体   繁体   English

C#LPT inpout32.dll

[英]C# LPT inpout32.dll

I don't get any error or exception. 我没有得到任何错误或异常。

Button in one Window: 一个窗口中的按钮:

private void button1_Click(object sender, EventArgs e)
{
    ControlPort.Output(0x378, 0xff);
}

and inpout.dll interface: 和inpout.dll接口:

class ControlPort
{
    [DllImport("inpout32.dll", EntryPoint = "Out32")]
    public static extern void Output(int adress, int value);
}

What is wrong? 怎么了? LED on D2 is on all the time. D2上的LED一直亮着。

I have Windows 7 x64 Ultimate. 我有Windows 7 x64旗舰版。

For x64 you should use "InpOutx64.dll". 对于x64,您应该使用“InpOutx64.dll”。

Visit: http://www.highrez.co.uk/Downloads/InpOut32/default.htm There you can read more and find samples. 访问: http//www.highrez.co.uk/Downloads/InpOut32/default.htm在那里,您可以阅读更多内容并查找样本。

Working code if somebody needs it. 工作代码,如果有人需要它。

 using System;
using System.Runtime.InteropServices;

namespace ParallelPort
{
    public class PortAccess
    {
        //inpout.dll

        [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);

        //inpoutx64.dll

        [DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
        private static extern UInt32 IsInpOutDriverOpen_x64();

        [DllImport("inpoutx64.dll", EntryPoint = "Out32")]
        private static extern void Out32_x64(short PortAddress, short Data);

        [DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
        private static extern char Inp32_x64(short PortAddress);

        [DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUshort")]
        private static extern void DlPortWritePortUshort_x64(short PortAddress, ushort Data);

        [DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUshort")]
        private static extern ushort DlPortReadPortUshort_x64(short PortAddress);

        [DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUlong")]
        private static extern void DlPortWritePortUlong_x64(int PortAddress, uint Data);

        [DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUlong")]
        private static extern uint DlPortReadPortUlong_x64(int PortAddress);

        [DllImport("inpoutx64.dll", EntryPoint = "GetPhysLong")]
        private static extern bool GetPhysLong_x64(ref int PortAddress, ref uint Data);

        [DllImport("inpoutx64.dll", EntryPoint = "SetPhysLong")]
        private static extern bool SetPhysLong_x64(ref int PortAddress, ref uint Data);

        private bool _X64;
        private short _PortAddress;

        public PortAccess(short PortAddress)
        {
            _X64 = false;
            _PortAddress = PortAddress;

            try
            {
                uint nResult = 0;
                try
                {
                    nResult = IsInpOutDriverOpen();
                }
                catch (BadImageFormatException)
                {
                    nResult = IsInpOutDriverOpen_x64();
                    if (nResult != 0)
                        _X64 = true;

                }

                if (nResult == 0)
                {
                    throw new ArgumentException("Unable to open InpOut32 driver");
                }
            }
            catch (DllNotFoundException)
            {
                throw new ArgumentException("Unable to find InpOut32.dll");
            }
        }

        //Public Methods
        public void Write(short Data)
        {
            if (_X64)
            {
                Out32_x64(_PortAddress, Data);
            }
            else
            {
                Out32(_PortAddress, Data);
            }
        }

        public byte Read()
        {
            if (_X64)
            {
                return (byte)Inp32_x64(_PortAddress);
            }
            else
            {
                return (byte)Inp32(_PortAddress);
            }
        }
    }
}

You are not going to get an exception when you get this wrong, at most a blue screen. 如果你弄错了,你不会得到例外,最多是蓝屏。 Pick one of: 选择以下之一:

  • you are using the wrong address (0x3bc, 0x2f8) 你使用了错误的地址(0x3bc,0x2f8)
  • you wired the LED wrong 你错误地连接了LED
  • you broke into the wrong museum to get the hardware 你闯进错误的博物馆拿到硬件

The question is too poorly documented to help you beyond this. 这个问题记录太差,无法帮助你解决这个问题。

I resolved a problem with LPT port on Windows 2000 on my old laptop, where the data port (pin2-pin9) could not be set. 我解决了旧笔记本电脑上Windows 2000上的LPT端口问题,无法设置数据端口(pin2-pin9)。

Using this imported function: 使用此导入的功能:

[DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Out32(int address, int value);

after every reboot or restart of Windows I have to call this line: 每次重启或重新启动Windows后,我都要调用此行:

Out32(0x378 + 2, 0x00);

so that the port works properly. 这样端口就能正常工作。 I think the problem is in the bi-directional settings (control port at 6th bit on 0x37A). 我认为问题出在双向设置(控制端口位于0x37A的第6位)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM