
// CanCommunicationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CanCommunication.h"
#include "CanCommunicationDlg.h"
#include "afxdialogex.h"
#include "ccan.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CCanCommunicationDlg dialog



CCanCommunicationDlg::CCanCommunicationDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_CANCOMMUNICATION_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCanCommunicationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEV_NAME, m_edit_dev_name);
	DDX_Control(pDX, IDC_EDIT_CH_NO, m_edit_ch_no);
	DDX_Control(pDX, IDC_EDIT_CAN_BITRATE, m_edit_can_bitrate);
	DDX_Control(pDX, IDC_EDIT_CAN_TSEG1, m_edit_can_tseg1);
	DDX_Control(pDX, IDC_EDIT_CAN_SJW, m_edit_can_sjw);
	DDX_Control(pDX, IDC_EDIT_CAN_TSEG2, m_edit_can_tseg2);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_ARB_BITRATE, m_edit_can_fd_arb_bitrate);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_ARB_TSEG1, m_edit_can_fd_arb_tseg1);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_ARB_SJW, m_edit_can_fd_arb_sjw);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_ARB_TSEG2, m_edit_can_fd_arb_tseg2);
	DDX_Control(pDX, IDC_CHECK_MASK_MSG_TYPE_CAN, m_check_mask_msg_type_can);
	DDX_Control(pDX, IDC_CHECK_MASK_MSG_TYPE_CAN_FD, m_check_mask_msg_type_can_fd);
	DDX_Control(pDX, IDC_CHECK_MASK_MSG_TYPE_REMOTE_FRAME, m_check_mask_msg_type_remote_frame);
	DDX_Control(pDX, IDC_CHECK_MASK_MSG_TYPE_BITRATE_SWITCH, m_check_mask_msg_type_bitrate_switch);
	DDX_Control(pDX, IDC_CHECK_MASK_MSG_TYPE_STD_FORMAT, m_check_mask_msg_type_std_format);
	DDX_Control(pDX, IDC_CHECK_MASK_MSG_TYPE_EXT_FORMAT, m_check_mask_msg_type_ext_format);
	DDX_Control(pDX, IDC_EDIT_CAN_ID_MASK, m_edit_can_id_mask);
	DDX_Control(pDX, IDC_EDIT_CAN_ID_CODE, m_edit_can_id_code);
	DDX_Control(pDX, IDC_CHECK_TRANSMIT_MSG_TYPE_CAN, m_check_transmit_msg_type_can);
	DDX_Control(pDX, IDC_CHECK_TRANSMIT_MSG_TYPE_CAN_FD, m_check_transmit_msg_type_can_fd);
	DDX_Control(pDX, IDC_CHECK_TRANSMIT_MSG_TYPE_REMOTE_FRAME, m_check_transmit_msg_type_remote_frame);
	DDX_Control(pDX, IDC_CHECK_TRANSMIT_MSG_TYPE_BITRATE_SWITCH, m_check_transmit_msg_type_bitrate_switch);
	DDX_Control(pDX, IDC_CHECK_TRANSMIT_MSG_TYPE_EXT_FORMAT, m_check_transmit_msg_type_ext_format);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_CAN_ID, m_edit_transmit_can_id);
	DDX_Control(pDX, IDC_COMBO_TRANSMIT_DATA_LENGTH, m_combo_transmit_data_length);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_0, m_edit_transmit_data_0);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_1, m_edit_transmit_data_1);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_2, m_edit_transmit_data_2);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_3, m_edit_transmit_data_3);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_4, m_edit_transmit_data_4);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_5, m_edit_transmit_data_5);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_6, m_edit_transmit_data_6);
	DDX_Control(pDX, IDC_EDIT_TRANSMIT_DATA_7, m_edit_transmit_data_7);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_DATA_BITRATE, m_edit_can_fd_data_bitrate);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_DATA_TSEG1, m_edit_can_fd_data_tseg1);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_DATA_SJW, m_edit_can_fd_data_sjw);
	DDX_Control(pDX, IDC_EDIT_CAN_FD_DATA_TSEG2, m_edit_can_fd_data_tseg2);
	DDX_Control(pDX, IDC_LIST_CTRL_RECEIVE_DATA, m_list_ctrl_receive_data);
	DDX_Control(pDX, IDC_EDIT_CAN_CH_STATUS, m_edit_can_ch_status);
	DDX_Control(pDX, IDC_EDIT_CAN_CH_STATUS_TX_ERR_COUNT, m_edit_can_ch_status_tx_err_count);
	DDX_Control(pDX, IDC_EDIT_CAN_CH_STATUS_RX_ERR_COUNT, m_edit_can_ch_status_rx_err_count);
	DDX_Control(pDX, IDC_EDIT_RET, m_edit_ret);
}

