using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Globalization; //allows for use of NumberStyles using Microsoft.Win32; //for registry access namespace ACCES32Sample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string Company; //The Company name is part of the registry path used //to discover the card name associated with the device ID. private uint[] Addresses; //This array will hold the important base addresss for //each card found. That way when the user changes the selection //in the CardList we can update the address to use for reads //and writes. //The following 4 methods are for the read and write buttons. //These demonstrate the use of InPort and OutPort functions private void ReadByteBTN_Click(object sender, EventArgs e) { uint address = GetAddr(); ReadValueLBL.Text = ACCES32.InPortB(address).ToString("x2"); StatusLabel.Text = "Byte size value read."; } private void ReadWordBTN_Click(object sender, EventArgs e) { uint address = GetAddr(); ReadValueLBL.Text = ACCES32.InPort(address).ToString("x4"); StatusLabel.Text = "Word size value read."; } private void WriteByteBTN_Click(object sender, EventArgs e) { uint address = GetAddr(); byte value; value = byte.Parse(WriteText.Text, NumberStyles.HexNumber); ACCES32.OutPortB(address, value); StatusLabel.Text = "Byte size value written."; } private void WriteWordBTN_Click(object sender, EventArgs e) { uint address = GetAddr(); ushort value; value = ushort.Parse(WriteText.Text, NumberStyles.HexNumber); ACCES32.OutPort(address, value); StatusLabel.Text = "Word size value written."; } public uint GetAddr() //returns the current address to be used for a read or write { return uint.Parse(AddressText.Text, NumberStyles.HexNumber); } private void Form1_Load(object sender, EventArgs e) { RegistryKey rk = Registry.LocalMachine; int numCards; byte[] raw; //Note: using ACCES32.TPCI_COMMON_CONFIG without then using the new operator will // cause problems. If we don't use new the fields in the structure won't be // initialized so you will get errors when reading from fields that are filled in // when we copy to the AsBytes field of config. ACCES32.TPCI_COMMON_CONFIG config = new ACCES32.TPCI_COMMON_CONFIG(); int outer, inner; string CardLine, CardName; //first get the Company name so we get the right part numbers for the cards. rk = rk.OpenSubKey(@"Software\PCIFind"); Company = Convert.ToString(rk.GetValue("Company")); rk.Close(); //calling rk.Close() will dispose of the rk object. This creates a new one rk = Registry.LocalMachine; //check to see if the key we want exists rk = rk.OpenSubKey(@"Software\PCIFind\NTioPCI\Parameters"); if (rk == null) { return; } //check to see how many cards are in the system numCards = Convert.ToInt16(rk.GetValue("NumDevices")); //no point in continuing if there are no cards if (numCards == 0) return; //create an array of bytes long enough to hold all the cards' information //each card takes 0x40 bytes. raw = new byte[0x40 * numCards]; //create the proper length Addressses array. Addresses = new uint[numCards]; //get the cards' data from the registry raw = (byte[])rk.GetValue("PCICommonConfig"); //read each cards data, get its name and base address, then add that to the //CardList for (outer = 0; outer < numCards ; outer ++) { for (inner = 0; inner < 0x40; inner++) { //have to wrap this in an unsafe block because we are using a fixed //length array. unsafe { config.AsBytes[inner] = raw[inner + (0x40 * outer)]; } } CardName = GetCardName(config.DeviceID); //have to wrap in an unsafe block for fixed length array. unsafe { Addresses[outer] = config.BaseAddresses[2] & 0xFFF8; } CardLine = string.Format("{0} : 0x{1:x4}", CardName, Addresses[outer]); CardList.Items.Add(CardLine); } CardList.SelectedIndex = 0; StatusLabel.Text = string.Format("Program ready. {0} card(s) found.", numCards); } //reads the cards friendly name out of the Registry given its DeviceID //this also requires the Company member variable to be initialized private string GetCardName(ushort DeviceID) { RegistryKey rk = Registry.LocalMachine; string RegPath; string CardKey; RegPath = String.Format("Software\\{0}\\Cardlist", Company); rk = rk.OpenSubKey(RegPath); //the key value pair is [DeviceID]:[Friendly_name] with DeviceID as a 4 digit hex number CardKey = string.Format("{0:x4}", DeviceID); return Convert.ToString(rk.GetValue(CardKey)); } private void CardList_SelectedIndexChanged(object sender, EventArgs e) { AddressText.Text = string.Format("{0:x4}", Addresses[CardList.SelectedIndex]); } } }