下面的代碼清單使用 DaqInScan 函數來讀取熱電偶輸入。(本文底部附模塊)該功能通常用于同時讀取USB-1616HS和USB-2523等設備上的設備子系統(模擬、數字). 但是,如果您需要以更快的速度掃描多個熱電偶,則還必須使用它來將通道與其 CJC 傳感器配對。在此示例中,我們使用它以每秒 100 個樣本讀取 8 個熱電偶輸入(以及相關的 CJC 通道)。緩沖區中返回的二進制數據包含 CJC 和通道數據。它被安排為 CJC、Channel0、CJC Channel1 等。必須注意在每個通道之前放置正確的 CJC。GetTCValues 函數用于將 CJC 和通道數據轉換為溫度值。使用該功能的關鍵是輸入第一個熱電偶/CJC 的位置和要轉換的樣本數。
下面附上 32 位代碼文件。要使用它,您必須創建一個 32 位 C# 控制臺項目,并且必須添加對 MccDaq 對象的引用。添加引用通常通過右鍵單擊項目 [在項目資源管理器下并選擇添加引用/擴展來完成。
免責聲明:
隨附的代碼或示例按原樣提供。它尚未作為產品進行測試或驗證,用于已部署的應用程序或系統,或用于危險環境。您承擔使用代碼或示例的所有風險。
使用系統;
使用麥克達克;// 另外,將 MccDaq 的引用添加到項目中
//本程序使用DaqInScan函數讀取8個熱電偶通道;返回數據
//以來自 CJC 和熱電偶的原始計數的形式;為了得到
//溫度數據,因為使用了名為 GetTCValues 的第二個函數
命名空間 CSharp_USB_1616HS_DaqInScan
{
靜態類常量
{
公共常量 int ChanCount = 16; //通道數(8x TC 8x CJC)
公共常量 int BUFFSIZE = 32 * ChanCount;
公共靜態 int bufferSize = BUFFSIZE;
公共常量 int HALFBUFF = BUFFSIZE / 2;
公共靜態 MccBoard 數據集;
公共靜態 int 速率 = 100;
公共靜態int preTrigCount = 0;
公共常量 int TC_Count = 2;
}
課堂節目
{
靜態無效主要(字符串 [] 參數)
{
int BoardNum = 0;
整數計數 = 0;
整數索引 = 0;
短 daqStatus = 0;
ushort[] ADData = new ushort[Constants.BUFFSIZE]; //原始數據
float[] tempVals = new float[Constants.BUFFSIZE]; //用于溫度
短 [] ChanArray = 新短 [Constants.ChanCount]; // 保存通道隊列信息的數組
ChannelType[] ChanTypeArray = new ChannelType[Constants.ChanCount]; // 保存通道類型信息的數組
Range[] GainArray = new Range[Constants.ChanCount]; // 保存增益隊列信息的數組
Console.WriteLine("正在定位設備...請稍候\n");
BoardNum = GetBoardNum("1616");
如果 (BoardNum == -1)
{
Console.WriteLine("未檢測到設備!");
等待鍵();
返回;
}
別的
{
常量.daq = new MccDaq.MccBoard(BoardNum);
常量.daq.AInputMode(AInputMode.Differential);
//通道配置數組定義
詮釋 i = 0;
//如果混合電壓一個 TC,將 TC 通道放在列表中的第一位,以便使用 GetTCValues 進行轉換。
//TC0
ChanArray[i] = 0;
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 0;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
//TC1
我++;
ChanArray[i] = 1;
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 1;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
我++;
//TC2
ChanArray[i] = 1; //TC2使用CJC1
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 2;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
我++;
//TC3
ChanArray[i] = 2; //TC3使用CJC2
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 3;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
我++;
//TC4
ChanArray[i] = 3; //TC4使用CJC3
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 4;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
我++;
//TC5
ChanArray[i] = 4; //TC5 使用 CJC 4
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 5;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
我++;
//TC6
ChanArray[i] = 4; //TC6也使用CJC4
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 6;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
我++;
//TC7
ChanArray[i] = 5; //TC7使用CJC5
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
我++;
ChanArray[i] = 7;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
ScanOptions 選項 = ScanOptions.ConvertData
| ScanOptions.Continuous
| 掃描選項。背景;
//分配緩沖區
int bufferSize = 常量.bufferSize;
IntPtr 緩沖區 = MccService.WinBufAllocEx(Constants.bufferSize);
Console.WriteLine("實際匯率 {0}\n", Constants.rate);
等待鍵();
IsError(Constants.daq.DaqInScan(ChanArray,
ChanTypeArray,
增益陣列,
常量.ChanCount,
參考常數.rate,
參考常量.preTrigCount,
參考常量.bufferSize,
緩沖,
選項));
System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();
bool readLow = true;
做
{
System.Threading.Thread.Sleep(1);
IsError(Constants.daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.DaqiFunction));
if ((Index > Constants.HALFBUFF) && readLow)
{
MccService.WinBufToArray(buffer, ADData, 0, Constants.HALFBUFF);
//緩沖區包含 16 個通道的 32 次掃描。因為 TC 在列表中排在第一位,所以 TC 轉換從 0 開始,用于較低的部分。
//請記住,GetTCValues 使用掃描,而 WinBufToArray 使用 Scans * Channel Count
IsError(Constants.daq.GetTCValues(ChanArray, ChanTypeArray, Constants.ChanCount, buffer, 0, 16, TempScale.Celsius, tempVals));
讀低 = 假;
我 = 0;
for (int idx = 0; idx < Constants.HALFBUFF; idx+=16)
{
Console.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"));
idx += 8;
}
}
else if ((Index < Constants.HALFBUFF) && !readLow)
{
MccService.WinBufToArray(buffer, ADData, Constants.HALFBUFF, Constants.HALFBUFF);
//the buffer contains 32 scans of the 16 channels. Because the TC are first in the list the TC conversion starts at 16 for the upper portion.
//Keep in mind that GetTCValues uses scans whereas WinBufToArray uses Scans * Channel Count
IsError(Constants.daq.GetTCValues(ChanArray, ChanTypeArray, Constants.ChanCount, buffer, 16, 16, TempScale.Celsius, tempVals));
readLow = true;
i = 0;
for (int idx = 0; idx < Constants.HALFBUFF; idx+=16)
{
Console.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"));
}
}
} while (!Console.KeyAvailable);
cki = Console.ReadKey();
//stop background operation
IsError(Constants.daq.StopBackground(FunctionType.DaqiFunction));
Constants.daq.TimerOutStop(0);
//free up memory
IsError(MccService.WinBufFreeEx(buffer));
WaitForKey();
}
// end of program
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int GetBoardNum(string dev)
{
MccDaq.DaqDeviceManager.IgnoreInstaCal();
MccDaq.DaqDeviceDescriptor[] inventory = MccDaq.DaqDeviceManager.GetDaqDeviceInventory(MccDaq.DaqDeviceInterface.Any);
int DevicesFound = inventory.Length;
if (DevicesFound > 0)
{
for (int boardNum = 0; boardNum < DevicesFound; boardNum++)
{
try
{
if (inventory[boardNum].ProductName.Contains(dev))
{
MccDaq.MccBoard daqBoard = MccDaq.DaqDeviceManager.CreateDaqDevice(boardNum, inventory[boardNum]);
Console.WriteLine("Product Name: {0}", inventory[boardNum].ProductName);
Console.WriteLine("Device Type # : {0}", inventory[boardNum].ProductID);
Console.WriteLine("Serial # : {0}", inventory[boardNum].UniqueID);
return boardNum;
}
}
catch (ULException ule)
{
Console.WriteLine("Error occured: " + ule.Message);
}
}
}
return -1;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static void WaitForKey()
{
Console.WriteLine("\nPress any key to continue...\n");
do
{ //idle loop
System.Threading.Thread.Sleep(10);
} while (!Console.KeyAvailable);
Console.ReadKey();//get rid of the key press
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int IsError(ErrorInfo e)
{
if (e.Value != 0)
{
Console.WriteLine(e.Message);
WaitForKey();
return 1;
}
return 0;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
//function to convert to engineering units; if just volts set scale to 1.0 and offset to 0.0
public static double ScaleData(Range rng, ushort dataval, double scale, double offset)
{
浮動 V2 = 0.0f;
Constants.daq.ToEngUnits(rng, dataval, out V2);
返回 V2 * 比例 + 偏移量;
}
}
}