2011-01-05 11 views
6

Non ricevo alcun errore o eccezione.C# LPT inpout32.dll

pulsante in una finestra:

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

e l'interfaccia inpout.dll:

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

Cosa c'è di sbagliato? Il LED su D2 è sempre acceso.

Ho Windows 7 x64 Ultimate.

risposta

3

Non si otterrà un'eccezione quando si sbaglia, al massimo una schermata blu. Scegli uno di:

  • si sta utilizzando l'indirizzo sbagliato (0x3bc, 0x2f8)
  • si WIRED il LED sbagliato
  • hai rotto nel museo sbagliato per ottenere la domanda l'hardware

è troppo poco documentato per aiutarti oltre.

+0

Niente di tutto questo. La prima risposta è corretta. Stavo usando la versione errata dll. – Hooch

+0

Hmm, l'utilizzo di una DLL a 32 bit in un processo a 64 bit produce sempre un'eccezione. –

5

Codice di lavoro se qualcuno ne ha bisogno.

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

Ottiene 'System.EntryPointNotFoundException: Impossibile trovare un punto di ingresso denominato 'IsInpOutDriverOpen' nella DLL 'inpoutx64.dll'. Durante il tentativo di chiamare' IsInpOutDriverOpen_x64() '. –

+0

Ora hanno questo 'BOOL _stdcall IsXP64Bit(); \t \t \t // Restituisce TRUE se il SO è a 64 bit (x64) Windows. Che fa parte di entrambi i file dll. – SSpoke

1

ho risolto un problema con la porta LPT in Windows 2000 sul mio vecchio computer portatile, in cui non è stato possibile impostare la porta dati (pin 2-pin9).

Usando questa funzione importati:

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

dopo ogni riavvio o riavviare Windows devo chiamare questa linea:

Out32(0x378 + 2, 0x00); 

in modo che la porta funziona correttamente. Penso che il problema sia nelle impostazioni bidirezionali (porta di controllo al sesto bit su 0x37A).