using System; using System.IO; using System.Runtime.InteropServices; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using pac8014WNet; namespace pac_i8014W_MagicScan { public partial class Form1 : Form { [System.Runtime.InteropServices.DllImport("coredll.dll")] private static extern UInt32 GetTickCount(); [System.Runtime.InteropServices.DllImport("coredll.dll")] private static extern uint GetModuleFileName(IntPtr hModule, System.Text.StringBuilder lpFileName, int nSize); bool activated = false; private static Int16 MAX_FIFO = 4096; private static Int16 slotIndex = 0, jumper = 0, maxCh = 8; Int16[] chArr = new Int16[16]; Int16[] gainArr = new Int16[16]; Int16 scanChCount; UInt32 T1, T2; float sampleRate, realsampleRate = 0; private static int runCnt = 0; short scanMode, triggerSource, triggerState; short[] hexData = new short[8192]; string[] GainStr = { "+/-10V", "+/-5V", "+/-2.5V", "+/-1.25V", "+/-20mA" }; string[] ModeStr = { "M1:Standard ", "M2:Sample and Hold " }; string[] SourceStr = { "Software Command", "Internal Interrupt Signal", "External Trigger Signal" }; string[] StateStr = { "High", "Low" }; bool ConfigFlag = false; Int32 TargetCnt=1000,totalScanned = 0; DateTime startTime = DateTime.Now; private string GetAppPath() { string csCurDir = ""; System.Text.StringBuilder sb = new System.Text.StringBuilder(256); if (GetModuleFileName(IntPtr.Zero, sb, sb.Capacity) > 0) csCurDir = sb.ToString(); else return string.Empty; int iStart = 0; while (!csCurDir.EndsWith(@"\")) { iStart = csCurDir.Length - 1; csCurDir = csCurDir.Remove(iStart, 1); } return csCurDir; } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { int i; ComboBox[] cboType = { cboType0, cboType1, cboType2, cboType3, cboType4, cboType5, cboType6, cboType7, cboType8, cboType9, cboType10, cboType11, cboType12, cboType13, cboType14, cboType15 }; ComboBox[] cboChArray = { cboChArr0, cboChArr1, cboChArr2, cboChArr3, cboChArr4, cboChArr5, cboChArr6, cboChArr7, cboChArr8, cboChArr9, cboChArr10, cboChArr11, cboChArr12, cboChArr13, cboChArr14, cboChArr15 }; cboChCnt.SelectedIndex = 0; cboScanMode.SelectedIndex = 0; cboTrigSrc.SelectedIndex = 0; cboTrigState.SelectedIndex = 0; for (i = 0; i < 16; i++) { cboType[i].SelectedIndex = 0; cboChArray[i].SelectedIndex = 0; } } private void cboSlot_SelectedIndexChanged(object sender, EventArgs e) { if(activated) SearchIO(cboSlot.SelectedIndex); } private bool SearchIO(int slot) { int ret = -1; string[] SingleEndedStr = new string[2]; SingleEndedStr[0] = "Differential"; SingleEndedStr[1] = "Single Ended"; ret = pac8014W.Init((Int16)cboSlot.SelectedIndex); if (ret == 0) { slotIndex = (Int16)cboSlot.SelectedIndex; jumper = pac8014W.SingleEndJumper(slotIndex); lbJumper.Text = SingleEndedStr[jumper]; return true; } else { MessageBox.Show("I-8014W is not at slot " + cboSlot.SelectedIndex.ToString()); return false; } } private void btnStart_Click(object sender, EventArgs e) { short ret; Int32 readFromFIFO = 0; if (!ConfigFlag) { MessageBox.Show("Please Configure the Magic Scan Parameters first", "Error"); return; } //Send external trigger for I-8014W module if (triggerSource == 2) { lstMsg.Items.Add(String.Format("Wait for external trigger signal source to {0} .....", StateStr[triggerState])); } hexData = new Int16[TargetCnt]; totalScanned = 0; pac8014W.StartMagicScan(slotIndex); T1 = GetTickCount(); ret = pac8014W.ReadFIFO_BlockMode(slotIndex, hexData, TargetCnt , ref readFromFIFO);// <=== get data from FIFO ,long* dataCnt txtDataCnt.Text = readFromFIFO.ToString(); if (ret == (Int16)ErrorCode.FIFO_LATCHED || (readFromFIFO != TargetCnt) ) { T2 = GetTickCount() - T1; pac8014W.StopMagicScan(slotIndex); MessageBox.Show("Please try to use lower sample rate and try again", "FIFO Latched"); } if (readFromFIFO > 0) { totalScanned = readFromFIFO; T2 = GetTickCount() - T1; pac8014W.StopMagicScan(slotIndex); } txtTicks.Text = T2.ToString(); ShowAI(); } private void ShowAI() { int i; float calibratedAI = 0; lstData.Items.Clear(); //Console.Write("Start to Print all data:\n\n\r"); for (i = 0; i < totalScanned; i++) { pac8014W.CalibrateData(slotIndex, gainArr[i % scanChCount], hexData[i], ref calibratedAI); lstData.Items.Add(String.Format("[{0}]CH[{1}]= {2}",i, chArr[i % scanChCount], calibratedAI)); } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void SaveAI() { int i; string tStr = String.Format("{0:00}_{1:00}_{2:00}_{3:00}_{4:00}", DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); string fPath = GetAppPath() + "\\SampleData" + tStr +".csv"; string data = string.Empty; StreamWriter Dsw = new StreamWriter(fPath, false, System.Text.Encoding.Default); float calibratedAI = 0; // Console.Write("Start to Print all data:\n\n\r"); for (i = 0; i < totalScanned; i++) { pac8014W.CalibrateData(slotIndex, gainArr[i % scanChCount], hexData[i], ref calibratedAI); if ((i % scanChCount) != 0) data += "," + calibratedAI.ToString(); else { if (i != 0) Dsw.WriteLine(data); data = i.ToString() + "," + calibratedAI.ToString(); } } Dsw.WriteLine(data); Dsw.Flush(); Dsw.Close(); } private void btnSet_Click(object sender, EventArgs e) { Int16 i, ret; ComboBox[] cboType = { cboType0, cboType1, cboType2, cboType3, cboType4, cboType5, cboType6, cboType7, cboType8, cboType9, cboType10, cboType11, cboType12, cboType13, cboType14, cboType15 }; ComboBox[] cboChArray = { cboChArr0, cboChArr1, cboChArr2, cboChArr3, cboChArr4, cboChArr5, cboChArr6, cboChArr7, cboChArr8, cboChArr9, cboChArr10, cboChArr11, cboChArr12, cboChArr13, cboChArr14, cboChArr15 }; lstMsg.Items.Clear(); string[] SingleEndedStr = new string[2]; scanChCount = (Int16)(cboChCnt.SelectedIndex + 1); // scan channel count must begin at 1 for (i = 0; i < scanChCount; i++) { chArr[i] = (Int16)cboChArray[i].SelectedIndex; gainArr[i] = (Int16)cboType[i].SelectedIndex; lstMsg.Items.Add(String.Format("Selected Magic Scan Channel Gain Array[{0}] C = {1} G ={2}", i, chArr[i], gainArr[i])); } // Sample channel counts // scanChCount can be 1 ~ 16 (max =16 for both Differential and Single Ended) sampleRate = Convert.ToSingle(txtSampleRate.Text); scanMode = (Int16)(cboScanMode.SelectedIndex + 1); // trigger source 0, start magic scan via polling Mode, software command can only trigger one slot I-8014 // trigger source 1, start magic scan via internal interrupt signal Mode, internal interrupt signal can trigger all I-8014Ws on slots at the same time. // trigger source 2, start magic scan via external trigger signal Mode, extern trigger can also trigger all I-8014Ws on slots at the same time. triggerSource = (Int16)cboTrigSrc.SelectedIndex; // triggerState only related with external DI trigger signal. // if triggerState 0, external DI trigger signal must be high to start magic scan (default) // if triggerState 1, external DI trigger signal must be low to start magic scan triggerState = (Int16)cboTrigState.SelectedIndex; // When Configure Sample Rate of Magic Scan, the result sample rate may be different from input sample rate // Note i8014W_ConfigMagicScan only tell I-8014W how to start magic scan,it will not start magic scan // We have to call i8014W_StartMagicScan to start up the magic scan. pac8014W.ConfigMagicScan(slotIndex, chArr, gainArr, scanChCount, sampleRate, scanMode, triggerSource, triggerState, ref realsampleRate); lstMsg.Items.Add(String.Format("The Magic Scan Configurations of I-8014W are:")); TargetCnt = Convert.ToInt16(txtTargetCnt.Text); lstMsg.Items.Add(String.Format("Scan channel count = {0}, Total sample count ={1}", scanChCount, TargetCnt)); for (i = 0; i < scanChCount; i++) { lstMsg.Items.Add(String.Format("CH[{0}]= {1} Gain[{2}]= {3} ( {4} )", i, chArr[i], i, gainArr[i], GainStr[gainArr[i]])); } lstMsg.Items.Add(String.Format("Scan Mode = {0} ( {1} )", scanMode, ModeStr[scanMode - 1])); lstMsg.Items.Add(String.Format("Trigger Source = {0} ( {1} )", triggerSource, SourceStr[triggerSource])); if (triggerSource == 2) lstMsg.Items.Add(String.Format("Trigger State = {0} ( {1} )", triggerState, StateStr[triggerState])); else lstMsg.Items.Add(String.Format("Trigger State = {0} ( No need for External Trigger Signal )", triggerState)); lstMsg.Items.Add(String.Format("Set Sample Rate = {0} Real Sample Rate = {1}", sampleRate, realsampleRate)); ConfigFlag = true; } private void btnStop_Click(object sender, EventArgs e) { SaveAI(); } private void Form1_Activated(object sender, EventArgs e) { activated = true; } } }