PicoScope 7 Software
Available on Windows, Mac and Linux
Code: Select all
using System;
using System.Timers;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Renishaw.Linear
{
///
/// HS10 contains the methods to communicate with the HS10 laser
///
public class HS10 : ILaser
{
#region Constants
///
/// defaultTimerLength is the ammount of time to wait before firing a timer event
///
private const int DefaultTimerLength = 1;
///
/// usbPort is the port to which the USB ADC is connected
///
private const int UsbPort = 101;
///
/// parallelPort is the port to which the parallel ADC is connected
///
private const int ParallelPort = 1;
///
/// product is the product code for the ADC40
///
private const int Product = 40;
///
/// Scales the 0 - 128 signal from the ADC too - 32
///
private const double Rescale = 0.25;
///
/// The voltage that indicates max signal strength
///
private const double MaxSignalStrength = 4.5;
///
/// The highest voltage that the ADC can read
///
private const double MaxVoltage = 5;
///
/// Subtracting this value from the Adc Value will set an adc reading of OV to 0 in the software
///
private const int ADCValueToZero = 128;
#endregion
#region Fields
///
/// Stores the port that the software connects to
///
private int port;
///
/// myTimer is used to take readings from the HS10 at regular intervals
///
Timer myTimer;
///
/// myAverage is used to return a smoother output from the raw laser data.
///
private Averager myAverage;
#endregion
#region Dll Imports
[DllImport("adc1032.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
static extern int adc10_open_unit(int port, int product);
[DllImport("adc1032.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
static extern int adc10_set_unit(int port);
[DllImport("adc1032.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
static extern int adc10_close_unit(int port);
[DllImport("adc1032.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
static extern short adc10_get_value();
#endregion
///
/// Create new timer event and set Averager buffer size to 4
///
public HS10()
{
myTimer = new Timer(DefaultTimerLength);
myTimer.Enabled = false;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myAverage = new Averager();
}
#region ILaser Members
///
/// Connect to the HS10
///
/// Connected = true, Connection error = false
public bool Connect()
{
bool retVal = false;
adc10_open_unit(ParallelPort,Product);
adc10_open_unit(UsbPort,Product);
if (adc10_set_unit(UsbPort) == 1)
{
port = UsbPort;
myTimer.Enabled = true;
retVal = true;
}
else
{
if (adc10_set_unit(ParallelPort) == 1)
{
port = ParallelPort;
myTimer.Enabled = true;
retVal = true;
}
else
{
retVal = false;
}
}
return retVal;
}
///
/// Disconnect from the HS10
///
public void Disconnect()
{
myTimer.Enabled = false;
adc10_close_unit(port);
}
///
/// LaserUpdated events are fired when new data is recieved from the Laser
///
public event LaserUpdatedHandler LaserUpdated;
///
/// Takes reading from the HS10 and fires a LaserUpdated event
///
private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (myTimer.Enabled)
{
int temp = 0;
try
{
temp = adc10_get_value();
}
catch
{
System.Diagnostics.Debug.WriteLine("Error with ADC 40");
}
temp = System.Convert.ToInt32((temp - ADCValueToZero) * Rescale/MaxSignalStrength*MaxVoltage);
LaserUpdated(this,new LaserUpdatedEventArgs(temp));
}
}
#endregion
}
}
Code: Select all
adc10_open_unit(UsbPort,Product);