C# - 读取并行端口状态(简单推送开关)

发布于 2024-12-21 03:47:49 字数 962 浏览 2 评论 0原文

我正在用 C# 更新一些最初用 VC++ V1.0 为 DOS6 编写的软件。

该软件的一方面是检查并行端口,其中连接了一个简单的按钮开关。我目前不知道开关连接到哪两个引脚,但我有旧程序的源代码,其相关部分如下...

#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 的信息位于此处。我已经做了很多搜索.Net 中并行端口的读/写操作,但似乎没有任何内容为我提供端口状态。

另外,您能否从这段代码(以及它如何检查“状态”)得出结论,两根开关线连接在 DB25 插头上的位置?

如果有人对此有任何帮助/建议,我将不胜感激。 非常感谢

I am updating in C# some software that was originally written in VC++ V1.0 for DOS6.

One aspect of the software is a check on the parallel port, where a simple push-button switch is connected. I don't currently know to which two pins the switch is connected, but I have the source for the old program, the relevant sections of which are below...

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

Now _bios_printer (within bios.h) is obviously not available to me in C#, but I'm struggling to find an alternative that can do this simple task.

Info on _bios_printer is here. I've done plenty of searching for reading/writing to/from the parallel port in .Net, but nothing seems to provide me with the port status.

Also, can you conclude from this code (and how it checks the 'status') where the two switch wires are connected on the DB25 plug?

I'd be grateful if anyone has some help/advice on this please.
Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2024-12-28 03:47:49

它似乎正在检查“错误”,引脚 15。IIRC,这是内部上拉的,因此您的开关应该下拉引脚 15。将其连接在引脚 15 和 18 之间。

有一些驱动程序可以允许读取 I/ O 地图端口。您将必须导入并进行 DLL 调用,然后几乎肯定会轮询引脚:((

我真希望这个接口已经死掉并被埋葬了!

It seems to be checking 'Error', pin 15. IIRC, this is pulled up internally, so your switch should pull down pin 15. Connect it between pins 15 and 18.

There are some drivers available that allow the reading of the I/O map ports. You will have to import and make DLL calls and then almost certainly poll the pin :((

I do wish this interface was dead and buried!

〗斷ホ乔殘χμё〖 2024-12-28 03:47:49

感谢您的回复。这就是我最终得到的结果...

我在 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 对于短整型来说太大了!将其更改为无符号短。

感谢您的帮助

Thanks for the reply. Here's what I ended up with...

I used this tutorial on CodeProject...
http://www.codeproject.com/KB/vb/Inpout32_read.aspx
When converted to C#, I used something similar to below. (Apologies if there is an error - I 'paraphrased' the code below from what i've ended up with - it works for me!)

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

}

Interestingly, as my Parallel Port starts at 0xE800 and not 0x378, I had to modify the source of inpout32.dll as the out32 method only accepts a short and 0xE800 is too big for a short! changed it to unsigned short.

Thanks for your help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文