日期:2014-05-20  浏览次数:20684 次

高分求并口读写实例!!!!!!!
最好是测试过的。
并口~

------解决方案--------------------
不能直接对并口操作的,需要一个DLL,这里你能下到,http://www.logix4u.net/inpout32.htm
下面是我写的代码,刚学C#,写的很乱....
IO.cs
///////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


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

[DllImport( "inpout32.dll ", EntryPoint = "Inp32 ")]
public static extern int Input(int adress);
}
}
////////////////////////////////////////////////


Form1.cs
/////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool key,kEnter;
private void button2_Click(object sender, EventArgs e)
{
IO.Output(int.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowHexSpecifier), int.Parse(textBox2.Text, System.Globalization.NumberStyles.AllowHexSpecifier));
}

private void button1_Click(object sender, EventArgs e)
{
int a;
a=IO.Input(int.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowHexSpecifier));
textBox2.Text = a.ToString( "X ");

}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (key==true )
e.Handled = true;
if (kEnter == true)
{
button1.Focus();
SendKeys.Send( "{ENTER} ");
kEnter = false;
}
}

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (key == true)
e.Handled = true;
if (kEnter == true)
{
button2.Focus();
SendKeys.Send( "{ENTER} ");
kEnter = false;
}

}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
key = false;
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{

if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode != Keys.Back && e.KeyCode != Keys.Enter && e.KeyCode != Keys.A && e.KeyCode != Keys.B && e.KeyCode != Keys.C && e.KeyCode != Keys.D && e.KeyCode != Keys.E && e.KeyCode != Keys.F)
{
key = true;
}
}
}

if (e.KeyCode == Keys.Enter)
kEnter = true;


}

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
key = false;
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{