using System; using System.Reflection; using System.Runtime.InteropServices; using System.Text; namespace PACNET { /// UART API public class UART { /// IN_DATA public const int IN_DATA = 0; /// OUT_DATA public const int OUT_DATA = 1; /// CTS public const int CTS = 0; /// DSR public const int DSR = 1; /// RI public const int RI = 2; /// CD public const int CD = 3; /// CTO_TIMEOUT_ALL public const int CTO_TIMEOUT_ALL = 0; /// CTO_READ_RETRY_TIMEOUT public const int CTO_READ_RETRY_TIMEOUT = 1; /// CTO_READ_TOTAL_TIMEOUT public const int CTO_READ_TOTAL_TIMEOUT = 2; /// CTO_WRITE_TOTAL_TIMEOUT public const int CTO_WRITE_TOTAL_TIMEOUT = 3; //8.1. PAC_API bool uart_BinRecv(HANDLE hPort, LPSTR buf, DWORD in_Len) /// /// This function is applied to receive the fix length response. /// /// [in] Handle to the open COM port. /// [out] A pointer to a buffer that receives the data. /// [in] The length of result string. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_BinRecv")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool BinRecv(IntPtr hPort, byte[] buf, uint in_Len); //8.2. PAC_API bool uart_BinSend(HANDLE hPort, LPCSTR buf, DWORD in_Len) /// /// Send out the command string by fix length, which is controlled by the Parameters “in_Len”. /// /// [in] Handle to the open COM port. /// [in] A pointer to a buffer that send the data. /// [in] The length of result string. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_BinSend")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool BinSend(IntPtr hPort, byte[] buf, uint in_Len); //8.3. PAC_API bool uart_BinSendCmd(HANDLE hPort, LPCSTR ByteCmd, DWORD in_Len, LPSTR ByteResult, DWORD out_Len) /// /// This function is used to Send binary command and receive binary data with the fixed length. /// /// [in] Handle to the open COM. /// [in] A pointer to a command. /// [in] The length of command string. /// [out] A pointer to a buffer that receives the data. /// [in] The length of result string. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_BinSendCmd")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool BinSendCmd(IntPtr hPort, byte[] ByteCmd, uint in_Len, byte[] ByteResult, uint out_Len); //8.4. PAC_API bool uart_Close(HANDLE hPort) /// /// This function closes the COM port which has been opened. /// /// [in] Handle to the open COM port to close. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_Close")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool Close(IntPtr hPort); //8.5. PAC_API void uart_EnableCheckSum(HANDLE hPort, bool bEnable) // Disable by default /// /// This function turns on the check sum or not. /// /// [in] Handle to the open COM port to close. /// [in] Decide the check sum turning on or not. Default is disabling. [DllImport("PACSDK.dll", EntryPoint = "uart_EnableCheckSum")] public static extern void EnableCheckSum(IntPtr hPort, bool bEnable); //8.6. PAC_API DWORD uart_GetDataSize(HANDLE hPort, int data_type) /// /// This function retrieves the number of bytes received by the serial provider but not yet read /// by a uart_Recv operation, or of user data remaining to transmit for write operations. /// /// [in] Handle to the open COM port. /// [in] A value specifies to retrieve in or out buffer. /// This Parameters can be following values, 0: IN_DATA, 1: #define OUT_DATA /// The number of bytes in/out buffer but not yet read/write. [DllImport("PACSDK.dll", EntryPoint = "uart_GetDataSize")] public static extern uint GetDataSize(IntPtr hPort, int data_type); //8.7. PAC_API bool uart_GetLineStatus(HANDLE hPort, int pin) /// /// This function retrieves the modem control-register values. /// /// [in] Handle to the open COM port. /// [in] A variable specifies state of a pin of the COM port. /// This Parameters can be following values, 0: CTS, 1: DSR, 2: RI 3:CD /// TRUE indicates the pin’s state is ON. 0 indicates OFF. [DllImport("PACSDK.dll", EntryPoint = "uart_GetLineStatus")] public static extern uint GetLineStatus(IntPtr hPort, int pin); //8.8. PAC_API HANDLE uart_Open(LPCSTR ConnectionString) // "COM0:,115200,8,N,1" [DllImport("PACSDK.dll", EntryPoint = "uart_Open")] static extern IntPtr _uart_Open(byte[] ConnectionString); /// /// This function opens the COM port and specifies the baud rate, parity bits, data bits, and stop bits. /// /// [in] Specifies the COM port, baud rate, parity bits, data bits, and stop bits. /// TRUE indicates the pin’s state is ON. 0 indicates OFF. public static IntPtr Open(string ConnectionString) { return _uart_Open(MISC.AnsiString(ConnectionString)); } //8.9. PAC_API bool uart_Recv(HANDLE hPort, LPSTR buf) /// /// This function retrieves data through the COM port which have been opened. /// /// [in] Handle to the open COM port. /// [out] A pointer to a buffer that receives the data. /// 1: indicates success. 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_Recv")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool Recv(IntPtr hPort, [In, Out] byte[] buf); //8.10. PAC_API bool uart_Send(HANDLE hPort, LPCSTR buf) /// /// This function sends data through the COM port which have been opened. /// /// [in] Handle to the open COM port. /// [in] A pointer to a buffer that send the data, 2048 bytes maximum. /// 1: indicates success. 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_Send")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool Send(IntPtr hPort, byte[] buf); //8.11. PAC_API bool uart_SendCmd(HANDLE hPort, LPCSTR cmd, LPSTR szResult) /// /// This function sends commands through the COM port which have been opened. /// /// [in] Handle to the open COM. /// [in] A pointer to a command. /// [out] A pointer to a buffer that receives the data. /// 1: indicates success. 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_SendCmd")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool SendCmd(IntPtr hPort, byte[] cmd, [In, Out] byte[] szResult); //8.12. PAC_API void uart_SetTerminator(HANDLE hPort, LPCSTR szTerm) // CR by default /// /// This function sets the terminate characters. /// /// [in] Handle to the open COM port. /// [in] Pointer the terminate characters. Default is CR (0x0D). [DllImport("PACSDK.dll", EntryPoint = "uart_SetTerminator")] public static extern void SetTerminator(IntPtr hPort, byte[] szTerm); //8.13. PAC_API void uart_SetTimeOut(HANDLE hPort, DWORD msec, int ctoType) /// /// This function sets the time out timer. /// /// [in] Handle to the open COM port. /// [in] millisecond to the timer. /// [in] Specifies the time out timer type as following: /// 0: CTO_TIMEOUT_ALL, 1: CTO_READ_RETRY_TIMEOUT, 2: CTO_READ_TOTAL_TIMEOUT, 3: CTO_WRITE_TOTAL_TIMEOUT [DllImport("PACSDK.dll", EntryPoint = "uart_SetTimeOut")] public static extern void SetTimeOut(IntPtr hPort, uint msec, int ctoType); //PAC_API boo uart_RecvExt(HANDLE hPort, LPSTR buf, DWORD in_Len); /// /// This function is applied to receive the fix length response. /// /// [in] Handle to the open COM port. /// [out] A pointer to a buffer that receives the data. /// [in] The length of result string. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_RecvExt")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool RecvExt(IntPtr hPort, byte[] buf, uint in_Len); //PAC_API bool uart_SendExt(HANDLE hPort, LPCSTR buf, DWORD out_Len); /// /// Send out the command string by fix length, which is controlled by the Parameters “in_Len”. /// /// [in] Handle to the open COM port. /// [in] A pointer to a buffer that send the data. /// [in] The length of result string. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_SendExt")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool SendExt(IntPtr hPort, byte[] buf, uint out_Len); //PAC_API bool uart_SendCmdExt(HANDLE hPort, LPCSTR cmd, DWORD out_Len, LPSTR szResult, DWORD in_Len); /// /// This function is used to Send string command and receive string data with the fixed length. /// /// [in] Handle to the open COM. /// [in] A pointer to a command. /// [in] The length of command string. /// [out] A pointer to a buffer that receives the data. /// [in] The length of result string. /// 1: indicates success, 0: indicates failure. [DllImport("PACSDK.dll", EntryPoint = "uart_SendCmdExt")] [return: MarshalAs(UnmanagedType.I1)] public static extern bool SendCmdExt(IntPtr hPort, byte[] ByteCmd, uint in_Len, byte[] ByteResult, uint out_Len); } }