﻿Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1
    '-----------------------------------
    ' Macro definition
    '-----------------------------------
    Public Const MAX_TRANSMIT_DATA As Integer = 8        ' Maximum number of transmitted data
    Public Const MAX_RECEIVE_MESSAGE As Integer = 100    ' Maximum number of received messages
    Public Const MAX_RECEIVE_DATA As Integer = 64        ' Maximum number of received data

    '-----------------------------------
    ' Declaration of global variables
    '-----------------------------------
    Dim g_id As Short                                    ' Device ID
    Dim textBox_TransmitData(7) As TextBox               ' Transmit data controls
    Dim gCh As GCHandle                                  ' GCHandle for callback routine
    Dim pdelegate_func As PCAN_EVENT_CALLBACK            ' Delegate for event notification
    Dim pfunc As IntPtr                                  ' IntPtr for callback routine

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim data_no As Short

        '-----------------------------------
        ' Set the default button
        '-----------------------------------
        AcceptButton = button_Exit
        '-----------------------------------
        ' Setting for control array
        '-----------------------------------
        '-----------------
        ' Controls for transmit data
        '-----------------
        textBox_TransmitData(0) = textBox_TransmitData0
        textBox_TransmitData(1) = textBox_TransmitData1
        textBox_TransmitData(2) = textBox_TransmitData2
        textBox_TransmitData(3) = textBox_TransmitData3
        textBox_TransmitData(4) = textBox_TransmitData4
        textBox_TransmitData(5) = textBox_TransmitData5
        textBox_TransmitData(6) = textBox_TransmitData6
        textBox_TransmitData(7) = textBox_TransmitData7
        '-----------------------------------
        ' Initializing screen display
        '-----------------------------------
        '-----------------------------------
        ' Initialization
        '-----------------------------------
        textBox_DeviceName.Text = "CAN000"
        '-----------------------------------
        ' Common setting value
        '-----------------------------------
        textBox_ChannelNo.Text = "0"
        '-----------------------------------
        ' CAN communication setting values
        '-----------------------------------
        textBox_CanBitrate.Text = "125000"
        textBox_CanTSEG1.Text = "240"
        textBox_CanSJW.Text = "80"
        textBox_CanTSEG2.Text = "80"
        '-----------------------------------
        ' CAN FD communication setting values
        '-----------------------------------
        '-----------------
        ' Arbitration bitrate
        '-----------------
        textBox_CanFdArbBitrate.Text = "125000"
        textBox_CanFdArbTSEG1.Text = "240"
        textBox_CanFdArbSJW.Text = "80"
        textBox_CanFdArbTSEG2.Text = "80"
        '-----------------
        ' Data bitrate
        '-----------------
        textBox_CanFdDataBitrate.Text = "1000000"
        textBox_CanFdDataTSEG1.Text = "30"
        textBox_CanFdDataSJW.Text = "10"
        textBox_CanFdDataTSEG2.Text = "10"
        '-----------------------------------
        ' Acceptance target CAN ID
        '-----------------------------------
        '-----------------
        ' Message type
        '-----------------
        checkBox_MessageTypeMaskCan.Checked = False
        checkBox_MessageTypeMaskCanFd.Checked = False
        checkBox_MessageTypeMaskRemoteFrame.Checked = False
        checkBox_MessageTypeMaskBitrateSwitch.Checked = False
        checkBox_MessageTypeMaskStdFormat.Checked = False
        checkBox_MessageTypeMaskExtFormat.Checked = False
        '-----------------
        ' CAN ID
        '-----------------
        textBox_CanIdMask.Text = "1FFFFF00"
        textBox_CanIdCode.Text = "100"
        '-----------------------------------
        ' Transmit
        '-----------------------------------
        '-----------------
        ' Message type
        '-----------------
        checkBox_TransmitMessageTypeCan.Checked = True
        checkBox_TransmitMessageTypeCanFd.Checked = False
        checkBox_TransmitMessageTypeRemoteFrame.Checked = False
        checkBox_TransmitMessageTypeBitrateSwitch.Checked = False
        checkBox_TransmitMessageTypeExtFormat.Checked = False
        '-----------------
        ' CAN ID
        '-----------------
        textBox_TransmitCanId.Text = "100"
        '-----------------
        ' Data length
        '-----------------
        comboBox_TransmitDataLength.Items.Add("0")
        comboBox_TransmitDataLength.Items.Add("1")
        comboBox_TransmitDataLength.Items.Add("2")
        comboBox_TransmitDataLength.Items.Add("3")
        comboBox_TransmitDataLength.Items.Add("4")
        comboBox_TransmitDataLength.Items.Add("5")
        comboBox_TransmitDataLength.Items.Add("6")
        comboBox_TransmitDataLength.Items.Add("7")
        comboBox_TransmitDataLength.Items.Add("8")
        comboBox_TransmitDataLength.SelectedIndex = 8
        '-----------------
        ' Data
        '-----------------
        For data_no = 0 To (MAX_TRANSMIT_DATA - 1)
            textBox_TransmitData(data_no).Text = data_no.ToString("x")
        Next data_no
        '-----------------------------------
        ' Status
        '-----------------------------------
        textBox_Status.Text = ""
        textBox_TxErrorCount.Text = "0"
        textBox_RxErrorCount.Text = "0"
        '-----------------------------------
        ' Return value
        '-----------------------------------
        textBox_Ret.Text = ""
        '-----------------------------------
        ' Initialize the delegate for event notification
        '-----------------------------------
        pdelegate_func = New PCAN_EVENT_CALLBACK(AddressOf CallBackProc)
        '-----------------------------------
        ' Do not release the delegate
        '-----------------------------------
        If (gCh.IsAllocated = False) Then
            gCh = GCHandle.Alloc(pdelegate_func)
        End If
    End Sub

    '=========================================================
    ' CanInit()
    '=========================================================
    Private Sub button_CanInit_Click(sender As Object, e As EventArgs) Handles button_CanInit.Click
        Dim ret As Integer
        Dim device_name As String
        Dim error_string As New StringBuilder("", 256)

        '-----------------------------
        ' Get device name
        '-----------------------------
        device_name = textBox_DeviceName.Text
        '-----------------------------
        ' Initialization and get device ID
        '-----------------------------
        ret = CanInit(device_name, g_id)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanSetCanTiming()
    '=========================================================
    Private Sub button_CanSetCanTiming_Click(sender As Object, e As EventArgs) Handles button_CanSetCanTiming.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim bitrate As UInteger
        Dim tseg1 As UShort
        Dim sjw As UShort
        Dim tseg2 As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Store bitrate
        '-----------------------------
        If (UInteger.TryParse(textBox_CanBitrate.Text, bitrate) = False) Then
            bitrate = 0
        End If
        '-----------------------------
        ' Store time segment 1
        '-----------------------------
        If (UShort.TryParse(textBox_CanTSEG1.Text, tseg1) = False) Then
            tseg1 = 0
        End If
        '-----------------------------
        ' Store SJW (Synchronization Jump Width)
        '-----------------------------
        If (UShort.TryParse(textBox_CanSJW.Text, sjw) = False) Then
            sjw = 0
        End If
        '-----------------------------
        ' Store time segment 2
        '-----------------------------
        If (UShort.TryParse(textBox_CanTSEG2.Text, tseg2) = False) Then
            tseg2 = 0
        End If
        '-----------------------------
        ' CAN communication timing setting
        '-----------------------------
        ret = CanSetCanTiming(g_id, channel_no, bitrate, tseg1, sjw, tseg2)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanGetCanTiming()
    '=========================================================
    Private Sub button_CanGetCanTiming_Click(sender As Object, e As EventArgs) Handles button_CanGetCanTiming.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim bitrate As UInteger
        Dim tseg1 As UShort
        Dim sjw As UShort
        Dim tseg2 As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Get CAN communication timing setting
        '-----------------------------
        ret = CanGetCanTiming(g_id, channel_no, bitrate, tseg1, sjw, tseg2)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
        '-----------------------------
        ' Update bitrate display
        '-----------------------------
        textBox_CanBitrate.Text = bitrate.ToString()
        '-----------------------------
        ' Update time segment 1 display
        '-----------------------------
        textBox_CanTSEG1.Text = tseg1.ToString()
        '-----------------------------
        ' Update SJW (Synchronization Jump Width) display
        '-----------------------------
        textBox_CanSJW.Text = sjw.ToString()
        '-----------------------------
        ' Update time segment 2 display
        '-----------------------------
        textBox_CanTSEG2.Text = tseg2.ToString()
    End Sub

    '=========================================================
    ' CanSetCanFdTiming()
    '=========================================================
    Private Sub button_CanSetCanFdTiming_Click(sender As Object, e As EventArgs) Handles button_CanSetCanFdTiming.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim arb_bitrate, data_bitrate As UInteger
        Dim arb_tseg1, data_tseg1 As UShort
        Dim arb_sjw, data_sjw As UShort
        Dim arb_tseg2, data_tseg2 As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Store arbitration bitrate
        '-----------------------------
        If (UInteger.TryParse(textBox_CanFdArbBitrate.Text, arb_bitrate) = False) Then
            arb_bitrate = 0
        End If
        '-----------------------------
        ' Store arbitration time segment 1
        '-----------------------------
        If (UShort.TryParse(textBox_CanFdArbTSEG1.Text, arb_tseg1) = False) Then
            arb_tseg1 = 0
        End If
        '-----------------------------
        ' Store arbitration SJW (Synchronization Jump Width)
        '-----------------------------
        If (UShort.TryParse(textBox_CanFdArbSJW.Text, arb_sjw) = False) Then
            arb_sjw = 0
        End If
        '-----------------------------
        ' Store arbitration time segment 2
        '-----------------------------
        If (UShort.TryParse(textBox_CanFdArbTSEG2.Text, arb_tseg2) = False) Then
            arb_tseg2 = 0
        End If
        '-----------------------------
        ' Store data bitrate
        '-----------------------------
        If (UInteger.TryParse(textBox_CanFdDataBitrate.Text, data_bitrate) = False) Then
            data_bitrate = 0
        End If
        '-----------------------------
        ' Store data time segment 1
        '-----------------------------
        If (UShort.TryParse(textBox_CanFdDataTSEG1.Text, data_tseg1) = False) Then
            data_tseg1 = 0
        End If
        '-----------------------------
        ' Store data SJW (Synchronization Jump Width)
        '-----------------------------
        If (UShort.TryParse(textBox_CanFdDataSJW.Text, data_sjw) = False) Then
            data_sjw = 0
        End If
        '-----------------------------
        ' Store data time segment 2
        '-----------------------------
        If (UShort.TryParse(textBox_CanFdDataTSEG2.Text, data_tseg2) = False) Then
            data_tseg2 = 0
        End If
        '-----------------------------
        ' CAN FD communication timing setting
        '-----------------------------
        ret = CanSetCanFdTiming(g_id, channel_no, arb_bitrate, arb_tseg1, arb_sjw, arb_tseg2, data_bitrate, data_tseg1, data_sjw, data_tseg2)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanGetCanFdTiming()
    '=========================================================
    Private Sub button_CanGetCanFdTiming_Click(sender As Object, e As EventArgs) Handles button_CanGetCanFdTiming.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim arb_bitrate, data_bitrate As UInteger
        Dim arb_tseg1, data_tseg1 As UShort
        Dim arb_sjw, data_sjw As UShort
        Dim arb_tseg2, data_tseg2 As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Get CAN FD communication timing setting
        '-----------------------------
        ret = CanGetCanFdTiming(g_id, channel_no, arb_bitrate, arb_tseg1, arb_sjw, arb_tseg2, data_bitrate, data_tseg1, data_sjw, data_tseg2)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
        '-----------------------------
        ' Update arbitration bitrate display
        '-----------------------------
        textBox_CanFdArbBitrate.Text = arb_bitrate.ToString()
        '-----------------------------
        ' Update arbitration time segment 1 display
        '-----------------------------
        textBox_CanFdArbTSEG1.Text = arb_tseg1.ToString()
        '-----------------------------
        ' Update arbitration SJW (Synchronization Jump Width) display
        '-----------------------------
        textBox_CanFdArbSJW.Text = arb_sjw.ToString()
        '-----------------------------
        ' Update arbitration time segment 2 display
        '-----------------------------
        textBox_CanFdArbTSEG2.Text = arb_tseg2.ToString()
        '-----------------------------
        ' Update data bitrate display
        '-----------------------------
        textBox_CanFdDataBitrate.Text = data_bitrate.ToString()
        '-----------------------------
        ' Update data time segment 1 display
        '-----------------------------
        textBox_CanFdDataTSEG1.Text = data_tseg1.ToString()
        '-----------------------------
        ' Update data SJW (Synchronization Jump Width) display
        '-----------------------------
        textBox_CanFdDataSJW.Text = data_sjw.ToString()
        '-----------------------------
        ' Update data time segment 2 display
        '-----------------------------
        textBox_CanFdDataTSEG2.Text = data_tseg2.ToString()
    End Sub

    '=========================================================
    ' CanSetChannelAcceptance()
    '=========================================================
    Private Sub button_CanSetChannelAcceptance_Click(sender As Object, e As EventArgs) Handles button_CanSetChannelAcceptance.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim message_type_mask As UInteger
        Dim can_id_mask As UInteger
        Dim can_id_code As UInteger

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Store receive message type mask
        '-----------------------------
        '-----------------
        ' Initialization
        '-----------------
        message_type_mask = CCAN_MSG_TYPE_DISABLE
        '-----------------
        ' Message type CAN
        '-----------------
        If (checkBox_MessageTypeMaskCan.Checked) Then
            message_type_mask = message_type_mask Or CCAN_MSG_TYPE_CAN
        End If
        '-----------------
        ' Message type CAN FD
        '-----------------
        If (checkBox_MessageTypeMaskCanFd.Checked) Then
            message_type_mask = message_type_mask Or CCAN_MSG_TYPE_CAN_FD
        End If
        '-----------------
        ' Message type Remote frame
        '-----------------
        If (checkBox_MessageTypeMaskRemoteFrame.Checked) Then
            message_type_mask = message_type_mask Or CCAN_MSG_TYPE_REMOTE_FRAME
        End If
        '-----------------
        ' Message type Bitrate switch
        '-----------------
        If (checkBox_MessageTypeMaskBitrateSwitch.Checked) Then
            message_type_mask = message_type_mask Or CCAN_MSG_TYPE_BITRATE_SWITCH
        End If
        '-----------------
        ' Message type Standard format
        '-----------------
        If (checkBox_MessageTypeMaskStdFormat.Checked) Then
            message_type_mask = message_type_mask Or CCAN_MSG_TYPE_STANDARD_FORMAT
        End If
        '-----------------
        ' Message type Extended format
        '-----------------
        If (checkBox_MessageTypeMaskExtFormat.Checked) Then
            message_type_mask = message_type_mask Or CCAN_MSG_TYPE_EXTENDED_FORMAT
        End If
        '-----------------------------
        ' Store CAN ID mask
        '-----------------------------
        Try
            can_id_mask = Convert.ToUInt32(textBox_CanIdMask.Text, 16)
        Catch
            can_id_mask = 0
        End Try
        '-----------------------------
        ' Store CAN ID code
        '-----------------------------
        Try
            can_id_code = Convert.ToUInt32(textBox_CanIdCode.Text, 16)
        Catch
            can_id_code = 0
        End Try
        '-----------------------------
        ' Receive acceptance setting
        '-----------------------------
        ret = CanSetChannelAcceptance(g_id, channel_no, message_type_mask, can_id_mask, can_id_code)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanGetChannelAcceptance()
    '=========================================================
    Private Sub button_CanGetChannelAcceptance_Click(sender As Object, e As EventArgs) Handles button_CanGetChannelAcceptance.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim message_type_mask As UInteger
        Dim can_id_mask As UInteger
        Dim can_id_code As UInteger

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Get receive acceptance setting
        '-----------------------------
        ret = CanGetChannelAcceptance(g_id, channel_no, message_type_mask, can_id_mask, can_id_code)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
        '-----------------------------
        ' Message type CAN
        '-----------------------------
        If ((message_type_mask And CCAN_MSG_TYPE_CAN) = CCAN_MSG_TYPE_CAN) Then
            checkBox_MessageTypeMaskCan.Checked = True
        Else
            checkBox_MessageTypeMaskCan.Checked = False
        End If
        '-----------------------------
        ' Message type CAN FD
        '-----------------------------
        If ((message_type_mask And CCAN_MSG_TYPE_CAN_FD) = CCAN_MSG_TYPE_CAN_FD) Then
            checkBox_MessageTypeMaskCanFd.Checked = True
        Else
            checkBox_MessageTypeMaskCanFd.Checked = False
        End If
        '-----------------------------
        ' Message type Remote frame
        '-----------------------------
        If ((message_type_mask And CCAN_MSG_TYPE_REMOTE_FRAME) = CCAN_MSG_TYPE_REMOTE_FRAME) Then
            checkBox_MessageTypeMaskRemoteFrame.Checked = True
        Else
            checkBox_MessageTypeMaskRemoteFrame.Checked = False
        End If
        '-----------------------------
        ' Message type Bitrate switch
        '-----------------------------
        If ((message_type_mask And CCAN_MSG_TYPE_BITRATE_SWITCH) = CCAN_MSG_TYPE_BITRATE_SWITCH) Then
            checkBox_MessageTypeMaskBitrateSwitch.Checked = True
        Else
            checkBox_MessageTypeMaskBitrateSwitch.Checked = False
        End If
        '-----------------------------
        ' Message type Standard format
        '-----------------------------
        If ((message_type_mask And CCAN_MSG_TYPE_STANDARD_FORMAT) = CCAN_MSG_TYPE_STANDARD_FORMAT) Then
            checkBox_MessageTypeMaskStdFormat.Checked = True
        Else
            checkBox_MessageTypeMaskStdFormat.Checked = False
        End If
        '-----------------------------
        ' Message type Extended format
        '-----------------------------
        If ((message_type_mask And CCAN_MSG_TYPE_EXTENDED_FORMAT) = CCAN_MSG_TYPE_EXTENDED_FORMAT) Then
            checkBox_MessageTypeMaskExtFormat.Checked = True
        Else
            checkBox_MessageTypeMaskExtFormat.Checked = False
        End If
        '-----------------------------
        ' Update CAN ID mask display
        '-----------------------------
        textBox_CanIdMask.Text = String.Format("{0:X}", can_id_mask)
        '-----------------------------
        ' Update CAN ID code display
        '-----------------------------
        textBox_CanIdCode.Text = String.Format("{0:X}", can_id_code)
    End Sub

    '=========================================================
    ' CanBusConnect()
    '=========================================================
    Private Sub button_CanBusConnect_Click(sender As Object, e As EventArgs) Handles button_CanBusConnect.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Bus connection
        '-----------------------------
        ret = CanBusConnect(g_id, channel_no)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanBusDisconnect()
    '=========================================================
    Private Sub button_CanBusDisconnect_Click(sender As Object, e As EventArgs) Handles button_CanBusDisconnect.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Bus disconnection
        '-----------------------------
        ret = CanBusDisconnect(g_id, channel_no)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanTransmit()
    '=========================================================
    Private Sub button_CanTransmit_Click(sender As Object, e As EventArgs) Handles button_CanTransmit.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim message_num As UInteger
        Dim message_type(0) As UInteger
        Dim can_id(0) As UInteger
        Dim data_length(0) As UShort
        Dim transmit_data(0, MAX_TRANSMIT_DATA - 1) As Byte
        Dim data_no As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Store transmit message type
        '-----------------------------
        '-----------------
        ' Initialization
        '-----------------
        message_type(0) = 0
        '-----------------
        ' Message type CAN
        '-----------------
        If (checkBox_TransmitMessageTypeCan.Checked) Then
            message_type(0) = message_type(0) Or CCAN_MSG_TYPE_CAN
        End If
        '-----------------
        ' Message type CAN FD
        '-----------------
        If (checkBox_TransmitMessageTypeCanFd.Checked) Then
            message_type(0) = message_type(0) Or CCAN_MSG_TYPE_CAN_FD
        End If
        '-----------------
        ' Message type Remote frame
        '-----------------
        If (checkBox_TransmitMessageTypeRemoteFrame.Checked) Then
            message_type(0) = message_type(0) Or CCAN_MSG_TYPE_REMOTE_FRAME
        End If
        '-----------------
        ' Message type Bitrate switch
        '-----------------
        If (checkBox_TransmitMessageTypeBitrateSwitch.Checked) Then
            message_type(0) = message_type(0) Or CCAN_MSG_TYPE_BITRATE_SWITCH
        End If
        '-----------------
        ' Message type Extended format
        '-----------------
        If (checkBox_TransmitMessageTypeExtFormat.Checked) Then
            message_type(0) = message_type(0) Or CCAN_MSG_TYPE_EXTENDED_FORMAT
        End If
        '-----------------------------
        ' Store CAN ID
        '-----------------------------
        Try
            can_id(0) = Convert.ToUInt32(textBox_TransmitCanId.Text, 16)
        Catch
            can_id(0) = 0
        End Try
        '-----------------------------
        ' Number of transmit messages
        '-----------------------------
        message_num = 1
        '-----------------------------
        ' Store transmit message data length
        '-----------------------------
        If (UShort.TryParse(comboBox_TransmitDataLength.Text, data_length(0)) = False) Then
            data_length(0) = 0
        End If
        '-----------------------------
        ' Store transmit message data
        '-----------------------------
        IF (data_length(0) > 0) Then
            For data_no = 0 To (data_length(0) - 1)
                Try
                    transmit_data(0, data_no) = Convert.ToByte(textBox_TransmitData(data_no).Text, 16)
                Catch
                    transmit_data(0, data_no) = 0
                End Try
            Next data_no
        End If
        '-----------------------------
        ' CAN / CAN FD Transmit
        '-----------------------------
        ret = CanTransmit(g_id, channel_no, message_num, message_type, can_id, data_length, transmit_data)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' Receive Start
    '=========================================================
    Private Sub button_CanReceiveStart_Click(sender As Object, e As EventArgs) Handles button_CanReceiveStart.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort

        '-----------------------------
        ' Clear receive message list
        '-----------------------------
        listView_RecviceData.Items.Clear()
        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Get the fixed pointer of the delegate for event notification
        '-----------------------------
        pfunc = Marshal.GetFunctionPointerForDelegate(pdelegate_func)
        '-----------------------------
        ' Receive event start
        '-----------------------------
        ret = CanNotifyEvent(g_id, channel_no, CCAN_EVENT_RECEIVE, pfunc, Nothing)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' Receive Stop
    '=========================================================
    Private Sub button_CanReceiveStop_Click(sender As Object, e As EventArgs) Handles button_CanReceiveStop.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Receive event stop
        '-----------------------------
        ret = CanStopNotifyEvent(g_id, channel_no, CCAN_EVENT_RECEIVE)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' CanGetChannelStatus()
    '=========================================================
    Private Sub button_CanGetChannelStatus_Click(sender As Object, e As EventArgs) Handles button_CanGetChannelStatus.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim channel_no As UShort
        Dim channel_status As UShort
        Dim tx_error_count As UShort
        Dim rx_error_count As UShort

        '-----------------------------
        ' Store channel number
        '-----------------------------
        If (UShort.TryParse(textBox_ChannelNo.Text, channel_no) = False) Then
            channel_no = 0
        End If
        '-----------------------------
        ' Get channel status
        '-----------------------------
        ret = CanGetChannelStatus(g_id, channel_no, channel_status, tx_error_count, rx_error_count)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
        '-----------------------------
        ' Update channel status display
        '-----------------------------
        If (channel_status = CCAN_CH_STATUS_ERR_ACTIVE) Then
            textBox_Status.Text = "Error Active"
        End If
        If (channel_status = CCAN_CH_STATUS_ERR_PASSIVE) Then
            textBox_Status.Text = "Error Passive"
        End If
        If (channel_status = CCAN_CH_STATUS_BUS_OFF) Then
            textBox_Status.Text = "Bus OFF"
        End If
        '-----------------------------
        ' Update transmit error count display
        '-----------------------------
        textBox_TxErrorCount.Text = tx_error_count.ToString()
        '-----------------------------
        ' Update receive error count display
        '-----------------------------
        textBox_RxErrorCount.Text = rx_error_count.ToString()
    End Sub

    '=========================================================
    ' CanExit
    '=========================================================
    Private Sub button_CanExit_Click(sender As Object, e As EventArgs) Handles button_CanExit.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)

        '-----------------------------
        ' Exit process
        '-----------------------------
        ret = CanExit(g_id)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
    End Sub

    '=========================================================
    ' Application termination
    '=========================================================
    Private Sub button_Exit_Click(sender As Object, e As EventArgs) Handles button_Exit.Click
        '-----------------------------
        ' Close dialog
        '-----------------------------
        Me.Close()
    End Sub

    '=========================================================
    ' Callback function
    '=========================================================
    Protected Sub CallBackProc(ByVal Id As Short, ByVal wParam As Integer, ByVal lParam As Integer, ByVal Param As IntPtr)
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim item As ListViewItem
        Dim channel_no As UShort
        Dim message_num As UInteger
        Dim message_type(MAX_RECEIVE_MESSAGE - 1) As UInteger
        Dim can_id(MAX_RECEIVE_MESSAGE - 1) As UInteger
        Dim data_length(MAX_RECEIVE_MESSAGE - 1) As UShort
        Dim receive_data(MAX_RECEIVE_MESSAGE - 1, MAX_RECEIVE_DATA - 1) As Byte
        Dim error_code(MAX_RECEIVE_MESSAGE - 1) As UInteger
        Dim tick_count(MAX_RECEIVE_MESSAGE - 1) As ULong
        Dim message_count, data_count As UInteger

        '-----------------------------
        ' Store channel number
        '-----------------------------
        channel_no = lParam
        '-----------------------------
        ' Number of receive messages
        '-----------------------------
        message_num = MAX_RECEIVE_MESSAGE
        '-----------------------------
        ' CAN / CAN FD Receive
        '-----------------------------
        ret = CanReceive(g_id, channel_no, message_num, message_type, can_id, data_length, receive_data, error_code, tick_count)
        '-----------------------------
        ' Error process
        '-----------------------------
        CanGetErrorString(ret, error_string)
        textBox_Ret.Text = "ret = " & ret & " : " & error_string.ToString()
        If (ret <> CAN_ERR_SUCCESS) Then
            Exit Sub
        End If
        '-----------------------------
        ' Add list for each received message
        '-----------------------------
        If (message_num > 0) Then
            For message_count = 0 To (message_num - 1)
                '-----------------------------
                ' Add item: No.
                '-----------------------------
                item = New ListViewItem(listView_RecviceData.Items.Count.ToString())
                '-----------------------------
                ' Add item: TickCount
                '-----------------------------
                item.SubItems.Add(String.Format("{0:D}", tick_count(message_count)))
                '-----------------------------
                ' Add item: MessageType
                '-----------------------------
                item.SubItems.Add(String.Format("0x{0:X}", message_type(message_count)))
                '-----------------------------
                ' Add item: CanId
                '-----------------------------
                item.SubItems.Add(String.Format("0x{0:X}", can_id(message_count)))
                '-----------------------------
                ' Add item: DataLength
                '-----------------------------
                item.SubItems.Add(String.Format("{0:D}", data_length(message_count)))
                '-----------------------------
                ' Add item: Data
                '-----------------------------
                str = ""
                If (data_length(message_count) > 0) Then
                    For data_count = 0 To (data_length(message_count) - 1)
                        str += String.Format("{0:X2}", receive_data(message_count, data_count))
                        If (data_count < (data_length(message_count) - 1)) Then
                            str += ", "
                        End If
                    Next data_count
                End If
                item.SubItems.Add(str)
                '-----------------------------
                ' Add item: ErrorCode
                '-----------------------------
                item.SubItems.Add(String.Format("0x{0:X}", error_code(message_count)))
                '-----------------------------
                ' Add item
                '-----------------------------
                listView_RecviceData.Items.Add(item)
                '-----------------------------
                ' Scroll to display additional items
                '-----------------------------
                listView_RecviceData.EnsureVisible(listView_RecviceData.Items.Count - 1)
            Next message_count
        End If
    End Sub
End Class
