using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; namespace AI_Scan_N_Sample_AnalogTrigger { public partial class Form1 : Form { IntPtr hHS; short TriggerMode = 6;//Analog Trigger private string[] AIModeStr = new string[4]; private string[] RangeStr = new string[2]; private string[] ChCntStr = new string[8]; CheckBox[] En_arr = new CheckBox[9]; TextBox[] HL_arr = new TextBox[9]; TextBox[] LL_arr = new TextBox[9]; bool RECV_flag = false; uint datacnt = 0; ushort[] HEX = new ushort[30000000]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { int i = 0; Mode_cmb.Items.Add("Analog Trigger"); Mode_cmb.SelectedIndex = 0; RangeStr[0] = "+/- 5V"; RangeStr[1] = "+/- 10V"; for (i = 0; i < 2; i++) Range_cmb.Items.Add(RangeStr[i]); ChCntStr[0] = "1 Channel"; ChCntStr[1] = "2 Channels"; ChCntStr[2] = "3 Channels"; ChCntStr[3] = "4 Channels"; ChCntStr[4] = "5 Channels"; ChCntStr[5] = "6 Channels"; ChCntStr[6] = "7 Channels"; ChCntStr[7] = "8 Channels"; for (i = 0; i < 8; i++) ChCnt_cmb.Items.Add(ChCntStr[i]); AIModeStr[0] = "Above"; AIModeStr[1] = "Below"; AIModeStr[2] = "Out-Range"; AIModeStr[3] = "In-Range"; for (i = 0; i < 4; i++) AIMode_cmb.Items.Add(AIModeStr[i]); En_arr[0] = En0_ckb; En_arr[1] = En1_ckb; En_arr[2] = En2_ckb; En_arr[3] = En3_ckb; En_arr[4] = En4_ckb; En_arr[5] = En5_ckb; En_arr[6] = En6_ckb; En_arr[7] = En7_ckb; En_arr[8] = EnAll_ckb; HL_arr[0] = HL0_txt; HL_arr[1] = HL1_txt; HL_arr[2] = HL2_txt; HL_arr[3] = HL3_txt; HL_arr[4] = HL4_txt; HL_arr[5] = HL5_txt; HL_arr[6] = HL6_txt; HL_arr[7] = HL7_txt; HL_arr[8] = HLAll_txt; LL_arr[0] = LL0_txt; LL_arr[1] = LL1_txt; LL_arr[2] = LL2_txt; LL_arr[3] = LL3_txt; LL_arr[4] = LL4_txt; LL_arr[5] = LL5_txt; LL_arr[6] = LL6_txt; LL_arr[7] = LL7_txt; LL_arr[8] = LLAll_txt; Disconnect_btn.Enabled = false; ChCnt_cmb.Enabled = false; Range_cmb.Enabled = false; Rate_txt.Enabled = false; Target_txt.Enabled = false; AIMode_cmb.Enabled = false; for (i = 0; i < 9; i++) { En_arr[i].Enabled = false; HL_arr[i].Enabled = false; LL_arr[i].Enabled = false; } LeftCnt_txt.Enabled = false; RightCnt_txt.Enabled = false; msg_list.Items.Add("Please input IP address of PET-7H16M and click 'Connect' button."); } private void connect_btn_Click(object sender, EventArgs e) { hHS = HSDAQNet.Sys.HS_Device_Create(IP_txt.Text); if (hHS == null) { MessageBox.Show("Invalid IP, please input again."); } else { connect_btn.Enabled = false; Disconnect_btn.Enabled = true; ChCnt_cmb.Enabled = true; Range_cmb.Enabled = true; Rate_txt.Enabled = true; Target_txt.Enabled = true; En_arr[8].Enabled = true; HL_arr[8].Enabled = true; LL_arr[8].Enabled = true; LeftCnt_txt.Enabled = true; RightCnt_txt.Enabled = true; msg_list.Items.Clear(); msg_list.Items.Add("The trigger mode is set as analog trigger in this demo."); msg_list.Items.Add("Please complete the following parameters then click the 'Set' button"); msg_list.Items.Add("Channel Count : 1 ~ 8"); msg_list.Items.Add("Range : +/- 5V , +/-10 V"); msg_list.Items.Add("Sample Rate : 1 Hz ~ 200K Hz"); msg_list.Items.Add("Target Count : 1 ~ 30M"); msg_list.Items.Add("AI Mode : Above, Below, Out-Range, In-Range"); msg_list.Items.Add("Enable : Enable or disable the specific channel for trigger"); msg_list.Items.Add("High Level : Set the high limit of the specific channel"); msg_list.Items.Add("Low Level : Set the low limit of the specific channel"); msg_list.Items.Add("Left Count : Set the data counts befoer triggered"); msg_list.Items.Add("Right Count : Set the data counts after triggered"); msg_list.Items.Add("P.S. 'Left Count' and 'Right Count' must add up to be equal to 'Target Count'"); } } private void Disconnect_btn_Click(object sender, EventArgs e) { int i = 0; HSDAQNet.HSIO.HS_StopAIScan(hHS); HSDAQNet.Sys.HS_Device_Release(hHS); connect_btn.Enabled = true; Disconnect_btn.Enabled = false; ChCnt_cmb.Enabled = false; Range_cmb.Enabled = false; Rate_txt.Enabled = false; Target_txt.Enabled = false; AIMode_cmb.Enabled = false; for (i = 0; i < 9; i++) { En_arr[i].Enabled = false; HL_arr[i].Enabled = false; LL_arr[i].Enabled = false; } LeftCnt_txt.Enabled = false; RightCnt_txt.Enabled = false; } private void EXIT_btn_Click(object sender, EventArgs e) { this.Close(); } private void ChCnt_cmb_SelectedIndexChanged(object sender, EventArgs e) { int i = 0; AIMode_cmb.Enabled = true; for (i = 0; i < ChCnt_cmb.SelectedIndex + 1; i++) { En_arr[i].Enabled = true; HL_arr[i].Enabled = true; LL_arr[i].Enabled = true; } for (i = ChCnt_cmb.SelectedIndex + 1; i < 8; i++) { En_arr[i].Enabled = false; HL_arr[i].Enabled = false; LL_arr[i].Enabled = false; } } private void EnAll_ckb_CheckedChanged(object sender, EventArgs e) { int i = 0; for (i = 0; i < 8; i++) { if (En_arr[i].Enabled) { En_arr[i].Checked = ((CheckBox)sender).Checked; Application.DoEvents(); } } } private void HLAll_txt_TextChanged(object sender, EventArgs e) { int i = 0; for (i = 0; i < 8; i++) { if (HL_arr[i].Enabled) { HL_arr[i].Text = ((TextBox)sender).Text; Application.DoEvents(); } } } private void LLAll_txt_TextChanged(object sender, EventArgs e) { int i = 0; for (i = 0; i < 8; i++) { if (LL_arr[i].Enabled) { LL_arr[i].Text = ((TextBox)sender).Text; Application.DoEvents(); } } } private void SET_btn_Click(object sender, EventArgs e) { int i = 0; short chcnt=0,gain=0,trig_mode=0,tramsmode=0,AutoRun=0; int rate=0; string[] gainstr=new string[2]{"+/- 5v","+/- 10V"}; string[] modestr = new string[7] { "Software Triggrt", "External Trigger", "Post Trigger", "Pre Trigger","Reserved", "Delay Trigger", "AI Trigger" }; string[] ENstr = new string[2] { "Disable", "Enable" }; UInt32 target=0; int analogmode=0; uint leftsidecnt=0; uint rightsidecnt=0; uint RESERVED=0; float[] hightriglevel=new float[8]; float[] lowtriglevel = new float[8]; bool[] En_Channel=new bool[8]; char[] Ch_En_arr = new char[8]; float[] HighLevel_arr = new float[8]; float[] LowLevel_arr = new float[8]; if (UInt32.Parse(Target_txt.Text) != UInt32.Parse(LeftCnt_txt.Text) + UInt32.Parse(RightCnt_txt.Text)) { MessageBox.Show("'Left Count' and 'Right Count' must add up to be equal to 'Target Count'"); return; } for (i = 0; i < 8; i++) { if (En_arr[i].Enabled) { if (En_arr[i].Checked) Ch_En_arr[i] = (char)1; else Ch_En_arr[i] = (char)0; } else Ch_En_arr[i] = (char)0; if (HL_arr[i].Enabled) { HighLevel_arr[i] = float.Parse(HL_arr[i].Text); } if (LL_arr[i].Enabled) { LowLevel_arr[i] = float.Parse(LL_arr[i].Text); } } if (HSDAQNet.HSIO.HS_SetAIScanParam(hHS, (short)(ChCnt_cmb.SelectedIndex + 1), (short)(Range_cmb.SelectedIndex), TriggerMode, int.Parse(Rate_txt.Text), uint.Parse(Target_txt.Text), 0, 0) != true) MessageBox.Show("Error code3 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); if (HSDAQNet.HSIO.HS_SetAIAnalogTriggerParam(hHS, AIMode_cmb.SelectedIndex, Ch_En_arr, HighLevel_arr, LowLevel_arr,8, uint.Parse(LeftCnt_txt.Text), uint.Parse(RightCnt_txt.Text), (uint)0) != true) MessageBox.Show("Error code3 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); if (HSDAQNet.HSIO.HS_GetAIScanParam(hHS, ref chcnt, ref gain, ref trig_mode, ref rate, ref target, ref tramsmode, ref AutoRun) != true) { MessageBox.Show("Error code3 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); } else { msg_list.Items.Clear(); msg_list.Items.Add("Set Analog Trigger success!"); msg_list.Items.Add("Trig Mode : " + modestr[trig_mode]); msg_list.Items.Add("Channels : " + chcnt); msg_list.Items.Add("Range : " + gainstr[gain]); msg_list.Items.Add("Sample Rate : " + rate); msg_list.Items.Add("Target Counts : " + target); } if (HSDAQNet.HSIO.HS_GetAIAnalogTriggerParam(hHS, ref analogmode, En_Channel, hightriglevel, lowtriglevel, 8, ref leftsidecnt, ref rightsidecnt, ref RESERVED) != true) { MessageBox.Show("Error code3 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); } else { msg_list.Items.Add("Analog Input Mode : " + analogmode ); msg_list.Items.Add("Total Channels : "+8); msg_list.Items.Add("Counts-befoer-tirg: " + leftsidecnt); msg_list.Items.Add("Counts-after-trig: " + rightsidecnt); for (i = 0; i < 8; i++) { if(En_Channel[i]) msg_list.Items.Add("Channel : " + i + " " + ENstr[1] + " High Level : " + hightriglevel[i] + " Low level : " + lowtriglevel[i]); else msg_list.Items.Add("Channel : " + i + " " + ENstr[0] + " High Level : " + hightriglevel[i] + " Low level : " + lowtriglevel[i]); } } msg_list.Items.Add("Please press 'Start' button."); } private void START_btn_Click(object sender, EventArgs e) { HSDAQNet.DATALOG.HS_StartLogger(hHS, Directory.GetCurrentDirectory(), 2, 0); datacnt = 0; RECV_flag = true; msg_list.Items.Clear(); msg_list.Items.Add("Sampling....."); RecvData(); } void RecvData() { bool ret = false; uint status = 0; while (RECV_flag) { ret = HSDAQNet.HSIO.HS_GetTotalSamplingStatus(hHS, ref datacnt, ref status); if (ret == false) { MessageBox.Show("Error code3 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); RECV_flag = false; break; } else { if ((status >> 7) == 1)// && datacnt >= UInt32.Parse(Target_txt.Text)) { RECV_flag = false; Test_Status_lab.Text = "Sampling Data Finished !! Total Counts : "+datacnt; break; } else if ((status >> 4) == 1) { MessageBox.Show("Unnormal stopped"); RECV_flag = false; break; } else if ((status >> 5) == 1) { MessageBox.Show("Data-Sampling timeout"); RECV_flag = false; break; } else if ((status >> 6) == 1) { Test_Status_lab.Text = "Received Count : " + datacnt + " Sampling Status : " + status.ToString("X"); } else { Test_Status_lab.Text="Sampling Count : " + datacnt + " Sampling Status : " + status.ToString("X"); } } Application.DoEvents(); } HSDAQNet.HSIO.HS_StopAIScan(hHS); msg_list.Items.Clear(); msg_list.Items.Add("Data safed in the ../Log/"+IP_txt.Text.Replace('.','_')); } private void STOP_btn_Click(object sender, EventArgs e) { HSDAQNet.DATALOG.HS_StopLogger(hHS); RECV_flag = false; } } }