using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
///I-8172W version 0.1.1.7, 2011/07/05
///I-8172W version 0.1.1.8, 2011/07/19
///Remove extra [DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_WriteDOGroup")]
namespace pac8172WNet
{
public class pac8172W
{
private class ICPDAS
{
// function for I-8172
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_GetLibVersion")]
public static extern Int16 pac_i8172W_GetLibVersion();
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_GetLibDate")]
public static extern void pac_i8172W_GetLibDate(byte[] libDate);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_Init")]
public static extern Int16 pac_i8172W_Init(int Slot);
// function for I-8172 to FR-2057
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_WriteDOGroup")]
public static extern void pac_i8172W_WriteDOGroup(int iSlot,int iPort, int iGroup,UInt16 iData);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_WriteDOBit")]
public static extern void pac_i8172W_WriteDOBit(int iSlot, int iPort, int iGroup, int iChannel, int iStatus);
// function for I-8172 to FR-2053
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_ReadDIGroup")]
public static extern UInt16 pac_i8172W_ReadDIGroup(int iSlot, int iPort, int iGroup);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_ReadDIBit")]
public static extern UInt16 pac_i8172W_ReadDIBit(int iSlot, int iPort, int iGroup,int iChannel);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_ReadInputGroupStatus")]
public static extern Int16 pac_i8172W_ReadInputGroupStatus(int iSlot, int iPort, int iGroup);
// function for I-8172 to FR-2054
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_DIO_WriteDOGroup")]
public static extern void pac_i8172W_DIO_WriteDOGroup(int iSlot, int iPort, int iGroup, UInt16 iData);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_DIO_WriteDOBit")]
public static extern void pac_i8172W_DIO_WriteDOBit(int iSlot, int iPort, int iGroup, int iChannel, int iStatus);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_DIO_ReadDIGroup")]
public static extern UInt16 pac_i8172W_DIO_ReadDIGroup(int iSlot, int iPort, int iGroup);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_DIO_ReadDIBit")]
public static extern UInt16 pac_i8172W_DIO_ReadDIBit(int iSlot, int iPort, int iGroup, int iChannel);
// function for I-8172 to FR-2017
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_ReadAIHex")]
public static extern Int16 pac_i8172W_ReadAIHex(int iSlot, int iPort, int iGroup, short bitMode, ref short iChannel,ref short aiHex);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_AIHexToFloat")]
public static extern void pac_i8172W_AIHexToFloat(Int16 hexData, Int16 typeCode, Int16 bitMode, ref float fData);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_ReadAI_Ch")]
public static extern Int16 pac_i8172W_ReadAI_Ch(int iSlot, int iPort, int iGroup, short bitMode, short typeCode, short iChannel,ref float aiFloat);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_ReadAI_All")]
public static extern Int16 pac_i8172W_ReadAI_All(int iSlot,int iPort, int iGroup, short bitMode,short isSingleEnded,short[] typeCode, float[] aiFloat);
// function for I-8172 to FR-2024
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_WriteAOHex")]
public static extern Int16 pac_i8172W_WriteAOHex(int iSlot, int iPort, int iGroup, short bitMode, short iChannel, short aoHexData, short ifDelay);
[DllImport("pac_i8172W.dll", EntryPoint = "pac_i8172W_WriteAOFloat")]
public static extern Int16 pac_i8172W_WriteAOFloat(int iSlot, int iPort, int iGroup, short bitMode, short iChannel, short typeCode, float aoFloat, short ifDelay);
}
///
/// Get the library version of this DLL (pac_i8172W.dll)
///
/// library version
public static Int16 LibVersion()
{
return ICPDAS.pac_i8172W_GetLibVersion();
}
///
/// Get the building date of this DLL (pac_i8172W.dll)
///
/// library building date
public static string LibDate()
{
//return Arr2Str(ICPDAS.pac_i8017H_GetLibDate());
byte[] libDate= new byte[32] ;
ICPDAS.pac_i8172W_GetLibDate(libDate);
return AscArr2Str(libDate);
}
///
/// Initialize the I-8172W in the specified slot
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0: OK; -1: Error.
public static Int16 Init(int slot)
{
return ICPDAS.pac_i8172W_Init(slot);
}
///
/// Write DO group value to the FRnet DO module, such as FR-2057.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 0 ~ 7 (the group of FRnet DOs, which is corresponding to a FRnet module)
/// 0 ~ 0xFFFF (the binary representation of 16 channel of DOs)
public static void WriteDOGroup(int iSlot,int iPort, int iGroup,UInt16 iData)
{
ICPDAS.pac_i8172W_WriteDOGroup(iSlot, iPort, iGroup,iData);
}
///
/// Write DO bit value to the FRnet DIO module, such as FR-2054.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 0 ~ 7 (the group of FRnet DOs, which is corresponding to a FRnet module)
/// 0 ~7 (the channel of FRnet DO module to write)
/// ON or OFF
public static void DIO_WriteDOBit(int iSlot, int iPort, int iGroup, int iChannel, int iStatus)
{
ICPDAS.pac_i8172W_DIO_WriteDOBit( iSlot, iPort, iGroup, iChannel, iStatus);
}
///
/// Write DO group value to the FRnet DO module, such as FR-2057.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 0 ~ 7 (the group of FRnet DOs, which is corresponding to a FRnet module)
/// 0 ~ 0xFFFF (the binary representation of 16 channel of DOs)
public static void DIO_WriteDOGroup(int iSlot, int iPort, int iGroup, UInt16 iData)
{
ICPDAS.pac_i8172W_DIO_WriteDOGroup(iSlot, iPort, iGroup, iData);
}
///
/// Write DO bit value to the FRnet DO module, such as FR-2057.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 0 ~ 7 (the group of FRnet DOs, which is corresponding to a FRnet module)
/// 0 ~15 (the channel of FRnet DO module to write)
/// ON or OFF
public static void WriteDOBit(int iSlot, int iPort, int iGroup, int iChannel, int iStatus)
{
ICPDAS.pac_i8172W_WriteDOBit(iSlot, iPort, iGroup, iChannel, iStatus);
}
///
/// Read DI group value from the FRnet DI module, such as FR-2053.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet DIs, which is corresponding to a FRnet module)
/// 0 ~ 0xFFFF (the binary representation of 16 channel of DIs)
public static UInt16 ReadDIGroup(int iSlot, int iPort, int iGroup)
{
return ICPDAS.pac_i8172W_ReadDIGroup(iSlot, iPort, iGroup);
}
///
/// Read DI bit value from the FRnet DI module, such as FR-2053.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet DIs, which is corresponding to a FRnet module)
/// 0 ~15 (the channel of FRnet DI module to read)
/// ON or OFF
public static UInt16 ReadDIBit(int iSlot, int iPort, int iGroup, int iChannel)
{
return ICPDAS.pac_i8172W_ReadDIBit(iSlot, iPort, iGroup, iChannel);
}
///
/// Read DI group value from the FRnet DIO module, such as FR-2054.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet DIs, which is corresponding to a FRnet module)
/// 0 ~ 0xFF (the binary representation of 8 channel of DIs)
public static Int16 ReadDIGroupstatus(int iSlot, int iPort, int iGroup)
{
return ICPDAS.pac_i8172W_ReadInputGroupStatus(iSlot, iPort, iGroup);
}
///
/// Get input communication status of input modules, such as FR-2053, FR-2054, FR-2017.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet inputs, which is corresponding to a FRnet module)
/// 0: off line; 1: on line
public static Int16 ReadInputGroupStatus(int iSlot, int iPort, int iGroup)
{
return ICPDAS.pac_i8172W_ReadInputGroupStatus(iSlot, iPort, iGroup);
}
public static UInt16 DIO_ReadDIGroup(int iSlot, int iPort, int iGroup)
{
return ICPDAS.pac_i8172W_DIO_ReadDIGroup(iSlot, iPort, iGroup);
}
///
/// Read DI bit value from the FRnet DIO module, such as FR-2054.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet DIs, which is corresponding to a FRnet module)
/// 0 ~7 (the channel of FRnet DI module to read)
/// ON or OFF
public static UInt16 DIO_ReadDIBit(int iSlot, int iPort, int iGroup, int iChannel)
{
return ICPDAS.pac_i8172W_DIO_ReadDIBit(iSlot, iPort, iGroup, iChannel);
}
///
/// Read AI value from the FRnet AI module, such as FR-2017.
/// Note: FR-2017 updates 16-bit data coutinuously every 2.8 ms in FRnet.
/// For 16-bit mode, channel always = 0 and aiHex has all its 16 bit data as analog input.
/// For 12-bit mode, channel is decided by the least significant 4 bits and analog input by most significant 12 bits.
/// For 12-bit mode, use function AIHexToFloat to convert hex format to floating-point value.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet inputs, which is corresponding to a FRnet module)
/// Bit_12: 12-bit mode; Bit_16: 16-bit mode
/// the channel which the AI value come from
/// analog input data of hex format
/// less than 0: error; else: success
public static Int16 ReadAIHex(int iSlot, int iPort, int iGroup, short bitMode, ref Int16 chIndex, ref Int16 aiHex)
{
return ICPDAS.pac_i8172W_ReadAIHex(iSlot, iPort, iGroup, bitMode, ref chIndex, ref aiHex);
}
///
/// Convert hex format analog input data to floating-point value, which comes from FR-2017.
///
/// hex format analog input data
/// FR-2017 input type and range
/// Bit_12: 12-bit mode; Bit_16: 16-bit mode
/// the converted result, floating-point value
public static void AIHexToFloat(Int16 hexData, Int16 typeCode, Int16 bitMode, ref float fData)
{
ICPDAS.pac_i8172W_AIHexToFloat(hexData, typeCode, bitMode,ref fData);
}
///
/// Read analog input value of the specified channel from FR-2017.
/// Compared with ReadAIHex, the application is blocked for about 0 ~ 45 ms to get data from the specified channel.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet inputs, which is corresponding to a FRnet module)
/// Bit_12: 12-bit mode; Bit_16: 16-bit mode
/// FR-2017 input type and range
/// the channel which the AI value come from
/// floating-point analog input data
/// the sampling time needed for getting the analog input of the specified channel (unit: ms)
public static Int16 ReadAI_Ch(int iSlot, int iPort, int iGroup, short bitMode, Int16 typeCode, Int16 chIndex, ref float aiFloat)
{
return ICPDAS.pac_i8172W_ReadAI_Ch(iSlot,iPort,iGroup,bitMode,typeCode,chIndex,ref aiFloat);
}
///
/// Read analog inputs from all of the channels from FR-2017.
/// Compared with ReadAIHex, the application is blocked for about 0 ~ 45 ms to get data from the all of the channels.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 8 ~ 15 (the group of FRnet inputs, which is corresponding to a FRnet module)
/// Bit_12: 12-bit mode; Bit_16: 16-bit mode
///
/// For 16-bit mode, maxChannel = 1.
/// For 12-bit mode, maxChannel = 8 if in differential mode.
/// For 12-bit mode, maxChannel = 16 if in single-ended mode.
///
/// the array of FR-2017 input type and range
/// the floating-point analog input array of all channels
/// the sampling time needed for getting the analog input of all the channels (unit: ms)
public static Int16 ReadAI_All(int iSlot, int iPort, int iGroup, short bitMode, short isSingleEnded,short[] typeCode, float[] aiFloat)
{
return ICPDAS.pac_i8172W_ReadAI_All(iSlot, iPort, iGroup, bitMode, isSingleEnded, typeCode, aiFloat);
}
///
/// Write AO value (hex format) to the FRnet AO module, such as FR-2024.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 0 ~ 7 (the group of FRnet inputs, which is corresponding to a FRnet module)
/// Bit_12: 12-bit mode (at present only support 12-bit mode)
/// the channel to which the AO value is wriiten
/// analog output data of hex format
/// false: no delay; true: delay 3 ms
/// always 0
public static Int16 WriteAOHex(int iSlot, int iPort, int iGroup, short bitMode, short iChannel, short aoHexData, short ifDelay)
{
return ICPDAS.pac_i8172W_WriteAOHex( iSlot, iPort, iGroup, bitMode, iChannel, aoHexData, ifDelay);
}
///
/// Write AO value (floating-point) to the FRnet AO module, such as FR-2024.
///
/// 1 ~ 7 (the slot which I-8172W is inserted)
/// 0 ~ 1 (the port of I-8172W which is used to connect FRnet)
/// 0 ~ 7 (the group of FRnet inputs, which is corresponding to a FRnet module)
/// Bit_12: 12-bit mode (at present only support 12-bit mode)
/// the channel to which the AO value is wriiten
/// FR-2024 output type and range
/// floatnig-point analog output data
/// false: no delay; true: delay 3 ms
/// always 0
public static Int16 WriteAOFloat(int iSlot, int iPort, int iGroup, short bitMode, short iChannel, short typeCode, float aoFloat, short ifDelay)
{
return ICPDAS.pac_i8172W_WriteAOFloat( iSlot, iPort, iGroup, bitMode, iChannel, typeCode, aoFloat, ifDelay);
}
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);
}
}
}