using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
///I-8088W version 1.0.0.6, 2011/07/05
///I-8088W version 1.0.0.7, 2011/07/19
///remove SetPWMDutyCount function
namespace pac8088WNet
{
public class pac8088W
{
private class ICPDAS
{
// for I/O version management
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetLibVersion")]
public static extern Int16 pac_i8088W_GetLibVersion();
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetLibDate")]
public static extern void pac_i8088W_GetLibDate(byte[] libDate);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetFirmwareVersion")]
public static extern Int16 pac_i8088W_GetFirmwareVersion(int Slot);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_Init")]
public static extern Int16 pac_i8088W_Init(int Slot);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetPWMDuty")]
public static extern Int16 pac_i8088W_SetPWMDuty(int slot,int ch,UInt32 hz,UInt16 duty);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetPWMDuty_Deci")]
public static extern Int16 pac_i8088W_SetPWMDuty_Deci(int slot, int ch, UInt32 deci_Hz, UInt16 deci_duty);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetPWMDuty_Float")]
public static extern Int16 pac_i8088W_SetPWMDuty_Float(int slot, int ch, float f_hz, float f_duty);
//[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetPWMDutyCount")]
//public static extern Int16 pac_i8088W_SetPWMDutyCount(int slot, int ch, UInt32 highDutyCount, UInt32 lowDutyCount);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetRealPWMDuty_Deci")]
public static extern Int16 pac_i8088W_GetRealPWMDuty_Deci(int slot, int ch, ref UInt32 deci_Hz, ref UInt16 deci_duty);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetPWMCountMode")]
public static extern Int16 pac_i8088W_SetPWMCountMode(int slot,int ch, byte countMode);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetBurstCount")]
public static extern Int16 pac_i8088W_SetBurstCount(int slot, int ch,UInt16 burstCount);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_PWM_Start")]
public static extern Int16 pac_i8088W_PWM_Start(int slot, int ch);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_PWM_Stop")]
public static extern Int16 pac_i8088W_PWM_Stop(int slot, int ch);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetSyncChannel")]
public static extern Int16 pac_i8088W_SetSyncChannel(int slot, int ch, Int16 enBit);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetSyncChannel")]
public static extern Int16 pac_i8088W_GetSyncChannel(int slot, int ch, ref Int16[] sycArr);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_Sync_Start")]
public static extern Int16 pac_i8088W_Sync_Start(int slot);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_Sync_Stop")]
public static extern Int16 pac_i8088W_Sync_Stop(int slot);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_SetHardwareTrigChannel")]
public static extern Int16 pac_i8088W_SetHardwareTrigChannel(int slot, int ch, Int32 triggerState);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetHardwareTrigChannel")]
public static extern Int16 pac_i8088W_GetHardwareTrigChannel(int slot, int ch, ref Int32 triggerState);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetDI")]
public static extern Int16 pac_i8088W_GetDI(int slot, ref UInt16 diVal, ref Int16[] diBitArr);
[DllImport("pac_i8088W.dll", EntryPoint = "pac_i8088W_GetPWMActiveState")]
public static extern Int16 pac_i8088W_GetPWMActiveState(int slot, ref Int32 pwmOutputState, Int32[] activeArray);
}
///
/// Get the library version of this DLL (pac_i8088W.dll)
///
/// library version
public static Int16 LibVersion()
{
return ICPDAS.pac_i8088W_GetLibVersion();
}
///
/// Get the firmware version of I-8088W in the specified slot.
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// firmware version
public static Int16 FirmwareVersion(int slot)
{
return ICPDAS.pac_i8088W_GetFirmwareVersion(slot);
}
///
/// Get the building date of this DLL (pac_i8088W.dll)
///
/// library building date
public static string LibDate()
{
//return Arr2Str(ICPDAS.pac_i8017H_GetLibDate());
byte[] libDate= new byte[32] ;
ICPDAS.pac_i8088W_GetLibDate(libDate);
return AscArr2Str(libDate);
}
///
/// Initialize the I-8088W in the specified slot
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 Init(int slot)
{
return ICPDAS.pac_i8088W_Init(slot);
}
private static string AscArr2Str(byte[] b)
{
return System.Text.UnicodeEncoding.Unicode.GetString(
System.Text.ASCIIEncoding.Convert(System.Text.Encoding.ASCII,
System.Text.Encoding.Unicode, b), 0, b.Length);
}
public static string Arr2Str(byte[] buffer)
{
return (new UnicodeEncoding()).GetString(buffer, 0, buffer.Length);
}
///
/// Set frequency and duty cycle in the specified channel
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// 10 ~ 500K Hz (non-continuous); frequency to set
/// percentage: 1~99 (the percentage of output high during a period)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetPWMDuty(int slot, int ch, UInt32 hz, UInt16 duty)
{
return ICPDAS.pac_i8088W_SetPWMDuty(slot, ch, hz, duty);
}
///
/// Set frequency and duty cycle in the specified channel, with 10 times precision
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// 100 ~ 5000K (non-continuous); frequency times 10 to set; for example 1000 == 100 Hz
/// percentage times 10: 1~999 (the percentage of output high during a period); for example 503 == 50.3%
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetPWMDuty_Deci(int slot, int ch, UInt32 deci_Hz, UInt16 deci_duty)
{
return ICPDAS.pac_i8088W_SetPWMDuty_Deci(slot, ch, deci_Hz, deci_duty);
}
///
/// Set frequency and duty cycle in the specified channel in floating-point format
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// 10 ~ 500K Hz (non-continuous); frequency to set (in floating-point format)
/// percentage: 0.1~99.9 in floating-point format (the percentage of output high during a period)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetPWMDuty_Float(int slot, int ch, float f_hz, float f_duty)
{
return ICPDAS.pac_i8088W_SetPWMDuty_Float(slot, ch, f_hz, f_duty);
}
///
/// Get the real frequency and real duty cycle in the specified channel.
/// Because the non-continuous property of frequency and duty cycle,
/// setting the frequency and the duty cycle may be rounded up to the nearest possible values.
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// 100 ~ 5000K (non-continuous); frequency times 10 to set; for example 1000 == 100 Hz
/// percentage times 10: 1~999 (the percentage of output high during a period); for example 503 == 50.3%
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 GetRealPWMDuty_Deci(int slot, int ch, ref UInt32 deci_Hz,ref UInt16 deci_duty)
{
return ICPDAS.pac_i8088W_GetRealPWMDuty_Deci(slot, ch,ref deci_Hz,ref deci_duty);
}
///
/// Set the count mode in the specified channel
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// BurstCount (send out certain number of periods and stop) or Continuos (non-stop outputs)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetPWMCountMode(int slot, int ch, byte countMode)
{
return ICPDAS.pac_i8088W_SetPWMCountMode(slot, ch, countMode);
}
///
/// Set counts for count mode = BurstCount
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// 0~65536; counts for count mode = BurstCount
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetBurstCount(int slot, int ch, UInt16 burstCount)
{
return ICPDAS.pac_i8088W_SetBurstCount(slot, ch, burstCount);
}
///
/// Start to output PWM wave in the specified channel
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 PWM_Start(int slot, int ch)
{
return ICPDAS.pac_i8088W_PWM_Start(slot, ch);
}
///
/// Stop to output PWM wave in the specified channel
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 PWM_Stop(int slot, int ch)
{
return ICPDAS.pac_i8088W_PWM_Stop(slot, ch);
}
///
/// Set the channel as a synchronous channel for SyncStart or SyncStop
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// whether the channel set as a synchronous channel or not
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetSyncChannel(int slot, int ch, Int16 enBit)
{
return ICPDAS.pac_i8088W_SetSyncChannel(slot, ch, enBit);
}
///
/// Get the information of synchronous channels
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
///
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 GetSyncChannel(int slot, int ch, ref Int16[] sycArr)
{
return ICPDAS.pac_i8088W_GetSyncChannel(slot, ch, ref sycArr);
}
///
/// Start to output PWM wave in all synchronized channels
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 Sync_Start(int slot)
{
return ICPDAS.pac_i8088W_Sync_Start(slot);
}
///
/// Stop to output PWM wave in all synchronized channels
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 Sync_Stop(int slot)
{
return ICPDAS.pac_i8088W_Sync_Stop(slot);
}
///
/// Set the trigger state of the specified channel.
/// Disabled: the channel is not triggered by hardware digital input.
/// Start: the channel is triggered to start outputting by hardware digital input.
/// Stop: the channel is triggered to stop outputting by hardware digital input.
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// state of trigger; Disabled, Start, Stop
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 SetHardwareTrigChannel(int slot, int ch, int triggerState)
{
return ICPDAS.pac_i8088W_SetHardwareTrigChannel(slot, ch, triggerState);
}
///
/// Get the trigger state of the specified channel.
/// Disabled: the channel is not triggered by hardware digital input.
/// Start: the channel is triggered to start outputting by hardware digital input.
/// Stop: the channel is triggered to stop outputting by hardware digital input.
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// 0 ~ 7 (the channel which outputs the PWM wave)
/// state of trigger; Disabled, Start, Stop
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 GetHardwareTrigChannel(int slot, int ch, ref int triggerState)
{
return ICPDAS.pac_i8088W_GetHardwareTrigChannel(slot, ch, ref triggerState);
}
///
/// Get digital inputs of I-8088W in the specified slot
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// the value of digital input which represents DI status of 8 channels
/// the array which represents DI status of 8 channels
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 GetDI(int slot, ref UInt16 diVal, ref Int16[] diBitArr)
{
return ICPDAS.pac_i8088W_GetDI(slot, ref diVal, ref diBitArr);
}
///
/// Get the active state of I-8088W in the specified slot
///
/// 1 ~ 7 (the slot which I-8088W is inserted)
/// the value of active states which represents active states of 8 channels
/// the array which represents active states of 8 channels (true = active; false = inactive)
/// the Error Codes which are defined in pac_i8088W.h
public static Int16 GetPWMActiveState(int slot, ref Int32 pwmOutputState, Int32[] activeArray)
{
return ICPDAS.pac_i8088W_GetPWMActiveState(slot, ref pwmOutputState, activeArray);
}
}
}