BEGIN_MESSAGE_MAP(CCanCommunicationDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CAN_INIT, &CCanCommunicationDlg::OnBnClickedButtonCanInit)
	ON_BN_CLICKED(IDC_BUTTON_SET_CAN_TIMING, &CCanCommunicationDlg::OnBnClickedButtonSetCanTiming)
	ON_BN_CLICKED(IDC_BUTTON_GET_CAN_TIMING, &CCanCommunicationDlg::OnBnClickedButtonGetCanTiming)
	ON_BN_CLICKED(IDC_BUTTON_SET_CAN_FD_TIMING, &CCanCommunicationDlg::OnBnClickedButtonSetCanFdTiming)
	ON_BN_CLICKED(IDC_BUTTON_GET_CAN_FD_TIMING, &CCanCommunicationDlg::OnBnClickedButtonGetCanFdTiming)
	ON_BN_CLICKED(IDC_BUTTON_SET_CH_ACCEPTANCE, &CCanCommunicationDlg::OnBnClickedButtonSetChAcceptance)
	ON_BN_CLICKED(IDC_BUTTON_GET_CH_ACCEPTANCE, &CCanCommunicationDlg::OnBnClickedButtonGetChAcceptance)
	ON_BN_CLICKED(IDC_BUTTON_BUS_CONNECT, &CCanCommunicationDlg::OnBnClickedButtonBusConnect)
	ON_BN_CLICKED(IDC_BUTTON_BUS_DISCONNECT, &CCanCommunicationDlg::OnBnClickedButtonBusDisconnect)
	ON_BN_CLICKED(IDC_BUTTON_CAN_TRANSMIT, &CCanCommunicationDlg::OnBnClickedButtonCanTransmit)
	ON_BN_CLICKED(IDC_BUTTON_CAN_RECEIVE, &CCanCommunicationDlg::OnBnClickedButtonCanReceive)
	ON_BN_CLICKED(IDC_BUTTON_GET_CH_STATUS, &CCanCommunicationDlg::OnBnClickedButtonGetChStatus)
	ON_BN_CLICKED(IDC_BUTTON_CAN_EXIT, &CCanCommunicationDlg::OnBnClickedButtonCanExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CCanCommunicationDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CCanCommunicationDlg message handlers

BOOL CCanCommunicationDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	short		data_no;
	TCHAR		cstr[100];
	float		mul_DPI;
	LVCOLUMN	lv_colum;
	int			colum_no;
	TCHAR		receive_data_list_caption[][32] = { _T("No."), _T("TickCount"), _T("MessageType"), _T("CanId"), _T("DataLength"), _T("Data[Hex]"), _T("ErrorCode") };
	const int	column_num = sizeof(receive_data_list_caption) / sizeof(receive_data_list_caption[0]);

	//-----------------------------------
	// Setting for control array
	//-----------------------------------
	//-----------------
	// Controls for transmit data
	//-----------------
	m_edit_transmit_data[0]			= &m_edit_transmit_data_0;
	m_edit_transmit_data[1]			= &m_edit_transmit_data_1;
	m_edit_transmit_data[2]			= &m_edit_transmit_data_2;
	m_edit_transmit_data[3]			= &m_edit_transmit_data_3;
	m_edit_transmit_data[4]			= &m_edit_transmit_data_4;
	m_edit_transmit_data[5]			= &m_edit_transmit_data_5;
	m_edit_transmit_data[6]			= &m_edit_transmit_data_6;
	m_edit_transmit_data[7]			= &m_edit_transmit_data_7;
	//-----------------------------------
	// Initializing screen display
	//-----------------------------------
	//-----------------------------------
	// Initialization
	//-----------------------------------
	m_edit_dev_name.SetWindowTextW(_T("CAN000"));
	//-----------------------------------
	// Common setting value
	//-----------------------------------
	m_edit_ch_no.SetWindowTextW(_T("0"));
	//-----------------------------------
	// CAN communication setting values
	//-----------------------------------
	m_edit_can_bitrate.SetWindowTextW(_T("125000"));
	m_edit_can_tseg1.SetWindowTextW(_T("240"));
	m_edit_can_sjw.SetWindowTextW(_T("80"));
	m_edit_can_tseg2.SetWindowTextW(_T("80"));
	//-----------------------------------
	// CAN FD communication setting values
	//-----------------------------------
	//-----------------
	// Arbitration bitrate
	//-----------------
	m_edit_can_fd_arb_bitrate.SetWindowTextW(_T("125000"));
	m_edit_can_fd_arb_tseg1.SetWindowTextW(_T("240"));
	m_edit_can_fd_arb_sjw.SetWindowTextW(_T("80"));
	m_edit_can_fd_arb_tseg2.SetWindowTextW(_T("80"));
	//-----------------
	// Data bitrate
	//-----------------
	m_edit_can_fd_data_bitrate.SetWindowTextW(_T("1000000"));
	m_edit_can_fd_data_tseg1.SetWindowTextW(_T("30"));
	m_edit_can_fd_data_sjw.SetWindowTextW(_T("10"));
	m_edit_can_fd_data_tseg2.SetWindowTextW(_T("10"));
	//-----------------------------------
	// Acceptance target CAN ID
	//-----------------------------------
	//-----------------
	// Message type
	//-----------------
	m_check_mask_msg_type_can.SetCheck(0);
	m_check_mask_msg_type_can_fd.SetCheck(0);
	m_check_mask_msg_type_remote_frame.SetCheck(0);
	m_check_mask_msg_type_bitrate_switch.SetCheck(0);
	m_check_mask_msg_type_std_format.SetCheck(0);
	m_check_mask_msg_type_ext_format.SetCheck(0);
	//-----------------
	// CAN ID
	//-----------------
	m_edit_can_id_mask.SetWindowTextW(_T("1FFFFF00"));
	m_edit_can_id_code.SetWindowTextW(_T("100"));
	//-----------------------------------
	// Transmit
	//-----------------------------------
	//-----------------
	// Message type
	//-----------------
	m_check_transmit_msg_type_can.SetCheck(1);
	m_check_transmit_msg_type_can_fd.SetCheck(0);
	m_check_transmit_msg_type_remote_frame.SetCheck(0);
	m_check_transmit_msg_type_bitrate_switch.SetCheck(0);
	m_check_transmit_msg_type_ext_format.SetCheck(0);
	//-----------------
	// CAN ID
	//-----------------
	m_edit_transmit_can_id.SetWindowTextW(_T("100"));
	//-----------------
	// Data length
	//-----------------
	m_combo_transmit_data_length.AddString(_T("0"));
	m_combo_transmit_data_length.AddString(_T("1"));
	m_combo_transmit_data_length.AddString(_T("2"));
	m_combo_transmit_data_length.AddString(_T("3"));
	m_combo_transmit_data_length.AddString(_T("4"));
	m_combo_transmit_data_length.AddString(_T("5"));
	m_combo_transmit_data_length.AddString(_T("6"));
	m_combo_transmit_data_length.AddString(_T("7"));
	m_combo_transmit_data_length.AddString(_T("8"));
	m_combo_transmit_data_length.SelectString(-1, _T("8"));
	//-----------------
	// Data
	//-----------------
	for(data_no = 0; data_no < MAX_TRANSMIT_DATA; data_no++){
		wsprintf(cstr, _T("%x"), data_no);
		m_edit_transmit_data[data_no]->SetWindowText(cstr);
	}
	//-----------------------------------
	// Receive
	//-----------------------------------
	//-----------------
	// Style setting
	//-----------------
	m_list_ctrl_receive_data.SetExtendedStyle(m_list_ctrl_receive_data.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
	//-----------------
	// Get DPI
	//-----------------
	mul_DPI = GetDeviceCaps((HDC)::GetDC(NULL), LOGPIXELSX) / 96.0f;
	mul_DPI = (float)(floor(mul_DPI * 10) / 10);
	//-----------------
	// Valid flag
	//-----------------
	lv_colum.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	//-----------------
	// Column setting
	//-----------------
	for(colum_no = 0; colum_no < column_num; colum_no++){
		//-----------------
		// Subitem number
		//-----------------
		lv_colum.iSubItem	= colum_no;
		//-----------------
		// Heading text
		//-----------------
		lv_colum.pszText	= receive_data_list_caption[colum_no];
		//-----------------
		// Width setting
		//-----------------
		switch(colum_no){
		case 0:
			lv_colum.cx = (int)(30 * mul_DPI);
			break;
		case 1:
			lv_colum.cx = (int)(100 * mul_DPI);
			break;
		case 2:
			lv_colum.cx = (int)(90 * mul_DPI);
			break;
		case 3:
			lv_colum.cx = (int)(50 * mul_DPI);
			break;
		case 4:
			lv_colum.cx = (int)(70 * mul_DPI);
			break;
		case 5:
			lv_colum.cx = (int)(150 * mul_DPI);
			break;
		case 6:
			lv_colum.cx = (int)(70 * mul_DPI);
			break;
		default:
			break;
		}
		//-----------------
		// Add column
		//-----------------
		if(m_list_ctrl_receive_data.InsertColumn(colum_no, &lv_colum) == -1){
			break;
		}
	}
	//-----------------------------------
	// Status
	//-----------------------------------
	m_edit_can_ch_status.SetWindowTextW(_T(""));
	m_edit_can_ch_status_tx_err_count.SetWindowTextW(_T("0"));
	m_edit_can_ch_status_rx_err_count.SetWindowTextW(_T("0"));
	//-----------------------------------
	// Return value
	//-----------------------------------
	m_edit_ret.SetWindowTextW(_T(""));

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCanCommunicationDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCanCommunicationDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//=========================================================
// CanInit()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonCanInit()
{
	long	ret;
	char	device_name[100];
	TCHAR	sz_device_name[100];
	char	error_string[100];
	TCHAR	return_string[100];

	//-----------------------------
	// Get device name
	//-----------------------------
	m_edit_dev_name.GetWindowText(sz_device_name, _countof(sz_device_name));
	sprintf_s(device_name, "%S", sz_device_name);
	//-----------------------------
	// Initialization and get device ID
	//-----------------------------
	ret	= CanInit(device_name, &dev_id);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanSetCanTiming()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonSetCanTiming()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	bitrate;
	unsigned short	tseg1;
	unsigned short	sjw;
	unsigned short	tseg2;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store bitrate
	//-----------------------------
	m_edit_can_bitrate.GetWindowText(cstr, _countof(cstr));
	bitrate		= wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store time segment 1
	//-----------------------------
	m_edit_can_tseg1.GetWindowText(cstr, _countof(cstr));
	tseg1		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store SJW (Synchronization Jump Width)
	//-----------------------------
	m_edit_can_sjw.GetWindowText(cstr, _countof(cstr));
	sjw			= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store time segment 2
	//-----------------------------
	m_edit_can_tseg2.GetWindowText(cstr, _countof(cstr));
	tseg2		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// CAN communication timing setting
	//-----------------------------
	ret	= CanSetCanTiming(dev_id, channel_no, bitrate, tseg1, sjw, tseg2);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanGetCanTiming()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonGetCanTiming()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	bitrate;
	unsigned short	tseg1;
	unsigned short	sjw;
	unsigned short	tseg2;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Get CAN communication timing setting
	//-----------------------------
	ret	= CanGetCanTiming(dev_id, channel_no, &bitrate, &tseg1, &sjw, &tseg2);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
	//-----------------------------
	// Update bitrate display
	//-----------------------------
	wsprintf(cstr, _T("%u"), bitrate);
	m_edit_can_bitrate.SetWindowText(cstr);
	//-----------------------------
	// Update time segment 1 display
	//-----------------------------
	wsprintf(cstr, _T("%u"), tseg1);
	m_edit_can_tseg1.SetWindowText(cstr);
	//-----------------------------
	// Update SJW (Synchronization Jump Width) display
	//-----------------------------
	wsprintf(cstr, _T("%u"), sjw);
	m_edit_can_sjw.SetWindowText(cstr);
	//-----------------------------
	// Update time segment 2 display
	//-----------------------------
	wsprintf(cstr, _T("%u"), tseg2);
	m_edit_can_tseg2.SetWindowText(cstr);
}

//=========================================================
// CanSetCanFdTiming()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonSetCanFdTiming()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	arb_bitrate, data_bitrate;
	unsigned short	arb_tseg1, data_tseg1;
	unsigned short	arb_sjw, data_sjw;
	unsigned short	arb_tseg2, data_tseg2;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store arbitration bitrate
	//-----------------------------
	m_edit_can_fd_arb_bitrate.GetWindowText(cstr, _countof(cstr));
	arb_bitrate		= wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store arbitration time segment 1
	//-----------------------------
	m_edit_can_fd_arb_tseg1.GetWindowText(cstr, _countof(cstr));
	arb_tseg1		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store arbitration SJW (Synchronization Jump Width)
	//-----------------------------
	m_edit_can_fd_arb_sjw.GetWindowText(cstr, _countof(cstr));
	arb_sjw			= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store arbitration time segment 2
	//-----------------------------
	m_edit_can_fd_arb_tseg2.GetWindowText(cstr, _countof(cstr));
	arb_tseg2		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store data bitrate
	//-----------------------------
	m_edit_can_fd_data_bitrate.GetWindowText(cstr, _countof(cstr));
	data_bitrate	= wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store data time segment 1
	//-----------------------------
	m_edit_can_fd_data_tseg1.GetWindowText(cstr, _countof(cstr));
	data_tseg1		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store data SJW (Synchronization Jump Width)
	//-----------------------------
	m_edit_can_fd_data_sjw.GetWindowText(cstr, _countof(cstr));
	data_sjw		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store data time segment 2
	//-----------------------------
	m_edit_can_fd_data_tseg2.GetWindowText(cstr, _countof(cstr));
	data_tseg2		= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// CAN FD communication timing setting
	//-----------------------------
	ret	= CanSetCanFdTiming(dev_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);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanGetCanFdTiming()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonGetCanFdTiming()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	arb_bitrate, data_bitrate;
	unsigned short	arb_tseg1, data_tseg1;
	unsigned short	arb_sjw, data_sjw;
	unsigned short	arb_tseg2, data_tseg2;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Get CAN FD communication timing setting
	//-----------------------------
	ret	= CanGetCanFdTiming(dev_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);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
	//-----------------------------
	// Update arbitration bitrate display
	//-----------------------------
	wsprintf(cstr, _T("%u"), arb_bitrate);
	m_edit_can_fd_arb_bitrate.SetWindowText(cstr);
	//-----------------------------
	// Update arbitration time segment 1 display
	//-----------------------------
	wsprintf(cstr, _T("%u"), arb_tseg1);
	m_edit_can_fd_arb_tseg1.SetWindowText(cstr);
	//-----------------------------
	// Update arbitration SJW (Synchronization Jump Width) display
	//-----------------------------
	wsprintf(cstr, _T("%u"), arb_sjw);
	m_edit_can_fd_arb_sjw.SetWindowText(cstr);
	//-----------------------------
	// Update arbitration time segment 2 display
	//-----------------------------
	wsprintf(cstr, _T("%u"), arb_tseg2);
	m_edit_can_fd_arb_tseg2.SetWindowText(cstr);
	//-----------------------------
	// Update data bitrate display
	//-----------------------------
	wsprintf(cstr, _T("%u"), data_bitrate);
	m_edit_can_fd_data_bitrate.SetWindowText(cstr);
	//-----------------------------
	// Update data time segment 1 display
	//-----------------------------
	wsprintf(cstr, _T("%u"), data_tseg1);
	m_edit_can_fd_data_tseg1.SetWindowText(cstr);
	//-----------------------------
	// Update data SJW (Synchronization Jump Width) display
	//-----------------------------
	wsprintf(cstr, _T("%u"), data_sjw);
	m_edit_can_fd_data_sjw.SetWindowText(cstr);
	//-----------------------------
	// Update data time segment 2 display
	//-----------------------------
	wsprintf(cstr, _T("%u"), data_tseg2);
	m_edit_can_fd_data_tseg2.SetWindowText(cstr);
}

//=========================================================
// CanSetChannelAcceptance()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonSetChAcceptance()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	message_type_mask;
	unsigned long	can_id_mask;
	unsigned long	can_id_code;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store receive message type mask
	//-----------------------------
	//-----------------
	// Initialization
	//-----------------
	message_type_mask	= CCAN_MSG_TYPE_DISABLE;
	//-----------------
	// Message type CAN
	//-----------------
	if(m_check_mask_msg_type_can.GetCheck()){
		message_type_mask |= CCAN_MSG_TYPE_CAN;
	}
	//-----------------
	// Message type CAN FD
	//-----------------
	if(m_check_mask_msg_type_can_fd.GetCheck()){
		message_type_mask |= CCAN_MSG_TYPE_CAN_FD;
	}
	//-----------------
	// Message type Remote frame
	//-----------------
	if(m_check_mask_msg_type_remote_frame.GetCheck()){
		message_type_mask |= CCAN_MSG_TYPE_REMOTE_FRAME;
	}
	//-----------------
	// Message type Bitrate switch
	//-----------------
	if(m_check_mask_msg_type_bitrate_switch.GetCheck()){
		message_type_mask |= CCAN_MSG_TYPE_BITRATE_SWITCH;
	}
	//-----------------
	// Message type Standard format
	//-----------------
	if (m_check_mask_msg_type_std_format.GetCheck()) {
		message_type_mask |= CCAN_MSG_TYPE_STANDARD_FORMAT;
	}
	//-----------------
	// Message type Extended format
	//-----------------
	if(m_check_mask_msg_type_ext_format.GetCheck()){
		message_type_mask |= CCAN_MSG_TYPE_EXTENDED_FORMAT;
	}
	//-----------------------------
	// Store CAN ID mask
	//-----------------------------
	m_edit_can_id_mask.GetWindowText(cstr, _countof(cstr));
	can_id_mask = wcstoul(cstr, NULL, 16);
	//-----------------------------
	// Store CAN ID code
	//-----------------------------
	m_edit_can_id_code.GetWindowText(cstr, _countof(cstr));
	can_id_code = wcstoul(cstr, NULL, 16);
	//-----------------------------
	// Receive acceptance setting
	//-----------------------------
	ret	= CanSetChannelAcceptance(dev_id, channel_no, message_type_mask, can_id_mask, can_id_code);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanGetChannelAcceptance()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonGetChAcceptance()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	message_type_mask;
	unsigned long	can_id_mask;
	unsigned long	can_id_code;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Get receive acceptance setting
	//-----------------------------
	ret	= CanGetChannelAcceptance(dev_id, channel_no, &message_type_mask, &can_id_mask, &can_id_code);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
	//-----------------------------
	// Message type CAN
	//-----------------------------
	if(message_type_mask & CCAN_MSG_TYPE_CAN){
		m_check_mask_msg_type_can.SetCheck(1);
	}else{
		m_check_mask_msg_type_can.SetCheck(0);
	}
	//-----------------------------
	// Message type CAN FD
	//-----------------------------
	if(message_type_mask & CCAN_MSG_TYPE_CAN_FD){
		m_check_mask_msg_type_can_fd.SetCheck(1);
	}else{
		m_check_mask_msg_type_can_fd.SetCheck(0);
	}
	//-----------------------------
	// Message type Remote frame
	//-----------------------------
	if(message_type_mask & CCAN_MSG_TYPE_REMOTE_FRAME){
		m_check_mask_msg_type_remote_frame.SetCheck(1);
	}else{
		m_check_mask_msg_type_remote_frame.SetCheck(0);
	}
	//-----------------------------
	// Message type Bitrate switch
	//-----------------------------
	if(message_type_mask & CCAN_MSG_TYPE_BITRATE_SWITCH){
		m_check_mask_msg_type_bitrate_switch.SetCheck(1);
	}else{
		m_check_mask_msg_type_bitrate_switch.SetCheck(0);
	}
	//-----------------------------
	// Message type Standard format
	//-----------------------------
	if (message_type_mask & CCAN_MSG_TYPE_STANDARD_FORMAT) {
		m_check_mask_msg_type_std_format.SetCheck(1);
	}
	else {
		m_check_mask_msg_type_std_format.SetCheck(0);
	}
	//-----------------------------
	// Message type Extended format
	//-----------------------------
	if(message_type_mask & CCAN_MSG_TYPE_EXTENDED_FORMAT){
		m_check_mask_msg_type_ext_format.SetCheck(1);
	}else{
		m_check_mask_msg_type_ext_format.SetCheck(0);
	}
	//-----------------------------
	// Update CAN ID mask display
	//-----------------------------
	wsprintf(cstr, _T("%X"), can_id_mask);
	m_edit_can_id_mask.SetWindowText(cstr);
	//-----------------------------
	// Update CAN ID code display
	//-----------------------------
	wsprintf(cstr, _T("%X"), can_id_code);
	m_edit_can_id_code.SetWindowText(cstr);
}

