using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Runtime.InteropServices; namespace XPacNET { public class XPac { //module type const public const int PAC_PARALLEL = 0x80; public const int PAC_SCAN = 0x40; public const int PAC_DI32 = 0xE3; public const int PAC_32BIT = 0x20; public const int PAC_DO32 = 0xE0; public const int PAC_DI16DO16 = 0xE2; public const int PAC_DI16 = 0xC3; public const int PAC_DO16 = 0xC0; public const int PAC_DI8DO8 = 0xC2; public const int PAC_8K = 8000; //memory const public const int PAC_MEM_SRAM = 0; public const int PAC_MEM_EEPROM = 1; //watchdog const public const int PAC_WDT_HW = 0; public const int PAC_WDT_OS = 1; //uart const public const int CTO_TIMEOUT_ALL = 0; public const int CTO_READ_RETRY_TIMEOUT = 1; public const int CTO_READ_TOTAL_TIMEOUT = 2; public const int CTO_WRITE_TOTAL_TIMEOUT = 3; //interrupt const public const int PAC_INTR_DONE = 0; public const int PAC_INTR_EXIT = 1; public const int PAC_INTR_FAIL = 2; public const int INTR_RISING_TRIG = 0; public const int INTR_LEVEL_TRIG = 1; public const int INTR_Falling_TRIG = 2; //cpu type public const int LX800 = 0x1; public const int ATOM = 0x2; private class ICPDAS { [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetSerialNumber")] public static extern void _pac_GetSerialNumber(byte[] SerialNumber); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetBackplaneID")] public extern static void _pac_GetBackplaneID(byte[] BKID); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetModuleName")] public static extern int _pac_GetModuleName(byte slot, byte[] strName); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadMemory")] unsafe public extern static bool pac_ReadMemory(uint address, byte* lpBuffer, uint dwLength, int mem_type); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_WriteMemory")] unsafe public extern static bool pac_WriteMemory(uint address, byte* lpBuffer, uint dwLength, int mem_type); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_Open")] public extern static IntPtr _uart_Open(byte[] ConnectionString); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetSDKVersion")] public extern static void _pac_GetSDKVersion(byte[] SDKVersion); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetCPUVersion")] public extern static void _pac_GetCPUVersion(byte[] cpu_version); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetOSVersion")] public extern static void _pac_GetOSVersion(byte[] os_version); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetMacAddress")] public extern static void _pac_GetMacAddress(byte lan, byte[] mac_addr); } //System API //================================================================== [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetModuleType")] public static extern int pac_GetModuleType(byte slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "_pac_get_CPUtype")] public static extern int pac_get_CPUtype(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetRotaryID")] public static extern int pac_GetRotaryID(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_EnableLED")] public static extern void pac_EnableLED(int pin, bool bFlag); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ChangeSlot")] public static extern void pac_ChangeSlot(byte slotNo); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_CheckSDKVersion")] public static extern bool pac_CheckSDKVersion(uint version); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_EnableLED")] public static extern void pac_EnableLED(byte pin,bool bFlag); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_Reboot")] public static extern void pac_Reboot(); public static string pac_GetSerialNumber() { byte[] result = new byte[32]; ICPDAS._pac_GetSerialNumber(result); string SN = XPac.WideString(result); return string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}", SN.Substring(0, 2), SN.Substring(2, 2), SN.Substring(4, 2), SN.Substring(6, 2), SN.Substring(8, 2), SN.Substring(10, 2), SN.Substring(12, 2), SN.Substring(14, 2)); } public static int pac_GetModuleName(byte slot, ref string strName) { byte[] result = new byte[128]; int type; type = ICPDAS._pac_GetModuleName(slot, result); strName = XPac.WideString(result); return type; } public static string pac_GetCPUVersion() { byte[] result = new byte[128]; ICPDAS._pac_GetCPUVersion(result); string CPUVersion = XPac.WideString(result); return CPUVersion; } public static string pac_GetOSVersion() { byte[] result = new byte[128]; ICPDAS._pac_GetOSVersion(result); string OSVersion = XPac.WideString(result); return OSVersion; } public static string pac_GetMacAddress(byte lan) { byte[] result = new byte[128]; ICPDAS._pac_GetMacAddress(lan, result); string MACAddr = XPac.WideString(result); return MACAddr; } public static string pac_GetXPacSDKVersion() { byte[] result = new byte[32]; ICPDAS._pac_GetSDKVersion(result); string SDKVersion = XPac.WideString(result); return SDKVersion; } public static string pac_GetXPacNETVersion() { return "1.1.1.1"; } //================================================================== //Backplane API //================================================================== [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetSlotCount")] public static extern int pac_GetSlotCount(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetDIPSwitch")] public static extern int pac_GetDIPSwitch(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetBatteryLevel")] public static extern int pac_GetBatteryLevel(int nBattery); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ModuleExists")] public static extern bool pac_ModuleExists(IntPtr hPort, byte slot); public static string pac_GetBackplaneID() { byte[] result = new byte[32]; ICPDAS._pac_GetBackplaneID(result); string str = XPac.WideString(result); return str; } //================================================================== //Memory Access API //================================================================== [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetMemorySize")] public static extern uint pac_GetMemorySize(int mem_type); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_EnableEEPROM")] public static extern void pac_EnableEEPROM(bool bEnable); unsafe public static bool pac_ReadMemory(uint index, byte[] Buffer, uint Length, int mem_type) { fixed (byte* pBuffer = Buffer) return ICPDAS.pac_ReadMemory(index, pBuffer, Length, mem_type); } unsafe public static bool pac_WriteMemory(uint index, byte[] Buffer, uint Length, int mem_type) { fixed (byte* pBuffer = Buffer) return ICPDAS.pac_WriteMemory(index, pBuffer, Length, mem_type); } public static byte pac_ReadByte(uint index, int mem_type) { byte[] b = new byte[1]; if (pac_ReadMemory(index, b, 1, mem_type) == true) return b[0]; return 0; } public static bool pac_WriteByte(uint index, byte b, int mem_type) { byte[] buf = new byte[1]; buf[0] = b; return pac_WriteMemory(index, buf, 1, mem_type); } //Watchdog API //================================================================== [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_EnableWatchDog")] public static extern bool pac_EnableWatchDog(int wdt, uint value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_DisableWatchDog")] public static extern void pac_DisableWatchDog(int wdt); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_RefreshWatchDog")] public static extern void pac_RefreshWatchDog(int wdt); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetWatchDogState")] public static extern bool pac_GetWatchDogState(int wdt); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetWatchDogTime")] public static extern uint pac_GetWatchDogTime(int wdt); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_SetWatchDogTime")] public static extern bool pac_SetWatchDogTime(int wdt, uint value); //================================================================== //UART API //================================================================== public static IntPtr uart_Open(string ConnectionString) { return ICPDAS._uart_Open(XPac.AnsiString(ConnectionString)); } [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_Close")] public static extern bool uart_Close(IntPtr hPort); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_Send")] public static extern bool uart_Send(IntPtr hPort, byte[] buf); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_Recv")] public static extern bool uart_Recv(IntPtr hPort, [In, Out] byte[] buf); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_SendCmd")] public extern static bool uart_SendCmd(IntPtr hPort, byte[] cmd, [In, Out] byte[] szResult); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_SetTimeOut")] public extern static void uart_SetTimeOut(IntPtr hPort, uint msec, int ctoType); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_EnableCheckSum")] public extern static void uart_EnableCheckSum(IntPtr hPort, bool bEnable); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_SetTerminator")] public extern static void uart_SetTerminator(IntPtr hPort, byte[] szTerm); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_BinSend")] public extern static bool uart_BinSend(IntPtr hPort, byte[] buf, uint in_Len); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_BinSendCmd")] public extern static bool uart_BinSendCmd(IntPtr hPort, byte[] ByteCmd, uint in_Len, byte[] ByteResult, uint out_Len); [DllImport("XPacSDK_CE.dll", EntryPoint = "uart_BinRecv")] public extern static bool uart_BinRecv(IntPtr hPort, byte[] buf, uint in_Len); //================================================================== //Error Handling API //================================================================== [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetLastError")] public extern static uint pac_GetLastError(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_SetLastError")] public extern static void pac_SetLastError(uint errno); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ClearLastError")] public extern static void pac_ClearLastError(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetErrorMessage")] private extern static void _pac_GetErrorMessage(uint dwMessageID,[In, Out] byte[] lpBuffer); public static string pac_GetErrorMessage(uint dwMessageID) { byte[] buffer = new byte[1024]; Regex r = new Regex("!"); _pac_GetErrorMessage(dwMessageID, buffer); string str = WideString(buffer); return r.Split(str)[0]; } //================================================================== //PAC Local/Remote IO API //================================================================== public static int PAC_REMOTE_IO(int iAddress) { return (iAddress + 0x1000); } public static bool pac_GetBit(int value, int index) { int bit = (value & (1 << index)); if (bit == 0) return false; else return true; } [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_WriteDO")] public extern static bool pac_WriteDO(IntPtr hPort, int slot, int iDO_TotalCh, uint lDO_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_WriteDOBit")] public extern static bool pac_WriteDOBit(IntPtr hPort, int slot, int iDO_TotalCh, int iChannel, int iBitValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadDO")] public extern static bool pac_ReadDO(IntPtr hPort, int slot, int iDO_TotalCh, ref uint IDO_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadDI")] public extern static bool pac_ReadDI(IntPtr hPort, int slot, int iDI_TotalCh, ref uint lDI_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadDIO")] public extern static bool pac_ReadDIO(IntPtr hPort, int slot, int iDI_TotalCh, int iDO_TotalCh, ref uint lDI_Value, ref uint lDO_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadDILatch")] public extern static bool pac_ReadDILatch(IntPtr hPort, int slot, int iDI_TotalCh, int iLatchType, ref uint lDI_Latch_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ClearDILatch")] public extern static bool pac_ClearDILatch(IntPtr hPort, int slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadDIOLatch")] public extern static bool pac_ReadDIOLatch(IntPtr hPort, int slot, int iDI_TotalCh, int iDO_TotalCh, int iLatchType, ref uint lDI_Latch_Value, ref uint lDO_Latch_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ClearDIOLatch")] public extern static bool pac_ClearDIOLatch(IntPtr hPort, int slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadDICNT")] public extern static bool pac_ReadDICNT(IntPtr hPort, int slot, int iChannel, int iDI_TotalCh, ref uint lCounter_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ClearDICNT")] public extern static bool pac_ClearDICNT(IntPtr hPort, int slot, int iChannel, int iDI_TotalCh); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_WriteAO")] public extern static bool pac_WriteAO(IntPtr hPort, int slot, int iChannel, int iAO_TotalCh, float fValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadAO")] public extern static bool pac_ReadAO(IntPtr hPort, int slot, int iChannel, int iAO_TotalCh, ref float fValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadAI")] public extern static bool pac_ReadAI(IntPtr hPort, int slot, int iChannel, int iAI_TotalCh, ref float fValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadAIHex")] public extern static bool pac_ReadAIHex(IntPtr hPort, int slot, int iChannel, int iAI_TotalCh, ref int iValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadAIAll")] public extern static bool pac_ReadAIAll(IntPtr hPort, int slot, float[] fValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadAIAllHex")] public extern static bool pac_ReadAIAllHex(IntPtr hPort, int slot, int[] iValue); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadCNT")] public extern static bool pac_ReadCNT(IntPtr hPort, int slot, int iChannel, ref uint lCounter_Value); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ClearCNT")] public extern static bool pac_ClearCNT(IntPtr hPort, int slot, int iChannel); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_ReadCNTOverflow")] public extern static bool pac_ReadCNTOverflow(IntPtr hPort, int slot, int iChannel, ref int iOverflow); //================================================================== //Interrupt API //================================================================== public delegate int PAC_CALLBACK_FUNC(); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_RegisterSlotInterrupt")] public extern static bool pac_RegisterSlotInterrupt(byte slot, PAC_CALLBACK_FUNC f); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_UnregisterSlotInterrupt")] public extern static bool pac_UnregisterSlotInterrupt(byte slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_SetSlotInterruptPriority")] public extern static bool pac_SetSlotInterruptPriority(byte slot, int nPriority); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_EnableSlotInterrupt")] public extern static void pac_EnableSlotInterrupt(byte slot, bool bEnable); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_InterruptDone")] public extern static void pac_InterruptDone(byte slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_GetSlotInterruptEvent")] public extern static IntPtr pac_GetSlotInterruptEvent(byte slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_SetTriggerType")] public extern static void pac_SetTriggerType(int iType); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_InterruptInitialize")] public extern static bool pac_InterruptInitialize(byte slot); [DllImport("XPacSDK_CE.dll", EntryPoint = "pac_SetSlotInterruptEvent")] public extern static void pac_SetSlotInterruptEvent(byte slot, IntPtr hEvent); //================================================================== //MISC API //================================================================== public static byte[] AnsiString([In] string str) { Encoding ascii = Encoding.ASCII; Encoding unicode = Encoding.Unicode; byte[] unicodeBytes = unicode.GetBytes(str); byte[] temp = Encoding.Convert(unicode, ascii, unicodeBytes); byte[] ret = new byte[temp.Length + 1]; temp.CopyTo(ret, 0); return ret; } public static int StringofByte_Len(byte[] bytes) { int len = 0; for (int i = 0; i < bytes.Length; i++) { if (bytes[i] != 0) { len++; } else break; } return len; } public static string WideString(byte[] CharStr) { #if false //Encoding ascii = Encoding.ASCII; //char[] asciiChars = new char[ascii.GetCharCount(CharStr, 0, CharStr.Length)]; //ascii.GetChars(CharStr, 0, CharStr.Length, asciiChars, 0); #else string asciiString = System.Text.Encoding.Default.GetString(CharStr, 0, StringofByte_Len(CharStr)); #endif return asciiString; } //================================================================== } }