using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; using System.Text.RegularExpressions; namespace AI_Scan_N_Sample_DelayTrigger { class Program { public static bool IsIP(string ip) //Check the IP format { return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } static void Main(string[] args) { string IP = ""; bool ret = false; IntPtr hHS; int ch = 0; UInt16 gVal = 0; Int16 oVal = 0; UInt16[] gVarr = new UInt16[8]; Int16[] oVarr = new Int16[8]; short ChCnt = 0; short Gain = 0; short TrigMode = 5; //5 : delay trigger int sampleRate = 0; UInt32 targetCnt = 0; UInt32 delaytime = 0; UInt32 Reserv = 0; short DataTransMethod = 0; short AutoRun = 0; float[] FdataBuffer; ushort BufferStatus = 0; uint readsize = 0; uint totalRecv = 0; uint i = 0; string msg = ""; Console.WriteLine("Please input IP (xxx.xxx.xxx.xxx) !!"); while (true) { IP = Console.ReadLine(); hHS = HSDAQNet.Sys.HS_Device_Create(IP); //ret = IsIP(IP); if (hHS == null) { Console.WriteLine("Invalid IP, please input again."); } else { Thread.Sleep(100); break; } } Console.WriteLine("ET-7H16 AI Scan Delay-trigger demo"); Console.WriteLine("HSDAQ SDK Version = " + HSDAQNet.Sys.GetSDKVersion().ToString()); Console.WriteLine("ET7H16 Firmware Versoin = " + HSDAQNet.Sys.GetFirmwareVersion(hHS).ToString()); for (ch = 0; ch < 8; ch++) { //Read Gain Offset valus for each channel HSDAQNet.Config.HS_ReadGainOffset(hHS, ch, ref gVal, ref oVal); Console.WriteLine("ch : " + ch + " Gain Value : " + gVal + " Offset Valus : " + oVal); gVarr[ch] = gVal; oVarr[ch] = oVal; } Console.WriteLine("Please input number of channels for sample data(1 ~ 8)"); ChCnt = short.Parse(Console.ReadLine()); Console.WriteLine("Please Select AI range"); Console.WriteLine("0 : +/- 5V"); Console.WriteLine("1 : +/- 10V"); Gain = short.Parse(Console.ReadLine()); Console.WriteLine("Please input sample rate(1 Hz ~ 200 KHz)"); sampleRate = int.Parse(Console.ReadLine()); Console.WriteLine("Please input Target Count(1 ~ 30000000)"); targetCnt = UInt32.Parse(Console.ReadLine()); Console.WriteLine("Please input Delay time(5us ~ 10000000us)"); delaytime = UInt32.Parse(Console.ReadLine()); ret = HSDAQNet.HSIO.HS_SetAIScanParam(hHS, ChCnt, Gain, TrigMode, sampleRate, targetCnt, DataTransMethod, AutoRun); if (!ret) { Console.WriteLine("HS_SetAIScanParam failed Error code1 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); return; } ret = HSDAQNet.HSIO.HS_SetAIDelayTriggerParam(hHS,delaytime,0); if (!ret) { Console.WriteLine("HS_SetAIDelayTriggerParam failed Error code1 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); return; } else { HSDAQNet.HSIO.HS_GetAIScanParam(hHS, ref ChCnt, ref Gain, ref TrigMode, ref sampleRate, ref targetCnt, ref DataTransMethod, ref AutoRun); HSDAQNet.HSIO.HS_GetAIDelayTriggerParam(hHS, ref delaytime,ref Reserv); Console.WriteLine("Set AI Scan : Ch = " + ChCnt + " Gain = " + Gain + " TrigMode = " + TrigMode + " sampleRate = " + sampleRate + " targetCnt = " + targetCnt+"Delay Time "+delaytime); FdataBuffer = new float[targetCnt]; ret = HSDAQNet.HSIO.HS_StartAIScan(hHS); if (ret == false) { Console.WriteLine("Error code2 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); } Console.WriteLine("The AI Scan is ready now, please input a 5v-pulse signal into the pins of Trig+ and Trig-"); while (true) { ret = HSDAQNet.HSIO.HS_GetAIBufferStatus(hHS, ref BufferStatus, ref totalRecv); if (ret == false) { Console.WriteLine("Error code3 0x" + HSDAQNet.ErrHandling.GetLastError().ToString("x8")); } else { if ((BufferStatus & 0x80) == 0x80 && totalRecv == targetCnt) { readsize = HSDAQNet.HSIO.HS_GetAIBuffer(hHS, FdataBuffer, targetCnt); break; } else if ((BufferStatus & 0x04) == 0x04) { Console.WriteLine("[error] AD_DATA_SAMPLING_TIMEOUT \n"); break; } else if ((BufferStatus & 0x02) == 0x02) { Console.WriteLine("[error] AD_BUF_OVERFLOW \n"); break; } else if ((BufferStatus & 0x01) == 0x01) { msg = string.Format("Received Count : " + totalRecv + "Status : " + BufferStatus + "\r"); Console.Write(msg); } else { msg = string.Format("Sampling Count : " + totalRecv + " Status : " + BufferStatus + "\r"); Console.Write(msg); } } Thread.Sleep(10); } HSDAQNet.HSIO.HS_StopAIScan(hHS); HSDAQNet.Sys.HS_Device_Release(hHS); Console.Write("\nIndex"); for (i = 0; i < ChCnt; i++) { Console.Write("|CH{0} ",i); } for (i = 0; i < targetCnt; i++) { if(i%ChCnt==0) Console.Write("\n{0,5}", i); Console.Write("|{0,9}", FdataBuffer[i]); if (i % ChCnt == ChCnt - 1) { Console.Write("\n"); } } Console.WriteLine("\r\nAP total read = " + totalRecv + "\r\n"); Console.Write("\n"); Console.WriteLine("Press any key to quit program"); Console.ReadLine(); } } } }