【问题标题】:C# LPT inpout32.dllC# LPT inpout32.dll
【发布时间】:2011-01-05 18:43:03
【问题描述】:

我没有收到任何错误或异常。

一个窗口中的按钮:

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

和inpout.dll接口:

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

怎么了? D2 上的 LED 一直亮。

我有 Windows 7 x64 Ultimate。

【问题讨论】:

    标签: c# .net parallel-port lpt


    【解决方案1】:

    对于 x64,您应该使用“InpOutx64.dll”。

    访问:http://www.highrez.co.uk/Downloads/InpOut32/default.htm 在那里您可以阅读更多内容并找到示例。

    【讨论】:

      【解决方案2】:

      如果有人需要,可以使用代码。

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

      【讨论】:

      • 我在尝试拨打 IsInpOutDriverOpen_x64() 时收到 System.EntryPointNotFoundException: Unable to find an entry point named 'IsInpOutDriverOpen' in DLL 'inpoutx64.dll'.
      • 现在他们有了这个BOOL _stdcall IsXP64Bit(); //Returns TRUE if the OS is 64bit (x64) Windows.,它是两个dll文件的一部分。
      【解决方案3】:

      当你弄错时,你不会得到异常,最多是蓝屏。选择以下之一:

      • 您使用了错误的地址(0x3bc,0x2f8)
      • 您的 LED 接线错误
      • 你为了获取硬件而闯入了错误的博物馆

      这个问题的文档记录太差,无法帮助您超越这一点。

      【讨论】:

      • 没有。第一个答案是正确的。我使用了错误版本的 dll。
      • 嗯,在 64 位进程中使用 32 位 DLL 总是会产生异常。
      【解决方案4】:

      我解决了旧笔记本电脑上 Windows 2000 上的 LPT 端口问题,无法设置数据端口 (pin2-pin9)。

      使用这个导入函数:

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

      每次重新启动或重新启动 Windows 后,我都必须调用此行:

      Out32(0x378 + 2, 0x00);
      

      以便端口正常工作。我认为问题在于双向设置(0x37A 上第 6 位的控制端口)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 2022-01-23
        相关资源
        最近更新 更多