//=========================================================
// CanBusConnect()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonBusConnect()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Bus connection
	//-----------------------------
	ret	= CanBusConnect(dev_id, channel_no);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanBusDisconnect()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonBusDisconnect()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Bus disconnection
	//-----------------------------
	ret	= CanBusDisconnect(dev_id, channel_no);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanTransmit()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonCanTransmit()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned long	message_num;
	unsigned long	message_type;
	unsigned long	can_id;
	unsigned short	data_length, data_no;
	unsigned char	transmit_data[1][MAX_TRANSMIT_DATA];

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store transmit message type
	//-----------------------------
	//-----------------
	// Initialization
	//-----------------
	message_type = 0;
	//-----------------
	// Message type CAN
	//-----------------
	if(m_check_transmit_msg_type_can.GetCheck()){
		message_type |= CCAN_MSG_TYPE_CAN;
	}
	//-----------------
	// Message type CAN FD
	//-----------------
	if(m_check_transmit_msg_type_can_fd.GetCheck()){
		message_type |= CCAN_MSG_TYPE_CAN_FD;
	}
	//-----------------
	// Message type Remote frame
	//-----------------
	if(m_check_transmit_msg_type_remote_frame.GetCheck()){
		message_type |= CCAN_MSG_TYPE_REMOTE_FRAME;
	}
	//-----------------
	// Message type Bitrate switch
	//-----------------
	if(m_check_transmit_msg_type_bitrate_switch.GetCheck()){
		message_type |= CCAN_MSG_TYPE_BITRATE_SWITCH;
	}
	//-----------------
	// Message type Extended format
	//-----------------
	if(m_check_transmit_msg_type_ext_format.GetCheck()){
		message_type |= CCAN_MSG_TYPE_EXTENDED_FORMAT;
	}
	//-----------------------------
	// Store CAN ID
	//-----------------------------
	m_edit_transmit_can_id.GetWindowText(cstr, _countof(cstr));
	can_id		= wcstoul(cstr, NULL, 16);
	//-----------------------------
	// Number of transmit messages
	//-----------------------------
	message_num	= 1;
	//-----------------------------
	// Store transmit message data length
	//-----------------------------
	m_combo_transmit_data_length.GetWindowText(cstr, _countof(cstr));
	data_length	= (short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Store transmit message data
	//-----------------------------
	for(data_no = 0; data_no < data_length; data_no++){
		m_edit_transmit_data[data_no]->GetWindowText(cstr, _countof(cstr));
		transmit_data[0][data_no]	= (unsigned char)wcstoul(cstr, NULL, 16);
	}
	//-----------------------------
	// CAN / CAN FD Transmit
	//-----------------------------
	ret	= CanTransmit(dev_id, channel_no, message_num, &message_type, &can_id, &data_length, &transmit_data[0][0]);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
}

