using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using Sys; namespace Extensometer.BLL { public class DAQAOClass { #region 实例、变量 static IntPtr taskHandle = (IntPtr)(0); static Int32 error = 0; /// /// 是否初始化标志量 /// public static bool B_IsInit = false; #endregion #region 方法 [DllImport("msvcrt.dll")] public static extern int _getch(); [DllImport("msvcrt.dll")] public static extern int _kbhit(); public static void GetErrorString(Int32 errorCode) { if (errorCode < 0) { byte[] errorInfo = new byte[2048]; ArtDAQ.ArtDAQ_GetExtendedErrorInfo(errorInfo, 2048); string str = Encoding.Default.GetString(errorInfo); Console.WriteLine("错误信息为:{0}", str); } return; } public static void Init() { string errorInfo = new string('0', 2048); string strTaskName = string.Empty; string strCardName = string.Empty; string strChannelName = string.Empty; /*********************************************/ // ArtDAQ Configure Code /*********************************************/ error = ArtDAQ.ArtDAQ_CreateTask(Encoding.Default.GetBytes("task1"), out taskHandle); strChannelName = "Dev1/ao1"; //step2: CreateChannel error = ArtDAQ.ArtDAQ_CreateAOVoltageChan(taskHandle, Encoding.Default.GetBytes(strChannelName), Encoding.Default.GetBytes(""), -10.0, 10.0, ArtDAQ.ArtDAQ_Val_Volts, null); GetErrorString(error); if (error < 0) { return; } //step3: Start Task error = ArtDAQ.ArtDAQ_StartTask(taskHandle); GetErrorString(error); if (error < 0) { return; } B_IsInit = true; _getch(); return; } public static void WriteAnalog(double Value) { Double[] data = new double[2]; data[0] = Value; Int32 nSamplePerChanWrite; //step4:Write Code error = ArtDAQ.ArtDAQ_WriteAnalogF64(taskHandle, 1, 0, 10.0, ArtDAQ.ArtDAQ_Val_GroupByChannel, data, out nSamplePerChanWrite, (IntPtr)0); GetErrorString(error); if (error < 0) { return; } return; } public static void Dispose() { B_IsInit = false; Exit(); } private static void Exit() { ArtDAQ.ArtDAQ_StopTask(taskHandle); ArtDAQ.ArtDAQ_ClearTask(taskHandle); _getch(); return; } #endregion } }