2011-12-14 46 views
0

我在C#中更新最初用VC++ V1.0爲DOS6編寫的一些軟件。C# - 讀取並行端口狀態(簡單的按鈕開關)

該軟件的一個方面是檢查並行端口,在該端口連接一個簡單的按鈕開關。我不知道目前的交換機所連接的兩個引腳,但我對老程序的源,相關章節,其中低於...

#define switch_mask  0x10 

void main(int argc, char *argv[]) 
{ 
    int NewState = 0, OldState = 0; 

    /* Check switch */ 
    NewState = _bios_printer (_PRINTER_STATUS, 0, 0); 

    if (NewState != OldState) 
    { 
    checkParallelPort (NewState); 
    OldState = NewState; 
    }  
} 

void checkParallelPort (int portState) 
{ 
    int SwitchState; 
    /* Switch bit is Active LOW */ 
    SwitchState = (portState & switch_mask) ? 1 : 0; 
} 

現在_bios_printer(bios.h內)在C#中顯然不適用於我,但我正在努力尋找可以完成這項簡單任務的替代方案。

_bios_printer上的信息是here。我已經做了大量的搜索/從並行端口讀取/寫入.Net,但似乎沒有提供我的端口狀態。

另外,您可以從這段代碼(以及它如何檢查'狀態')得出兩個開關線連接在DB25插頭上的結論嗎?

如果有人對此有任何幫助/建議,我將不勝感激。 非常感謝

+1

_「最初用VC++ V1.0編寫的DOS6」_,祝你好運。 – 2011-12-14 09:06:55

回答

1

這似乎是檢查「錯誤」,銷15 IIRC,這是內部上拉,所以你的開關應拉低銷15引腳15和18

之間存在連接它有些驅動程序可用於讀取I/O映射端口。你將不得不進口,使DLL的調用,然後幾乎可以肯定輪詢針:((

我真希望這個接口是死了,埋

+0

感謝您的幫助,我使用了您的答案,並提出了以下解決方案! – Ian 2011-12-20 09:33:37

0

感謝您的答覆這裏是我結束了.. 。

我曾經在CodeProject本教程... http://www.codeproject.com/KB/vb/Inpout32_read.aspx 當轉換爲C#中,我用類似下面(道歉,如果有一個錯誤 - 我「轉述」下面的代碼我已經結束了什麼與 - 它適用於我!)

private void button1_Click(object sender, EventArgs e) 
{ 

     short InReg = 0x379; // Location of Port Status register 
     int NewState;   // int named NewState 

     NewState = InpOut32_Declarations.Inp(InReg); //Now NewState has the values in 'Status port' 

     MessageBox.Show(NewState); //A popup will indicate the current value of NewState 

} 

static class InpOut32_Declarations 
{ 
    [DllImport("inpout32.dll", EntryPoint = "Inp32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 

    //Inp and Out declarations for port I/O using inpout32.dll. 

    public static extern int Inp(ushort PortAddress); 
    [DllImport("inpout32.dll", EntryPoint = "Out32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 

    public static extern void Out(ushort PortAddress, short Value); 

} 

有趣的是,由於我的並行端口起始於0xE800而不是0x378,因此我不得不修改inpout32.dll的源代碼,因爲out32方法只接受短路,而0xE800對於短路來說太大了!將其更改爲無符號短。

感謝您的幫助