//=========================================================
// CanReceive()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonCanReceive()
{
	long				ret;
	char				error_string[100];
	TCHAR				return_string[100];
	TCHAR				cstr[100];
	CString				str;
	LVITEM				lv_item;
	unsigned short		channel_no;
	unsigned long		message_num;
	unsigned long		message_type[MAX_RECEIVE_MESSAGE];
	unsigned long		can_id[MAX_RECEIVE_MESSAGE];
	unsigned short		data_length[MAX_RECEIVE_MESSAGE];
	unsigned char		receive_data[MAX_RECEIVE_MESSAGE][MAX_RECEIVE_DATA];
	unsigned long		error_code[MAX_RECEIVE_MESSAGE];
	unsigned long long	tick_count[MAX_RECEIVE_MESSAGE];
	unsigned long		message_count, data_count;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Number of receive messages
	//-----------------------------
	message_num	= MAX_RECEIVE_MESSAGE;
	//-----------------------------
	// CAN / CAN FD Receive
	//-----------------------------
	ret	= CanReceive(dev_id, channel_no, &message_num, message_type, can_id, data_length, &receive_data[0][0], error_code, tick_count);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
	//-----------------------------
	// Clear receive message list
	//-----------------------------
	m_list_ctrl_receive_data.DeleteAllItems();
	//-----------------------------
	// List view item member setting
	//-----------------------------
	lv_item.mask = LVIF_TEXT;
	//-----------------------------
	// Add list for each received message
	//-----------------------------
	for(message_count = 0; message_count < message_num; message_count++){
		//-----------------------------
		// Add item: No.
		//-----------------------------
		lv_item.iItem = message_count;
		str.Format(_T("%d"), (message_count));
		lv_item.iSubItem = 0;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.InsertItem(&lv_item);
		//-----------------------------
		// Add item: TickCount
		//-----------------------------
		str.Format(_T("%llu"), tick_count[message_count]);
		lv_item.iSubItem = 1;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.SetItem(&lv_item);
		//-----------------------------
		// Add item: MessageType
		//-----------------------------
		str.Format(_T("0x%lX"), message_type[message_count]);
		lv_item.iSubItem = 2;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.SetItem(&lv_item);
		//-----------------------------
		// Add item: CanId
		//-----------------------------
		str.Format(_T("0x%lX"), can_id[message_count]);
		lv_item.iSubItem = 3;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.SetItem(&lv_item);
		//-----------------------------
		// Add item: DataLength
		//-----------------------------
		str.Format(_T("%hu"), data_length[message_count]);
		lv_item.iSubItem = 4;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.SetItem(&lv_item);
		//-----------------------------
		// Add item: Data
		//-----------------------------
		str.Empty();
		for(data_count = 0; data_count < data_length[message_count]; data_count++){
			str.AppendFormat(_T("%02hhX"), receive_data[message_count][data_count]);
			if(data_count < (unsigned long)(data_length[message_count] - 1)){
				str.Append(_T(", "));
			}
		}
		lv_item.iSubItem = 5;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.SetItem(&lv_item);
		//-----------------------------
		// Add item: ErrorCode
		//-----------------------------
		str.Format(_T("0x%lX"), error_code[message_count]);
		lv_item.iSubItem = 6;
		lv_item.pszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(str));
		m_list_ctrl_receive_data.SetItem(&lv_item);
	}
}

