Option Strict Off
Option Explicit On 
Imports System.Runtime.InteropServices
Imports System.Text

Module CAN
	'================================================================
	' Parameters
	'================================================================
	'----------------------------------------
	' Monitor mode
	'----------------------------------------
	Public Const CCAN_MONITOR_OFF As Short = 0							' Monitor mode OFF
	Public Const CCAN_MONITOR_ON As Short = 1							' Monitor mode ON
	'----------------------------------------
	' Message type
	'----------------------------------------
	Public Const CCAN_MSG_TYPE_DISABLE As Short = &H0S					' All messages are valid
	Public Const CCAN_MSG_TYPE_CAN As Short = &H01S						' CAN message
	Public Const CCAN_MSG_TYPE_CAN_FD As Short = &H02S					' CAN FD message
	Public Const CCAN_MSG_TYPE_REMOTE_FRAME As Short = &H04S			' Remote frame
	Public Const CCAN_MSG_TYPE_BITRATE_SWITCH As Short = &H08S			' Bitrate switch
	Public Const CCAN_MSG_TYPE_STANDARD_FORMAT As Short = &H10S			' Standard format
	Public Const CCAN_MSG_TYPE_EXTENDED_FORMAT As Short = &H20S			' Extended format
	'----------------------------------------
	' CAN bus error
	'----------------------------------------
	Public Const CCAN_STATUS_ERR_BIT As Short = &H01S					' Bit error
	Public Const CCAN_STATUS_ERR_FORM As Short = &H02S					' Form error
	Public Const CCAN_STATUS_ERR_STUFF As Short = &H03S					' Stuff error
	Public Const CCAN_STATUS_ERR_ACK As Short = &H04S					' ACK error
	Public Const CCAN_STATUS_ERR_CRC As Short = &H05S					' CRC error
	Public Const CCAN_STATUS_ERR_OTHER As Short = &H06S					' Other error
	'----------------------------------------
	' Channel status
	'----------------------------------------
	Public Const CCAN_CH_STATUS_ERR_ACTIVE As Short = &H0000S			' Error active
	Public Const CCAN_CH_STATUS_ERR_PASSIVE As Short = &H0001S			' Error passive
	Public Const CCAN_CH_STATUS_BUS_OFF As Short = &H0002S				' Bus off
	'----------------------------------------
	' Event type
	'----------------------------------------
	Public Const CCAN_EVENT_RECEIVE As Short = &H01S					' Receive event

	'================================================================
	' Error codes
	'================================================================
	' Initialize Error
	' Common
	Public Const CAN_ERR_SUCCESS As Short = 0							' Normal completed
	Public Const CAN_ERR_INI_RESOURCE As Short = 1						' Invalid resource reference specified
	Public Const CAN_ERR_INI_INTERRUPT As Short = 2						' Invalid interrupt routine registered
	Public Const CAN_ERR_INI_MEMORY As Short = 3						' Invalid memory allocationed
	Public Const CAN_ERR_INI_REGISTRY As Short = 4						' Invalid registry accesse
	Public Const CAN_ERR_SYS_RECOVERED_FROM_STANDBY As Short = 7		' Execute CanResetDevice function because the device has recovered from standby mode.
	Public Const CAN_ERR_INI_NOT_FOUND_SYS_FILE As Short = 8			' Because the ccan.sys file is not found, it is not possible to initialize it.
	Public Const CAN_ERR_INI_DLL_FILE_VERSION As Short = 9				' Because version information on the ccan.dll file cannot be acquired, it is not possible to initialize it.
	Public Const CAN_ERR_INI_SYS_FILE_VERSION As Short = 10				' Because version information on the ccan.sys file cannot be acquired, it is not possible to initialize it.
	Public Const CAN_ERR_INI_NO_MATCH_DRV_VERSION As Short = 11			' Because version information on ccan.dll and ccan.sys is different, it is not possible to initialize it.

	' DLL Error
	' Common
	Public Const CAN_ERR_DLL_DEVICE_NAME As Short = 10000				' Invalid device name specified.
	Public Const CAN_ERR_DLL_INVALID_ID As Short = 10001				' Invalid ID specified.
	Public Const CAN_ERR_DLL_CALL_DRIVER As Short = 10002				' Not call the driver.(Invalid device I/O controller)
	Public Const CAN_ERR_DLL_CREATE_FILE As Short = 10003				' Not create the file.(Invalid CreateFile)
	Public Const CAN_ERR_DLL_CLOSE_FILE As Short = 10004				' Not close the file.(Invalid CloseFile)
	Public Const CAN_ERR_DLL_CREATE_THREAD As Short = 10005				' Not create the thread.(Invalid CreateThread)
	Public Const CAN_ERR_INFO_INVALID_DEVICE As Short = 10050			' Invalid device infomation specified .Please check the spell.
	Public Const CAN_ERR_INFO_NOT_FIND_DEVICE As Short = 10051			' Not find the available device
	Public Const CAN_ERR_INFO_INVALID_INFOTYPE As Short = 10052			' Specified device infomation type beyond the limit
	Public Const CAN_ERR_INFO_NOT_FOUND_DEVINFO As Short = 10053		' No device information was found in the information retrieval.
	' CAN
	Public Const CAN_ERR_DLL_BUFF_ADDRESS As Short = 10100				' Invalid data buffer address
	Public Const CAN_ERR_DLL_HWND As Short = 10200						' Window handle beyond the limit
	Public Const CAN_ERR_DLL_EVENT_TYPE As Short = 10300				' Trigger kind beyond the limit
	Public Const CAN_ERR_DLL_CALLBACK As Short = 10400					' Invalid callback function address.

	' SYS Error
	' Common
	Public Const CAN_ERR_SYS_MEMORY As Short = 20000					' Not secure memory
	Public Const CAN_ERR_SYS_NOT_SUPPORTED As Short = 20001				' This board couldn't use this function
	Public Const CAN_ERR_SYS_BOARD_EXECUTING As Short = 20002			' Board is behaving, not execute
	Public Const CAN_ERR_SYS_USING_OTHER_PROCESS As Short = 20003		' Other process is using the device, not execute
	Public Const CAN_ERR_SYS_NOT_FOUND_PROCESS_DATA As Short = 20004	' Process information is not found.
	' CAN
	Public Const CAN_ERR_SYS_MONITOR_MODE As Short = 20100				' Monitor mode is out of the settable range.
	Public Const CAN_ERR_SYS_CH_NO As Short = 20101						' The channel number is out of the settable range.
	Public Const CAN_ERR_SYS_DATA_FRAME_FORMAT As Short = 20102			' Data frame format is out of the settable range.
	Public Const CAN_ERR_SYS_BITRATE As Short = 20110					' Bit rate is out of the settable range.
	Public Const CAN_ERR_SYS_TOTAL_TQ As Short = 20111					' Total TQ is out of the settable range.
	Public Const CAN_ERR_SYS_TSEG1 As Short = 20120						' Time segment 1 is outside the settable range.
	Public Const CAN_ERR_SYS_SJW As Short = 20121						' Sync jump width is out of the settable range.
	Public Const CAN_ERR_SYS_TSEG2 As Short = 20122						' Time segment 2 is outside the settable range.
	Public Const CAN_ERR_SYS_ARB_BITRATE As Short = 20130				' Arbitration bit rate is outside the settable range.
	Public Const CAN_ERR_SYS_ARB_TSEG1 As Short = 20131					' Arbitration time segment 1 is outside the settable range.
	Public Const CAN_ERR_SYS_ARB_SJW As Short = 20132					' Arbitration synchronous jump width is outside the settable range.
	Public Const CAN_ERR_SYS_ARB_TSEG2 As Short = 20133					' Arbitration time segment 2 is outside the settable range.
	Public Const CAN_ERR_SYS_DATA_BITRATE As Short = 20140				' Bit rate is out of the settable range.
	Public Const CAN_ERR_SYS_DATA_TSEG1 As Short = 20141				' Data time segment 1 is outside the settable range.
	Public Const CAN_ERR_SYS_DATA_SJW As Short = 20142					' Data synchronization jump width is outside the settable range.
	Public Const CAN_ERR_SYS_DATA_TSEG2 As Short = 20143				' Data time segment 2 is outside the settable range.
	Public Const CAN_ERR_SYS_MESSAGE_NUM As Short = 20150				' Number of messages is out of the settable range.
	Public Const CAN_ERR_SYS_MESSAGE_TYPE As Short = 20151				' Message type is outside the settable range.
	Public Const CAN_ERR_SYS_DATA_LEN As Short = 20160					' Data length is outside the settable range.
	Public Const CAN_ERR_SYS_CAN_ID As Short = 20170					' CAN ID is outside the settable range.
	Public Const CAN_ERR_SYS_TIMEOUT_TIME As Short = 20180				' Timeout time is outside the settable range.
	Public Const CAN_ERR_SYS_BUSON As Short = 20200						' Cannot execute because the bus on.
	Public Const CAN_ERR_SYS_REC_FIFO_OVERFLOW As Short = 20220			' Receive FIFO overflowed.
	Public Const CAN_ERR_SYS_DEVICE_ACCESS As Short = 20500				' The device could not be accessed normally.

	'----------------------------------------
	' Delegate
	'----------------------------------------
	Public Delegate Sub PCAN_EVENT_CALLBACK(ByVal Id As Short, ByVal wParam As Integer, ByVal lParam As Integer, ByVal Param As IntPtr)

	'================================================================
	' Prototype definition
	'================================================================
	'----------------------------------------
	' Common function
	'----------------------------------------
	Declare Function CanInit Lib "CCAN.DLL" (ByVal DeviceName As String, ByRef Id As Short) As Integer
	Declare Function CanExit Lib "CCAN.DLL" (ByVal Id As Short) As Integer
	Declare Function CanResetDevice Lib "CCAN.DLL" (ByVal Id As Short) As Integer
	Declare Function CanGetErrorString Lib "CCAN.DLL" (ByVal ErrorCode As Integer, ByVal ErrorString As StringBuilder) As Integer
	'----------------------------------------
	' Information acquisition function
	'----------------------------------------
	Declare Function CanQueryDeviceName Lib "CCAN.DLL" (ByVal Index As Short, ByVal DeviceName As String, ByVal Device As String) As Integer
	Declare Function CanGetMaxChannels Lib "CCAN.DLL" (ByVal Id As Short, ByRef CanChannelNum As UShort, ByRef CanFdChannelNum As UShort) As Integer
	'----------------------------------------
	' Setup and get setup functions
	'----------------------------------------
	Declare Function CanResetDeviceTickCount Lib "CCAN.DLL" (ByVal Id As Short) As Integer
	Declare Function CanSetChannelAcceptance Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal MessageTypeMask As UInteger, ByVal CanIdMask As UInteger, ByVal CanIdCode As UInteger) As Integer
	Declare Function CanGetChannelAcceptance Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByRef MessageTypeMask As UInteger, ByRef CanIdMask As UInteger, ByRef CanIdCode As UInteger) As Integer
	Declare Function CanSetCanTiming Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal Bitrate As UInteger, ByVal TSeg1 As UShort, ByVal SJW As UShort, ByVal TSeg2 As UShort) As Integer
	Declare Function CanGetCanTiming Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByRef Bitrate As UInteger, ByRef TSeg1 As UShort, ByRef SJW As UShort, ByRef TSeg2 As UShort) As Integer
	Declare Function CanSetCanFdTiming Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal ArbBitrate As UInteger, ByVal ArbTSeg1 As UShort, ByVal ArbSJW As UShort, ByVal ArbTSeg2 As UShort, ByVal DataBitrate As UInteger, ByVal DataTSeg1 As UShort, ByVal DataSJW As UShort, ByVal DataTSeg2 As UShort) As Integer
	Declare Function CanGetCanFdTiming Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByRef ArbBitrate As UInteger, ByRef ArbTSeg1 As UShort, ByRef ArbSJW As UShort, ByRef ArbTSeg2 As UShort, ByRef DataBitrate As UInteger, ByRef DataTSeg1 As UShort, ByRef DataSJW As UShort, ByRef DataTSeg2 As UShort) As Integer
	Declare Function CanSetMonitorMode Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal MonitorMode As UShort) As Integer
	Declare Function CanSetTimeout Lib "CCAN.DLL" (ByVal Id As Short, ByVal Timeout As UInteger) As Integer
	Declare Function CanGetTimeout Lib "CCAN.DLL" (ByVal Id As Short, ByRef Timeout As UInteger) As Integer
	'----------------------------------------
	' Bus control function
	'----------------------------------------
	Declare Function CanBusConnect Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort) As Integer
	Declare Function CanBusDisconnect Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort) As Integer
	'----------------------------------------
	' Communication function
	'----------------------------------------
	Declare Function CanTransmit Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal MessageNum As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal MessageType() As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal CanId() As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal DataLength() As UShort, <MarshalAs(UnmanagedType.LPArray)> ByVal Data(,) As Byte) As Integer
	Declare Function CanReceive Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByRef MessageNum As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal MessageType() As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal CanId() As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal DataLength() As UShort, <MarshalAs(UnmanagedType.LPArray)> ByVal Data(,) As Byte, <MarshalAs(UnmanagedType.LPArray)> ByVal ErrorCode() As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal TickCount() As ULong) As Integer
	Declare Function CanGetChannelStatus Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByRef ChannelStatus As UShort, ByRef TxErrorCount As UShort, ByRef RxErrorCount As UShort) As Integer
	'----------------------------------------
	' Event function
	'----------------------------------------
	Declare Function CanNotifyEvent Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal EventType As UShort, ByVal CallBackProc As IntPtr, ByVal Param As IntPtr) As Integer
	Declare Function CanStopNotifyEvent Lib "CCAN.DLL" (ByVal Id As Short, ByVal ChannelNo As UShort, ByVal EventType As UShort) As Integer

End Module
