using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MccDaq;
using System.IO;
namespace GC004
{
public partial class Form1 : Form
{
public const int NUMOFBLKS = 8;
public const int PACKETSIZE = 8;
public const int CHANCOUNT = 4;
public const int FIRSTCHANNEL = 0;
public const int LASTCHANNEL = 3;
public const int FREQ = 100;
public const string DEVICE = "USB-1208LS";
public static MccBoard daq;
public int BoardNum = 0;
public int Rate = FREQ;
public bool ReadLower = true;
public double tmpch0 = 0.0;
public double tmpch1 = 0.0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Start";
label2.Text = "LoadCell X:";
label3.Text = "LoadCell Y:";
label5.Text = "BodyGC X:";
label6.Text = "BodyGC Y:";
label7.Text = "AD board no.";
label8.Text = "Here is your GravityCenter!";
this.Text = Application.ProductName;
timer1.Interval = 2000;
timer1.Enabled = false;
}
private void MainP()
{
Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics grp = Graphics.FromImage(canvas);
Pen pblack = new Pen(Color.Black, 1);
Pen pwhite = new Pen(Color.White, 1);
BoardNum = GetBoardNum(DEVICE);
int BUFFERSIZE = PACKETSIZE * CHANCOUNT * NUMOFBLKS;
int HALFBUFFSIZE = BUFFERSIZE / 2;
daq = new MccDaq.MccBoard(BoardNum);
IntPtr buffer = MccService.WinBufAllocEx(BUFFERSIZE);
short[] chArray = new short[CHANCOUNT]; //configuration array for channel numbers
Range[] chRange = new Range[CHANCOUNT]; //configuration array for input ranges
chArray[0] = 0;
chArray[1] = 1;
chArray[2] = 2;
chArray[3] = 3;
chRange[0] = Range.Bip10Volts;
chRange[1] = Range.Bip10Volts;
chRange[2] = Range.Bip10Volts;
chRange[3] = Range.Bip10Volts;
IsError(daq.ALoadQueue(chArray, chRange, CHANCOUNT));
ScanOptions options = ScanOptions.Background | ScanOptions.Continuous| ScanOptions.NoCalibrateData;
//setup the acquisiton
IsError(daq.AInScan(FIRSTCHANNEL,
LASTCHANNEL,
BUFFERSIZE,
ref Rate,
Range.Bip10Volts,
buffer,
options)
);
int Count = 0;
int Index = 0;
short daqStatus;
ushort[] ushortArray = new ushort[BUFFERSIZE];
double height = 0.9;
double width = 0.50;
double bodyweight = 50.0;
double indexA = 0.0;
double indexB = 0.0;
double finalX = 0.0;
double finalY = 0.0;
int posX;
int posY;
for (int k = 0; k < 10000; k++)
{
IsError(daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.AiFunction));
if ((Index >= HALFBUFFSIZE) & ReadLower) //check for 50% more data
{
IsError(MccService.WinBufToArray(buffer, ushortArray, 0, HALFBUFFSIZE));
tmpch0 = DisplayData(ushortArray, HALFBUFFSIZE / CHANCOUNT, 0);
ReadLower = false; //flag that controls the next read
}
else if ((Index < HALFBUFFSIZE) & !ReadLower)
{
IsError(MccService.WinBufToArray(buffer, ushortArray, HALFBUFFSIZE, HALFBUFFSIZE));
tmpch0 = DisplayData(ushortArray, HALFBUFFSIZE / CHANCOUNT, 0);
ReadLower = true;//flag that controls the next read
}
button3.Text = tmpch0.ToString("00.00");
button3.Update();
System.Threading.Thread.Sleep(10);
IsError(daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.AiFunction));
if ((Index >= HALFBUFFSIZE) & ReadLower) //check for 50% more data
{
//get lower half of buffer
IsError(MccService.WinBufToArray(buffer, ushortArray, 0, HALFBUFFSIZE));
tmpch1 = DisplayData(ushortArray, HALFBUFFSIZE / CHANCOUNT, 1);
ReadLower = false; //flag that controls the next read
}
else if ((Index < HALFBUFFSIZE) & !ReadLower)
{
//get the upper half
IsError(MccService.WinBufToArray(buffer, ushortArray, HALFBUFFSIZE, HALFBUFFSIZE));
tmpch1 = DisplayData(ushortArray, HALFBUFFSIZE / CHANCOUNT, 1);
ReadLower = true;//flag that controls the next read
}
button2.Text = tmpch1.ToString("00.00");
button2.Update();
button1.Text = k.ToString("00.00");
button1.Update();
if (tmpch0 == 0) { tmpch0 = 0.01; }
indexA = (tmpch1 * width / tmpch0 + height) / bodyweight;
indexB = width / tmpch0 - indexA;
finalX = tmpch0 * indexA;
finalY = (bodyweight - tmpch1) * indexA;
if (finalX > 2.0) { finalX = 2.0; };
if (finalX < -2.0) { finalX = -2.0; };
if (finalY > 2.0) { finalY = 2.0; };
if (finalY < -1.0) { finalY = -1.0; };
button5.Text = finalX.ToString("00.00");
button6.Text = finalY.ToString("00.00");
button5.Update();
button6.Update();
label4.Text = BoardNum.ToString("0");
label4.Update();
grp.DrawRectangle(pblack, 200 - (int)(finalX * 800.0), 200 + (int)(finalY * 100.0), 50, 50);
grp.DrawRectangle(pwhite, 200 - (int)(finalX * 800.0)-1, 200 + (int)(finalY * 100.0)-1, 52, 52);
grp.DrawRectangle(pwhite, 200 - (int)(finalX * 800.0)+1, 200 + (int)(finalY * 100.0)+1, 48, 48);
grp.DrawRectangle(pblack, 200 - (int)(finalX * 800.0), 200 + (int)(finalY * 100.0), 50, 50);
grp.DrawRectangle(pwhite, 200 - (int)(finalX * 800.0) - 2, 200 + (int)(finalY * 100.0) - 2, 54, 54);
grp.DrawRectangle(pwhite, 200 - (int)(finalX * 800.0) + 2, 200 + (int)(finalY * 100.0) + 2, 46, 46);
System.Threading.Thread.Sleep(10);
pictureBox1.Image = canvas;
pictureBox1.Update();
}
pblack.Dispose();
grp.Dispose();
IsError(daq.StopBackground(FunctionType.AiFunction));
MccService.WinBufFreeEx(buffer);
}
public static int IsError(ErrorInfo e)
{
if (e.Value != 0)
{
return 1;
}
return 0;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int GetBoardNum(string dev)
{
for (int BoardNum = 0; BoardNum < 99; BoardNum++)
{
MccDaq.MccBoard daq = new MccDaq.MccBoard(BoardNum);
if (daq.BoardName.Contains(dev))
{
return BoardNum;
}
}
return -1;
}
public static double DisplayData(ushort[] datArray, int rows, int readch)
{
int i = 0;
float temp = 0;
double temp2 = 0.0;
double ch1 = 0.0;
double ch0 = 0.0;
for (int row = 0; row < rows; row++)
{
IsError(daq.ToEngUnits(Range.Bip10Volts, datArray[i], out temp));
temp2 = ((double)temp - 1.44) * 10.0;
i++;
ch0 = temp2;
IsError(daq.ToEngUnits(Range.Bip10Volts, datArray[i], out temp));
temp2 = ((double)temp - 1.79) * 10.0;
//Console.Write("{0}\t", temp2.ToString("00.00"));
i++;
ch1 = temp2;
IsError(daq.ToEngUnits(Range.Bip10Volts, datArray[i], out temp));
i++;
IsError(daq.ToEngUnits(Range.Bip10Volts, datArray[i], out temp));
i++;
}
if (readch == 0)
{
return ch0;
}
if (readch == 1)
{
return ch1;
}
return 5.0;
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime d = DateTime.Now;
label1.Text = d.Hour + ":" + d.Minute + ":" + d.Second;
}
private void button1_Click(object sender, EventArgs e)
{
MainP();
}
private void button4_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}