//=========================================================
// CanGetChannelStatus()
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonGetChStatus()
{
	long			ret;
	char			error_string[100];
	TCHAR			return_string[100];
	TCHAR			cstr[100];
	unsigned short	channel_no;
	unsigned short  channel_status;
	unsigned short	tx_error_count;
	unsigned short	rx_error_count;

	//-----------------------------
	// Store channel number
	//-----------------------------
	m_edit_ch_no.GetWindowText(cstr, _countof(cstr));
	channel_no	= (unsigned short)wcstoul(cstr, NULL, 10);
	//-----------------------------
	// Get channel status
	//-----------------------------
	ret	= CanGetChannelStatus(dev_id, channel_no, &channel_status, &tx_error_count, &rx_error_count);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
	if(ret != CAN_ERR_SUCCESS){
		return;
	}
	//-----------------------------
	// Update channel status display
	//-----------------------------
	if(channel_status == CCAN_CH_STATUS_ERR_ACTIVE){
		m_edit_can_ch_status.SetWindowTextW(_T("Error Active"));
	}
	if(channel_status == CCAN_CH_STATUS_ERR_PASSIVE){
		m_edit_can_ch_status.SetWindowTextW(_T("Error Passive"));
	}
	if(channel_status == CCAN_CH_STATUS_BUS_OFF){
		m_edit_can_ch_status.SetWindowTextW(_T("Bus OFF"));
	}
	//-----------------------------
	// Update transmit error count display
	//-----------------------------
	wsprintf(cstr, _T("%d"), tx_error_count);
	m_edit_can_ch_status_tx_err_count.SetWindowText(cstr);
	//-----------------------------
	// Update receive error count display
	//-----------------------------
	wsprintf(cstr, _T("%d"), rx_error_count);
	m_edit_can_ch_status_rx_err_count.SetWindowText(cstr);
}

//=========================================================
// CanExit
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonCanExit()
{
	long	ret;
	char	error_string[100];
	TCHAR	return_string[100];

	//-----------------------------
	// Exit process
	//-----------------------------
	ret	= CanExit(dev_id);
	//-----------------------------
	// Error process
	//-----------------------------
	CanGetErrorString(ret, error_string);
	wsprintf(return_string, _T("ret = %d : %S"), ret, error_string);
	m_edit_ret.SetWindowText(return_string);
}

//=========================================================
// Application termination
//=========================================================
void CCanCommunicationDlg::OnBnClickedButtonExit()
{
	//-----------------------------
	// Close dialog
	//-----------------------------
	CDialog::OnOK();
}
