2016-10-14 2 views
0

Я хочу, чтобы порт 0xE020 (Hex -> Dec = 57.376), но он за границей для краткости. Теперь мне это нужно, чтобы протестировать параллельные порты расширения PCI-E.Можно ли адресовать высокие порты Inpout32.dll?

Благодаря ответ на этом вопросе (https://stackoverflow.com/a/4618383/3391678) я использую следующий рабочий код для 32-битных сред:

using System; 
using System.Runtime.InteropServices; 

namespace LPTx86 
{ 
    public class LPT 
    { 
     //inpout.dll 

     [DllImport("inpout32.dll")] 
     private static extern void Out32(short PortAddress, short Data); 

     [DllImport("inpout32.dll")] 
     private static extern char Inp32(short PortAddress); 

     private short _PortAddress; 

     public LPT(short PortAddress) 
     { 
      _PortAddress = PortAddress; 
     } 

     public void Write(short Data) 
     { 
      Out32(_PortAddress, Data); 
     } 

     public byte Read() 
     { 
      return (byte)Inp32(_PortAddress); 
     } 
    } 
} 

И для 64 битных сред:

using System; 
using System.Runtime.InteropServices; 

namespace LPTx64 
{ 
    class LPT 
    { 
     [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); 

     private short _PortAddress; 

     public LPT64(short PortAddress) 
     { 
      _PortAddress = PortAddress; 
     } 

     public void Write(short Data) 
     { 
      Out32_x64(_PortAddress, Data); 
     } 

     public byte Read() 
     { 
      return (byte)Inp32_x64(_PortAddress); 
     } 
    } 
} 

(Да, я извлекал каждый элемент безопасности и проверка работоспособности, мое второе имя - «Опасность»;))

Возможно ли, и как я могу указать порты, которые не вписываются в короткий?

+1

Используйте 'ushort', чтобы получить ах ead, номер порта не является знаковым значением. –

+0

Я заглянул в файлы заголовков и да, это ухорт, я лучше исправлю это, пока что-то не сломается ... –

ответ

0

Возможно:

Я попробовал и изменить тип переменной порта от короткого замыкания на ИНТ, я проверил это и это похоже на работу:

Edit: рефакторинг код, чтобы объединить данные/контроль/состояние портов в один пакет

using System; 
using System.Runtime.InteropServices; 

namespace LPTtestdrive 
{ 
    class LPT_X 
    { 
     //inpout.dll 

     [DllImport("inpout32.dll")] 
     private static extern UInt32 IsInpOutDriverOpen(); 

     [DllImport("inpout32.dll")] 
     private static extern void Out32(int PortAddress, short Data); 

     [DllImport("inpout32.dll")] 
     private static extern char Inp32(int PortAddress); 

     //inpoutx64.dll 

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

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

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

     private bool _X64; 
     private int DataAddress; 
     private int StatusAddress; 
     private int ControlAddress; 

     public LPT_X(int PortAddress) 
     { 
      DataAddress = PortAddress; 
      StatusAddress = (int)(DataAddress + 1); 
      ControlAddress = (int)(DataAddress + 2); 

      //now the code tries to determine if it should use inpout32.dll or inpoutx64.dll 
      uint nResult = 0; 

      try 
      { 
       Console.WriteLine("trying 32 bits"); 
       nResult = IsInpOutDriverOpen(); 
       if (nResult != 0) 
       { 
        Console.WriteLine("using 32 bits"); 
        return; 
       } 
      } 
      catch (BadImageFormatException) 
      { 
       Console.WriteLine("32 bits failed"); 
      } 
      catch (DllNotFoundException) 
      { 
       throw new ArgumentException("Unable to find InpOut32.dll"); 
      } 
      catch (Exception exc) 
      { 
       Console.WriteLine(exc.Message); 
      } 

      try 
      { 
       Console.WriteLine("trying 64 bits"); 
       nResult = IsInpOutDriverOpen_x64(); 
       if (nResult != 0) 
       { 
        Console.WriteLine("using 64 bits"); 
        _X64 = true; 
        return; 
       } 
      } 
      catch (BadImageFormatException) 
      { 
       Console.WriteLine("64 bits failed"); 
      } 
      catch (DllNotFoundException) 
      { 
       throw new ArgumentException("Unable to find InpOutx64.dll"); 
      } 
      catch (Exception exc) 
      { 
       Console.WriteLine(exc.Message); 
      } 

      throw new ArgumentException("Unable to open either inpout32 and inpoutx64"); 
     } 

     public void WriteData(short Data) 
     { 
      if (_X64) 
      { 
       Out32_x64(DataAddress, Data); 
      } 
      else 
      { 
       Out32(DataAddress, Data); 
      } 
     } 

     public void WriteControl(short Data) 
     { 
      if (_X64) 
      { 
       Out32_x64(ControlAddress, Data); 
      } 
      else 
      { 
       Out32(ControlAddress, Data); 
      } 
     } 

     public byte ReadData() 
     { 
      if (_X64) 
      { 
       return (byte)Inp32_x64(DataAddress); 
      } 
      else 
      { 
       return (byte)Inp32(DataAddress); 
      } 
     } 

     public byte ReadControl() 
     { 
      if (_X64) 
      { 
       return (byte)Inp32_x64(ControlAddress); 
      } 
      else 
      { 
       return (byte)Inp32(ControlAddress); 
      } 
     } 

     public byte ReadStatus() 
     { 
      if (_X64) 
      { 
       return (byte)Inp32_x64(StatusAddress); 
      } 
      else 
      { 
       return (byte)Inp32(StatusAddress); 
      } 
     } 
    } 
} 

Используйте это так:

LPT_X LPT; 
int read; 
int Adress = 0xE020; 
try 
{ 
    LPT = new LPT_X(Address); 
    LPT.WriteData(251); 
    LPT.WriteControl(0); 

    read = LPT.ReadStatus(); 
    ... 
} 
Смежные вопросы