
// zlgcanDlg.cpp : 实现文件
//

#include "stdafx.h"
#include <process.h>
#include "zlgcanDemo.h"
#include "zlgcanDlg.h"
#include "zlgcan/zlgcan.h"
#include "utility.h"
#include "zcloudDlg.h"

using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define TMP_BUFFER_LEN      1000

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// Cusbcanfdx00udemoDlg 对话框

CZlgcanDlg::CZlgcanDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CZlgcanDlg::IDD, pParent)
	, device_type_index_(0)
	, device_index_(0)
	, channel_index_(0)
	, work_mode_index_(0)
	, abit_baud_index_(0)
	, dbit_baud_index_(0)
	, custom_baud_enable_(FALSE)
	, resistance_enable_(TRUE)
	, frame_type_index_(0)
	, protocol_index_(1)
	, canfd_exp_index_(1)
	, send_type_index_(0)
	, acc_code_(_T("00000000"))
	, acc_mask_(_T("FFFFFFFF"))
	, id_(_T("555"))
	, datas_(_T("55 55 55 55 55 55 55 55"))
	, custom_baudrate_(_T(""))
	, filter_mode_(0)
    , net_mode_index_(0)
    , baud_index_(0)
    , send_count_once_(1)
    , frm_delay_time_(1000)
    , frm_delay_flag_(FALSE)
    , auto_send_index_(0)
    , auto_send_period_(1000)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	device_opened_ = FALSE;
	start_ = FALSE;
    property_ = NULL;
    support_delay_send_ = FALSE;
    support_delay_send_mode_ = FALSE;
    support_get_send_mode_ = FALSE;
}

void CZlgcanDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_CBIndex(pDX, IDC_COMBO_DEVICE, device_type_index_);
	DDX_CBIndex(pDX, IDC_COMBO_DEVICE_INDEX, device_index_);
	DDX_CBIndex(pDX, IDC_COMBO_CHANNEL_INDEX, channel_index_);
	DDX_CBIndex(pDX, IDC_COMBO_DEVICE_INDEX2, work_mode_index_);
	DDX_CBIndex(pDX, IDC_COMBO_ABIT, abit_baud_index_);
	DDX_CBIndex(pDX, IDC_COMBO_ABIT2, dbit_baud_index_);
	DDX_CBIndex(pDX, IDC_COMBO_BAUD, baud_index_);
	DDX_Check(pDX, IDC_CHECK_CUSTOM_BAUDRATE, custom_baud_enable_);
	DDX_Check(pDX, IDC_CHECK_RESISTANCE, resistance_enable_);
	DDX_CBIndex(pDX, IDC_COMBO_FRAME_TYPE, frame_type_index_);
	DDX_CBIndex(pDX, IDC_COMBO_PROTOCOL, protocol_index_);
	DDX_CBIndex(pDX, IDC_COMBO_CANFD_EXP, canfd_exp_index_);
	DDX_CBIndex(pDX, IDC_COMBO_SEND_TYPE, send_type_index_);
	DDX_Control(pDX, IDC_LIST1, data_recv_list_);
	DDX_Text(pDX, IDC_EDIT_ACC_CODE, acc_code_);
	DDV_MaxChars(pDX, acc_code_, 8);
	DDX_Text(pDX, IDC_EDIT_ACC_MASK, acc_mask_);
	DDV_MaxChars(pDX, acc_mask_, 8);
	DDX_Text(pDX, IDC_EDIT_FILTER_START2, id_);
	DDV_MaxChars(pDX, id_, 8);
	DDX_Text(pDX, IDC_EDIT_FILTER_START3, datas_);
	DDX_Control(pDX, IDC_COMBO_DEVICE, ctrl_device_type_);
	DDX_Control(pDX, IDC_COMBO_DEVICE_INDEX, ctrl_device_index_);
	DDX_Control(pDX, IDC_COMBO_CHANNEL_INDEX, ctrl_channel_index_);
	DDX_Text(pDX, IDC_EDIT_CUSTOM_BAUDRATE, custom_baudrate_);
	DDX_Control(pDX, IDC_BUTTON_OPEN, ctrl_open_device_);
	DDX_Control(pDX, IDC_BUTTON_INITCAN, ctrl_int_can_);
	DDX_Control(pDX, IDC_BUTTON_STARTCAN, ctrl_start_can_);
	DDX_Control(pDX, IDC_BUTTON_CLOSE, ctrl_close_device_);
	DDX_CBIndex(pDX, IDC_COMBO_FILTER_MODE, filter_mode_); 
	DDX_Control(pDX, IDC_COMBO_ABIT, m_comboAbit);
	DDX_Control(pDX, IDC_COMBO_ABIT2, m_comboDbit);
	DDX_Text(pDX, IDC_EDIT_SEND_ONCE, send_count_once_);  
	DDV_MinMaxInt(pDX, send_count_once_, 1, 10000);  
}

BEGIN_MESSAGE_MAP(CZlgcanDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CZlgcanDlg::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_INITCAN, &CZlgcanDlg::OnBnClickedButtonInitcan)
	ON_BN_CLICKED(IDC_BUTTON_STARTCAN, &CZlgcanDlg::OnBnClickedButtonStartcan)
	ON_BN_CLICKED(IDC_BUTTON_RESET, &CZlgcanDlg::OnBnClickedButtonReset)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CZlgcanDlg::OnBnClickedButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_SEND, &CZlgcanDlg::OnBnClickedButtonSend)
	ON_CBN_SELCHANGE(IDC_COMBO_DEVICE, &CZlgcanDlg::OnCbnSelchangeComboDevice)
	ON_CBN_SELCHANGE(IDC_COMBO_NET_MODE, &CZlgcanDlg::OnCbnSelchangeComboNetMode)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CZlgcanDlg::OnBnClickedButtonClear)
    ON_BN_CLICKED(IDC_BUTTON_GET_TX_AVAILABLE, &CZlgcanDlg::OnBnClickedButtonGetTxAvailable)
    ON_BN_CLICKED(IDC_BUTTON_CLEAR_TX_QUEUE, &CZlgcanDlg::OnBnClickedButtonClearTxQueue)
    ON_BN_CLICKED(IDC_CHECK_DELAY_SEND_MODE, &CZlgcanDlg::OnBnClickedCheckDelaySendMode)
    ON_BN_CLICKED(IDC_BUTTON_GET_SEND_MODE, &CZlgcanDlg::OnBnClickedButtonGetSendMode)
    ON_BN_CLICKED(IDC_BUTTON_AUTOSEND_START, &CZlgcanDlg::OnBnClickedButtonAutosendStart)
    ON_BN_CLICKED(IDC_BUTTON_AUTOSEND_STOP, &CZlgcanDlg::OnBnClickedButtonAutosendStop)
    ON_BN_CLICKED(IDC_BUTTON_STOP_SINGLE_AUTOSEND, &CZlgcanDlg::OnBnClickedButtonStopSingleAutosend)
    ON_BN_CLICKED(IDC_BUTTON_ADD_AUTOSEND, &CZlgcanDlg::OnBnClickedButtonAddAutosend)
    ON_BN_CLICKED(IDC_BUTTON_GET_DEV_AUTOSEND, &CZlgcanDlg::OnBnClickedButtonGetDevAutosend)
	ON_CBN_SELCHANGE(IDC_COMBO_DEVICE_INDEX, &CZlgcanDlg::OnCbnSelchangeComboDeviceIndex)
END_MESSAGE_MAP()


// Cusbcanfdx00udemoDlg 消息处理程序

BOOL CZlgcanDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	InitCombobox(IDC_COMBO_DEVICE_INDEX, 0, 32, 0);
	OnCbnSelchangeComboDevice();
	 
	//设置标题栏
	SetWindowText(L"ZQWL-USBCAN");

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CZlgcanDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CZlgcanDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

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

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CZlgcanDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

typedef struct _DeviceInfo
{
	UINT device_type;  //设备类型
	UINT channel_count;//设备的通道个数
}DeviceInfo;
/*
列表数据需要和对话框中设备列表数据一一对应
*/
static const DeviceInfo kDeviceType[] = {
    /*
	 new DeviceInfo(Define.ZQWL_UCANFD_100C, 1),
	        new DeviceInfo(Define.ZQWL_UCAN_101C, 1),
	        new DeviceInfo(Define.ZQWL_UCANFD_100K, 1),
	        new DeviceInfo(Define.ZQWL_UCAN_101K, 1),
	        new DeviceInfo(Define.ZQWL_UCANFD_100E, 1),
	        new DeviceInfo(Define.ZQWL_UCAN_101E, 1),
            new DeviceInfo(Define.ZQWL_UCANFD_200U, 2),
            new DeviceInfo(Define.ZQWL_UCAN_201U, 2),
            new DeviceInfo(Define.ZQWL_UCANFD_200C, 2),
            new DeviceInfo(Define.ZQWL_UCAN_201C, 1),
            new DeviceInfo(Define.ZCAN_UCAN_401U, 4),
            new DeviceInfo(Define.ZCAN_UCANFD_400U, 4),
	*/
	/*
	{ZCAN_USBCAN1, 1},
	{ZCAN_USBCAN2, 2},
	{ZCAN_PCI9820I,2},
	{ZCAN_USBCAN_E_U, 1},
	{ZCAN_USBCAN_2E_U, 2},
	{ZCAN_USBCAN_4E_U, 4},
	{ZCAN_PCIE_CANFD_100U, 1},
	{ZCAN_PCIE_CANFD_200U, 2},
	{ZCAN_PCIE_CANFD_400U_EX, 4 },
	{ZCAN_USBCANFD_200U, 2},
	{ZCAN_USBCANFD_100U, 1},
	{ZCAN_USBCANFD_MINI, 1},
	{ZCAN_CANETTCP, 1},
	{ZCAN_CANETUDP, 1},
	{ZCAN_WIFICAN_TCP, 1},
	{ZCAN_WIFICAN_UDP, 1},
	{ZCAN_CLOUD, 1},
	{ZCAN_CANFDWIFI_TCP, 1},
	{ZCAN_CANFDWIFI_UDP, 1},
	{ZCAN_CANFDNET_TCP, 2},
	{ZCAN_CANFDNET_UDP, 2},
	{ZCAN_CANFDNET_400U_TCP, 4},
	{ZCAN_CANFDNET_400U_UDP, 4},
	{ZCAN_UCAN_401U, 4},
	{ZCAN_UCANFD_400U, 4},
	*/

	/**/
    {ZCAN_USBCANFD_100U, 1},
    {ZCAN_USBCAN1, 1},
	{ZCAN_USBCANFD_100U,1},
    {ZCAN_USBCAN1, 1},
    {ZCAN_USBCANFD_100U, 1},
    {ZCAN_USBCAN1, 1},
	{ZCAN_USBCANFD_200U, 2},
	{ZCAN_USBCAN2, 2 },
	{ZCAN_USBCANFD_200U, 2},
	{ZCAN_USBCAN2, 2},
	{ZCAN_UCAN_401U, 4},
    {ZCAN_UCANFD_400U, 4},
	
};

void CZlgcanDlg::OnBnClickedButtonOpen()
{
	UpdateData(TRUE);
    if (kDeviceType[device_type_index_].device_type == ZCAN_CLOUD)
    {
        zcloudDlg dlg;
        if (IDCANCEL == dlg.DoModal())
        {
            return;
        }
        device_index_ = dlg.GetDeviceIndex();
    }
	//ZCAN_OpenDevice(0, device_index_, 0);
    device_handle_ = ZCAN_OpenDevice(kDeviceType[device_type_index_].device_type, device_index_, 0);
	if (INVALID_DEVICE_HANDLE == device_handle_)
	{
		AddData(_T("打开设备失败!"));
		return;
	}
	device_opened_ = TRUE;
	EnableCtrl(TRUE);
    AddData(_T("打开设备成功!"));
}
//USBCANFD
static const UINT kAbitTimingUSB[] = {
	1000000,//1Mbps
    800000,//800kbps
    500000,//500kbps
    250000,//250kbps
    125000,//125kbps
    100000,//100kbps
    50000 //50kbps
};
static const UINT kDbitTimingUSB[] = {
    5000000,//5Mbps
    4000000,//4Mbps
    2000000,//2Mbps
    1000000, //1Mbps
	800000,//800kbps
	500000,//500kbps
	250000,//250kbps
	125000,//125kbps
	100000,//100kbps
	50000 //50kbps
};

//PCIECANFD brp=1
static const UINT kAbitTimingPCIE[] = {
    1000000, //1M(80%)
    800000, //800K(80%)
    500000, //500K(80%)
    250000, //250K(80%)
    125000  //125K(80%)
};
static const UINT kDbitTimingPCIE[] = {
    8000000, //8Mbps(80%)
    5000000, //5Mbps(75%)
    5000000, //5Mbps(87.5%)
    4000000, //4Mbps(80%)
    2000000, //2Mbps(80%)
    1000000,  //1Mbps(80%)
	800000,//800kbps
	500000,//500kbps
	250000,//250kbps
	125000,//125kbps
	100000,//100kbps
	50000 //50kbps
};

static const unsigned kBaudrate[] = {
    1000000,
    800000,
    500000,
    250000,
    125000,
    100000,
    50000,
    20000,
    10000,
    5000
};

void CZlgcanDlg::OnBnClickedButtonInitcan()
{
	if (!device_opened_)
	{
		AddData(_T("设备还没打开!"));
		return;
	}
	ZCAN_CHANNEL_INIT_CONFIG config;
	memset(&config, 0, sizeof(config));
    UpdateData(TRUE);
    UINT type = kDeviceType[device_type_index_].device_type;
    const BOOL cloudDevice = type==ZCAN_CLOUD;
    const BOOL netcanfd = IsNetCANFD(type);
    const BOOL netcan = IsNetCAN(type);
    const BOOL netDevice = (netcan || netcanfd);
    const BOOL tcpDevice = IsNetTCP(type);
    const BOOL server = net_mode_index_ == 0;

	const BOOL usbcanfd = type == ZCAN_USBCANFD_100U ||
		type == ZCAN_USBCANFD_200U || type == ZCAN_USBCANFD_MINI || type == ZCAN_UCANFD_400U;

    //const BOOL usbcanfd = type== ZQWL_UCANFD_100C ||
    //    type== ZQWL_UCANFD_100K || type== ZQWL_UCANFD_100E || type == ZQWL_UCANFD_200U || type == ZQWL_UCANFD_200C || type == ZCAN_UCANFD_400U;
    const BOOL canfdDevice = usbcanfd ;


    if (cloudDevice)
    {
    }
    else if (netDevice)
    {
        
    }
    else
    {
        //设置波特率
        if (custom_baud_enable_)
        {	
            if (!SetCustomBaudrate())
            {
                AddData(_T("设置自定义波特率失败!"));
                return;
            }
        }
        else
        {
            if (!canfdDevice && !SetBaudrate())
            {
                AddData(_T("设置波特率失败!"));
                return;
            }
        }
		/*
        if (usbcanfd) {

            char path[50] = {0};
            char value[100] = {0};
            sprintf_s(path, "%d/canfd_standard", channel_index_);
            sprintf_s(value, "%d", 0);
			ZCAN_SetValue(device_handle_, path, value);
        }*/
        if (usbcanfd)
        {   
			//设置波特率
			if (custom_baud_enable_)
			{
				if (!SetCustomBaudrate())
				{
					AddData(_T("设置自定义波特率失败!"));
					return;
				}
			}
			else
			{
				if (!SetCanfdBaudrate())
				{
					AddData(_T("设置波特率失败!"));
					return;
				}
			}
            config.can_type = TYPE_CANFD;
            config.canfd.mode = work_mode_index_;
            config.canfd.filter = filter_mode_;
            config.canfd.acc_code = _tcstoul(acc_code_, 0, 16);
            config.canfd.acc_mask = _tcstoul(acc_mask_, 0, 16);

        }
        else
        {
            config.can_type = TYPE_CAN;
            config.can.mode = work_mode_index_;
            config.can.filter = filter_mode_;
            config.can.acc_code = _tcstoul(acc_code_, 0, 16);
            config.can.acc_mask = _tcstoul(acc_mask_, 0, 16);
        }
    }
	//ZCAN_DEVICE_INFO zcan_device_info;
	//ZCAN_GetDeviceInf(device_handle_,zcan_device_info);

 

	channel_handle_ =  ZCAN_InitCAN(device_handle_, channel_index_, config)[0];
	if (INVALID_CHANNEL_HANDLE == channel_handle_.handle)
	{
		AddData(_T("初始化CAN失败!"));
		return;
	}
    if (usbcanfd)
    {
        if (resistance_enable_ && !SetResistanceEnable())
        {
            AddData(_T("设置终端电阻失败!"));
            return;
        }
    }
	ctrl_int_can_.EnableWindow(FALSE);
    AddData(_T("初始化成功!"));
}

void CZlgcanDlg::OnBnClickedButtonStartcan()
{
	//ZCAN_StartCAN(channel_handle_)
	if (ZCAN_StartCAN(channel_handle_) != STATUS_OK)
	{
		AddData(_T("启动CAN失败!"));
		return;
	}
	ctrl_start_can_.EnableWindow(FALSE);
	start_ = TRUE;
	_beginthreadex(NULL, 0, OnDataRecv, this, 0, NULL);
	AddData(_T("启动CAN成功!"));
}

void CZlgcanDlg::OnBnClickedButtonReset()
{
	if (ZCAN_ResetCAN(channel_handle_) != STATUS_OK)
	{
		AddData(_T("复位失败!"));
		return;
	}
	ctrl_start_can_.EnableWindow(TRUE);
	start_ = FALSE;
    AddData(_T("复位成功!"));
}

void CZlgcanDlg::OnBnClickedButtonClose()
{
	try {

		// TODO: Add your control notification handler code here
		if (device_opened_ == false) return;
		//Sleep(2);
		ZCAN_ResetCAN(channel_handle_);
		ZCAN_CloseDevice(device_handle_); 

		start_ = FALSE;
		EnableCtrl(FALSE);
		ctrl_start_can_.EnableWindow(TRUE);
		ctrl_int_can_.EnableWindow(TRUE);
		device_opened_ = FALSE;
		AddData(_T("设备已关闭!"));
	}
	catch (exception e) {
	}
}

void CZlgcanDlg::OnBnClickedButtonSend()
{
    CWaitCursor waitCursor;
	UpdateData(TRUE);
	if (datas_.IsEmpty())
	{
		AddData(_T("数据为空"));
		return;
	}
	///for (int i = 0; i < 1000000; i++) 
	{
		int  nSendCount = send_count_once_;
		UINT result;//发送的帧数
		if (0 == protocol_index_)//can
		{
			ZCAN_Transmit_Data can_data;
			GetViewCANFrame(can_data, TRUE);

			//if (nSendCount > 0)
			{
				//for (int ti = 0; ti < 86; ti++)
				{
					ZCAN_Transmit_Data* pData = new ZCAN_Transmit_Data[nSendCount];
					for (int i = 0; i < nSendCount; ++i)
					{
						memcpy_s(&pData[i], sizeof(ZCAN_Transmit_Data), &can_data, sizeof(can_data));
					}

					AddData(pData, 1);
					result = ZCAN_Transmit(channel_handle_, pData, nSendCount);
					delete[] pData;
				}
			}
		}
		else //canfd
		{
			ZCAN_TransmitFD_Data canfd_data;
			GetViewCANFrame(canfd_data, TRUE);

			if (nSendCount > 0)
			{
				//for (int ti = 0; ti < 100000; ti++)
				{
					ZCAN_TransmitFD_Data* pData = new ZCAN_TransmitFD_Data[nSendCount];
					for (int i = 0; i < nSendCount; ++i)
					{
						memcpy_s(&pData[i], sizeof(ZCAN_TransmitFD_Data), &canfd_data, sizeof(canfd_data));
					}

					//AddData(pData, 1); 
					result = ZCAN_TransmitFD(channel_handle_, pData, nSendCount);
					delete[] pData;
				}

			}
		}
		/**/
		CString csText;
		csText.Format(_T("发送数量:%d, 成功数量:%d"), nSendCount, result);
		if (result != nSendCount)
		{
			AddData(_T("发送数据失败! ") + csText);
		}
		else
		{
			AddData(_T("发送数据成功 ") + csText);
		}
		
	}
    
    
}

void CZlgcanDlg::InitCombobox( int ctrl_id, int start, int end, int current )
{
	CComboBox* ctrl = static_cast<CComboBox*>(GetDlgItem(ctrl_id));
	ASSERT(ctrl != NULL);
	ctrl->ResetContent();
	CString temp;
	for (int i = start; i < end; ++i)
	{
		temp.Format(_T("%d"), i);
		ctrl->AddString(temp);
	}
	ctrl->SetCurSel(current);
}

void CZlgcanDlg::OnCbnSelchangeComboDevice()
{
	UpdateData(TRUE);
	InitCombobox(IDC_COMBO_CHANNEL_INDEX, 0, kDeviceType[device_type_index_].channel_count, 0);
    UINT type = kDeviceType[device_type_index_].device_type;
    const BOOL cloudDevice = type==ZCAN_CLOUD;
    const BOOL netcan = IsNetCAN(type);
    const BOOL netDevice = false;
    const BOOL tcpDevice = IsNetTCP(type);

	const BOOL usbcanfd = type == ZCAN_USBCANFD_100U ||
		type == ZCAN_USBCANFD_200U || type == ZCAN_USBCANFD_MINI || type == ZCAN_UCANFD_400U;

	/*
	const BOOL usbcanfd = type == ZQWL_UCANFD_100C ||
		type == ZQWL_UCANFD_100K || type == ZQWL_UCANFD_100E || type == ZQWL_UCANFD_200U || type == ZQWL_UCANFD_200C || type == ZCAN_UCANFD_400U;
    */

    const BOOL canfdDevice = usbcanfd ;
    const BOOL accFilter = false;



 
    if (usbcanfd)
    {	/**/
        m_comboAbit.ResetContent();
	
        m_comboAbit.AddString(TEXT("1Mbps"));
        m_comboAbit.AddString(TEXT("800kbps"));
        m_comboAbit.AddString(TEXT("500kbps"));
        m_comboAbit.AddString(TEXT("250kbps"));
        m_comboAbit.AddString(TEXT("125kbps"));
        m_comboAbit.AddString(TEXT("100kbps"));
        m_comboAbit.AddString(TEXT("50kbps"));
        m_comboAbit.SetCurSel(0);
        m_comboDbit.ResetContent();
	
        m_comboDbit.AddString(TEXT("5Mbps"));
        m_comboDbit.AddString(TEXT("4Mbps"));
        m_comboDbit.AddString(TEXT("2Mbps"));
        m_comboDbit.AddString(TEXT("1Mbps")); 
		m_comboDbit.AddString(TEXT("800kbps"));
		m_comboDbit.AddString(TEXT("500kbps"));
		m_comboDbit.AddString(TEXT("250kbps"));
		m_comboDbit.AddString(TEXT("125kbps"));
		m_comboDbit.AddString(TEXT("100kbps"));
		m_comboDbit.AddString(TEXT("50kbps"));
        m_comboDbit.SetCurSel(0);
    }
    
    GetDlgItem(IDC_COMBO_DEVICE_INDEX2)->EnableWindow(!cloudDevice && !netDevice);
    GetDlgItem(IDC_CHECK_RESISTANCE)->EnableWindow(usbcanfd);
    GetDlgItem(IDC_COMBO_BAUD)->EnableWindow(!canfdDevice && !netDevice && !cloudDevice);
    GetDlgItem(IDC_COMBO_ABIT)->EnableWindow(canfdDevice && !netDevice && !cloudDevice);
    GetDlgItem(IDC_COMBO_ABIT2)->EnableWindow(canfdDevice && !netDevice && !cloudDevice);
    GetDlgItem(IDC_CHECK_CUSTOM_BAUDRATE)->EnableWindow(!cloudDevice && !netDevice);
    GetDlgItem(IDC_EDIT_CUSTOM_BAUDRATE)->EnableWindow(!cloudDevice && !netDevice);
    GetDlgItem(IDC_COMBO_FILTER_MODE)->EnableWindow(accFilter && !cloudDevice && !netDevice);
    GetDlgItem(IDC_EDIT_ACC_CODE)->EnableWindow(accFilter && !cloudDevice && !netDevice);
    GetDlgItem(IDC_EDIT_ACC_MASK)->EnableWindow(accFilter && !cloudDevice && !netDevice);
    GetDlgItem(IDC_STATIC_WORK_MODE)->EnableWindow(!cloudDevice && !netDevice);
    GetDlgItem(IDC_STATIC_BAUD)->EnableWindow(!canfdDevice && !netDevice && !cloudDevice);
    GetDlgItem(IDC_STATIC_A_BAUDRATE)->EnableWindow(canfdDevice && !netDevice && !cloudDevice);
    GetDlgItem(IDC_STATIC_D_BAUDRATE)->EnableWindow(canfdDevice && !netDevice && !cloudDevice);
    GetDlgItem(IDC_STATIC_CUSTOM_BAUDRATE)->EnableWindow(!cloudDevice && !netDevice);
    GetDlgItem(IDC_STATIC_FILTER_MODE)->EnableWindow(accFilter && !cloudDevice && !netDevice);
    GetDlgItem(IDC_STATIC_ACC_CODE)->EnableWindow(accFilter && !cloudDevice && !netDevice);
    GetDlgItem(IDC_STATIC_ACC_MASK)->EnableWindow(accFilter && !cloudDevice && !netDevice);

    OnCbnSelchangeComboNetMode();
}

void CZlgcanDlg::OnCbnSelchangeComboNetMode()
{
	
}

void CZlgcanDlg::EnableCtrl( BOOL opened )
{
	ctrl_device_type_.EnableWindow(!opened);
	ctrl_device_index_.EnableWindow(!opened);
	ctrl_channel_index_.EnableWindow(!opened);
	ctrl_open_device_.EnableWindow(!opened);
}

//设置自定义波特率, 需要从CANMaster目录下的baudcal生成字符串
BOOL CZlgcanDlg::SetCustomBaudrate()
{

	char path[50] = {0};
	char value[100] = {0};
	string baudrate = Utility::W2AEx(custom_baudrate_);
	sprintf_s(path, "%d/baud_rate_custom", channel_index_);
	sprintf_s(value, "%s", baudrate.data());
	return 1 == ZCAN_SetValue(device_handle_, path, value);
}

UINT WINAPI CZlgcanDlg::OnDataRecv( LPVOID data )
{
	CZlgcanDlg* this_ = static_cast<CZlgcanDlg*>(data);
	if (this_)
	{
		this_->OnRecv();
	}
	return 0;
}

void CZlgcanDlg::OnRecv()
{
	ZCAN_Receive_Data can_data[1000];
	ZCAN_ReceiveFD_Data canfd_data[1000];
	UINT len;
	while(start_)
	{
		if (len = ZCAN_GetReceiveNum(channel_handle_, TYPE_CAN))
		{
			len = ZCAN_Receive(channel_handle_, can_data, 1000, 50);
			AddData(can_data, len);
		}
		if (len = ZCAN_GetReceiveNum(channel_handle_, TYPE_CANFD))
		{
			len = ZCAN_ReceiveFD(channel_handle_, canfd_data, 1000, 50);
			AddData(canfd_data, len); 
			
		}
        //避免无数据时变成While(1),会占用大量的CPU
        Sleep(15);
	}
}

void CZlgcanDlg::AddData( const ZCAN_Receive_Data* data, UINT len )
{
	char item[TMP_BUFFER_LEN];
	for (UINT i = 0; i < len; ++i)
	{
		const ZCAN_Receive_Data& can = data[i];
		const canid_t& id = can.frame.can_id;
		sprintf_s(item, "接收到CAN ID:%08X %s %s 长度:%d 数据:", GET_ID(id), IS_EFF(id)?"扩展帧":"标准帧"
			, IS_RTR(id)?"远程帧":"数据帧", can.frame.can_dlc);
		for (UINT i = 0; i < can.frame.can_dlc; ++i)
		{
			size_t item_len = strlen(item);
			sprintf_s(&item[item_len], TMP_BUFFER_LEN-item_len, "%02X ", can.frame.data[i]);
		}
		AddData(CString(item));
	}
}

void CZlgcanDlg::AddData( const ZCAN_ReceiveFD_Data* data, UINT len )
{
	char item[TMP_BUFFER_LEN];
	for (UINT i = 0; i < len; ++i)
	{
		const ZCAN_ReceiveFD_Data& canfd = data[i];
		const canid_t& id = canfd.frame.can_id;
		sprintf_s(item, "接收到CANFD ID:%08X %s %s 长度:%d 数据:", GET_ID(id), IS_EFF(id)?"扩展帧":"标准帧"
			, IS_RTR(id)?"远程帧":"数据帧", canfd.frame.len);
		for (UINT i = 0; i < canfd.frame.len; ++i)
		{
			size_t item_len = strlen(item);
			sprintf_s(&item[item_len], TMP_BUFFER_LEN-item_len, "%02X ", canfd.frame.data[i]);
		}
		AddData(CString(item)); 
		
	}
 
	//delete &data;
}

void CZlgcanDlg::AddData( const CString& data )
{
    SYSTEMTIME stTime;
    GetLocalTime(&stTime);
    CString csData;
    csData.Format(_T("[%02d:%02d:%02d.%03d] "), stTime.wHour, stTime.wMinute, stTime.wSecond, stTime.wMilliseconds);
    csData += data;
    const int MAX_LEN_IN_ONELINE = 100;
    int nStrLen = csData.GetLength();
    int nPrinted = 0;
    CString csLine;
    int nOnce = 0;
    while (nPrinted < nStrLen)
    {
        nOnce = min(MAX_LEN_IN_ONELINE, nStrLen - nPrinted);
        csLine = csData.Mid(nPrinted, nOnce);
        data_recv_list_.AddString(csLine);
        nPrinted += nOnce;
    }
	data_recv_list_.SetCurSel(data_recv_list_.GetCount() - 1);
	 
}

void CZlgcanDlg::AddData( const ZCAN_Transmit_Data* data, UINT len )
{
    char item[TMP_BUFFER_LEN];
    for (UINT i = 0; i < len; ++i)
    {
        const ZCAN_Transmit_Data& can = data[i];
        const canid_t& id = can.frame.can_id;
        sprintf_s(item, "Tx CAN ID:%08X %s %s 长度:%d 数据:", GET_ID(id), IS_EFF(id)?"扩展帧":"标准帧"
            , IS_RTR(id)?"远程帧":"数据帧", can.frame.can_dlc);
        for (UINT i = 0; i < can.frame.can_dlc; ++i)
        {
            size_t item_len = strlen(item);
            sprintf_s(&item[item_len], TMP_BUFFER_LEN-item_len, "%02X ", can.frame.data[i]);
        }
        AddData(CString(item));
    }

}

void CZlgcanDlg::AddData( const ZCAN_TransmitFD_Data* data, UINT len )
{
    char item[TMP_BUFFER_LEN];
    for (UINT i = 0; i < len; ++i)
    {
        const ZCAN_TransmitFD_Data& can = data[i];
        const canid_t& id = can.frame.can_id;
        sprintf_s(item, "Tx CANFD ID:%08X %s %s 长度:%d 数据:", GET_ID(id), IS_EFF(id)?"扩展帧":"标准帧"
            , IS_RTR(id)?"远程帧":"数据帧", can.frame.len);
        for (UINT i = 0; i < can.frame.len; ++i)
        {
            size_t item_len = strlen(item);
            sprintf_s(&item[item_len], TMP_BUFFER_LEN-item_len, "%02X ", can.frame.data[i]);
        }
        AddData(CString(item));
    }
}

void CZlgcanDlg::OnClose()
{
	//OnBnClickedButtonClose(); 
	exit(0);
	//OnBnClickedButtonClose();
	//CDialog::OnClose();
}
void CZlgcanDlg::OnBnClickedButtonClear()
{
	data_recv_list_.ResetContent();
}

BOOL CZlgcanDlg::SetTransmitType()
{

	char path[50] = {0};
	char value[100] = {0};
	sprintf_s(path, "%d/send_type", channel_index_);
	sprintf_s(value, "%d", send_type_index_);
	return 1 == ZCAN_SetValue(device_handle_, path, value);
}

//设置终端电阻使能
BOOL CZlgcanDlg::SetResistanceEnable()
{

	char path[50] = {0};
	sprintf_s(path, "%d/initenal_resistance", channel_index_);
	char value[10] = {0};
	sprintf_s(value, "%d", resistance_enable_);
	return 1 == ZCAN_SetValue(device_handle_, path, value);
}

//设置CAN卡波特率
BOOL CZlgcanDlg::SetBaudrate()
{

	char path[50] = {0};
	sprintf_s(path, "%d/baud_rate", channel_index_);
	char value[10] = {0};
	sprintf_s(value, "%d", kBaudrate[baud_index_]);
	return 1 == ZCAN_SetValue(device_handle_, path, value);
}

//设置USBCANFD卡波特率
BOOL CZlgcanDlg::SetCanfdBaudrate()
{

	char path[50] = { 0 };
	sprintf_s(path, "%d/canfd_abit_baud_rate", channel_index_);
	char value[10] = { 0 };
	sprintf_s(value, "%d", kAbitTimingUSB[abit_baud_index_]);
	int ret_a = ZCAN_SetValue(device_handle_, path, value);

	sprintf_s(path, "%d/canfd_dbit_baud_rate", channel_index_);
	sprintf_s(value, "%d", kDbitTimingUSB[dbit_baud_index_]);
	int ret_d = ZCAN_SetValue(device_handle_, path, value);
	return 1 == (ret_a&&ret_d);
}

void CZlgcanDlg::OnBnClickedButtonGetTxAvailable()
{
    // TODO: Add your control notification handler code here

    char path[50] = {0};
    char value[100] = {0};
    CString csText;
    sprintf_s(path, "%d/get_device_available_tx_count/1", channel_index_);
	const char* pRet =(const char*) ZCAN_GetValue(device_handle_,path);
    if (pRet)
    {
        UINT nSpace = *(int*)pRet;
        csText.Format(_T("队列可用空间:%d"), nSpace);
    }
    else
    {
        csText.Format(_T("获取队列可用空间失败!"));
    }
    AddData(csText);
}

void CZlgcanDlg::OnBnClickedButtonClearTxQueue()
{
    // TODO: Add your control notification handler code here
    
    char path[50] = {0};
    char value[100] = {0};
    sprintf_s(path, "%d/clear_delay_send_queue", channel_index_);
	int nRet = ZCAN_SetValue(device_handle_, path, value);
    CString csText;
    csText.Format(_T("清除队列发送 [%s]"), nRet > 0 ? _T("成功") : _T("失败") );
    AddData(csText);
}

void CZlgcanDlg::OnBnClickedCheckDelaySendMode()
{
    // TODO: Add your control notification handler code here
  
    char path[50] = {0};
    char value[100] = {0};
    int nDelaySendQueueMode = (ctrl_delay_send_mode_.GetCheck() == BST_CHECKED);
    sprintf_s(path, "%d/set_send_mode", channel_index_);
    sprintf_s(value, "%d", nDelaySendQueueMode);
	int nRet = ZCAN_SetValue(device_handle_, path, value);
    CString csText, csRet;
    csText = (nDelaySendQueueMode ? _T("开启队列发送模式") : _T("关闭队列发送模式"));
    csRet.Format(_T(" [%s]"), nRet > 0 ? _T("成功") : _T("失败"));
    AddData(csText + csRet);
}

void CZlgcanDlg::SetCtrlStateDelaySend( BOOL delay_send, BOOL delay_send_mode, BOOL get_send_mode )
{
     
}

BOOL CZlgcanDlg::IsNetCAN( UINT type )
{
    return (type==ZCAN_CANETUDP || type==ZCAN_CANETTCP || type==ZCAN_WIFICAN_TCP || type==ZCAN_WIFICAN_UDP ||
            type==ZCAN_CANDTU_NET || type==ZCAN_CANDTU_NET_400);
}

BOOL CZlgcanDlg::IsNetCANFD( UINT type )
{
    return (type==ZCAN_CANFDNET_TCP || type==ZCAN_CANFDNET_UDP ||
        type==ZCAN_CANFDNET_400U_TCP || type==ZCAN_CANFDNET_400U_UDP ||
        type==ZCAN_CANFDWIFI_TCP || type==ZCAN_CANFDWIFI_UDP);
}

BOOL CZlgcanDlg::IsNetTCP( UINT type )
{
    return (type==ZCAN_CANETTCP || type==ZCAN_WIFICAN_TCP || type==ZCAN_CANDTU_NET || type==ZCAN_CANDTU_NET_400 ||
        type==ZCAN_CANFDNET_TCP || type==ZCAN_CANFDNET_400U_TCP ||type==ZCAN_CANFDWIFI_TCP );
}

BOOL CZlgcanDlg::IsNetUDP( UINT type )
{
    return (type==ZCAN_CANETUDP || type==ZCAN_WIFICAN_UDP ||
        type==ZCAN_CANFDNET_UDP || type==ZCAN_CANFDNET_400U_UDP ||type==ZCAN_CANFDWIFI_UDP);
}

void CZlgcanDlg::OnBnClickedButtonGetSendMode()
{
    // TODO: Add your control notification handler code here
   
    CString csText;
    char path[50] = {0};
    sprintf_s(path, "%d/get_send_mode/1", channel_index_);
	const char* pRet = (const char*)ZCAN_GetValue(device_handle_, path);
    if (pRet)
    {
        UINT nMode = *(int*)pRet;
        csText.Format(_T("设备当前模式:[%s]"), nMode ? _T("队列发送模式") : _T("正常发送模式"));
    }
    else
    {
        csText.Format(_T("获取通道模式失败!"));
    }
    AddData(csText);
}

void CZlgcanDlg::SetAutoSendCtrlState( BOOL support_can, BOOL support_canfd, BOOL support_index, BOOL support_single_cancel, BOOL support_get_autosend_list)
{
    BOOL support_autosend = support_can || support_canfd;
    GetDlgItem(IDC_EDIT_AUTOSEND_INDEX)->EnableWindow(support_index);
    GetDlgItem(IDC_EDIT_AUTOSEND_TIME_PERIOD)->EnableWindow(support_autosend);
    GetDlgItem(IDC_BUTTON_ADD_AUTOSEND)->EnableWindow(support_autosend);
    GetDlgItem(IDC_BUTTON_AUTOSEND_START)->EnableWindow(support_autosend);
    GetDlgItem(IDC_BUTTON_AUTOSEND_STOP)->EnableWindow(support_autosend);
    GetDlgItem(IDC_BUTTON_STOP_SINGLE_AUTOSEND)->EnableWindow(support_single_cancel);
    GetDlgItem(IDC_BUTTON_GET_DEV_AUTOSEND)->EnableWindow(support_get_autosend_list);
}

void CZlgcanDlg::AddAutosendCan(int nEnable)
{
    // TODO: Add your control notification handler code here

    UpdateData(TRUE);
    ZCAN_AUTO_TRANSMIT_OBJ autoObj;
    memset(&autoObj, 0, sizeof(autoObj));
    autoObj.enable = nEnable;
    autoObj.interval = auto_send_period_;
    autoObj.index = auto_send_index_;
    GetViewCANFrame(autoObj.obj, FALSE);

    char path[50] = {0};
    sprintf_s(path, "%d/auto_send", channel_index_);
	int nRet = ZCAN_SetValue(device_handle_, path, (char*)&autoObj);
    CString csText;
    csText.Format(_T("添加CAN定时发送 索引:%d 启用:%d 周期:%d ms ID:0x%X [%s]"), autoObj.index, autoObj.enable, autoObj.interval, autoObj.obj.frame.can_id, (nRet ? _T("成功") : _T("失败")));
    AddData(csText);
}

void CZlgcanDlg::AddAutosendCanfd(int nEnable)
{
    // TODO: Add your control notification handler code here

    UpdateData(TRUE);
    ZCANFD_AUTO_TRANSMIT_OBJ autoObj;
    memset(&autoObj, 0, sizeof(autoObj));
    autoObj.enable = nEnable;
    autoObj.interval = auto_send_period_;
    autoObj.index = auto_send_index_;
    GetViewCANFrame(autoObj.obj, FALSE);

    char path[50] = {0};
    sprintf_s(path, "%d/auto_send_canfd", channel_index_);
	int nRet =  ZCAN_SetValue(device_handle_, path, ( char*)&autoObj);
    CString csText;
    csText.Format(_T("添加CANFD定时发送 索引:%d 启用:%d 周期:%d ms ID:0x%X [%s]"), autoObj.index, autoObj.enable, autoObj.interval, autoObj.obj.frame.can_id, (nRet ? _T("成功") : _T("失败")));
    AddData(csText);
}

void CZlgcanDlg::OnBnClickedButtonAutosendStart()
{
    // TODO: Add your control notification handler code here

    char path[50] = {0};
    char value[100] = {0};
    sprintf_s(path, "%d/apply_auto_send", channel_index_);
	int nRet = ZCAN_SetValue(device_handle_, path,"0");
    CString csText;
    csText.Format(_T("开始定时发送 [%s]"), (nRet ? _T("成功") : _T("失败")));
    AddData(csText);
}

void CZlgcanDlg::OnBnClickedButtonAutosendStop()
{
    // TODO: Add your control notification handler code here

    char path[50] = {0};
    char value[100] = {0};
    sprintf_s(path, "%d/clear_auto_send", channel_index_);
	int nRet = ZCAN_SetValue(device_handle_, path, "0");
    CString csText;
    csText.Format(_T("停止定时发送 [%s]"), (nRet ? _T("成功") : _T("失败")));
    AddData(csText);
}

void CZlgcanDlg::OnBnClickedButtonStopSingleAutosend()
{
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);
    if (0 == protocol_index_)
    {
        //CAN
        AddAutosendCan(0);
    }
    else
    {
        // CANFD
        AddAutosendCanfd(0);
    }
}

void CZlgcanDlg::GetViewCANFrame( ZCAN_Transmit_Data& can_data, BOOL add_delay_flag )
{
    UpdateData(TRUE);
    UINT id = _tcstoul(id_, 0, 16);
    string data = Utility::W2AEx(datas_);
    BOOL bDelay = frm_delay_flag_;
    UINT nDelayTime = frm_delay_time_;

    memset(&can_data, 0, sizeof(can_data));
    can_data.frame.can_id = MAKE_CAN_ID(id, frame_type_index_, 0, 0);
    can_data.frame.can_dlc = Utility::split(can_data.frame.data, CAN_MAX_DLEN, data, ' ', 16);
    can_data.transmit_type = send_type_index_;
    if (add_delay_flag && bDelay)
    {
        can_data.frame.__pad |= TX_DELAY_SEND_FLAG;
        can_data.frame.__res0 = LOBYTE(nDelayTime);
        can_data.frame.__res1 = HIBYTE(nDelayTime);
    }
}

void CZlgcanDlg::GetViewCANFrame( ZCAN_TransmitFD_Data& canfd_data, BOOL add_delay_flag )
{
    UpdateData(TRUE);
    UINT id = _tcstoul(id_, 0, 16);
    string data = Utility::W2AEx(datas_);
    BOOL bDelay = frm_delay_flag_;
    UINT nDelayTime = frm_delay_time_;

    memset(&canfd_data, 0, sizeof(canfd_data));
    canfd_data.frame.can_id = MAKE_CAN_ID(id, frame_type_index_, 0, 0);
    canfd_data.frame.len = Utility::split(canfd_data.frame.data, CANFD_MAX_DLEN, data, ' ', 16);
    canfd_data.transmit_type = send_type_index_;
    canfd_data.frame.flags |= canfd_exp_index_ ? CANFD_BRS : 0;
    if (add_delay_flag && bDelay)
    {
        canfd_data.frame.flags |= TX_DELAY_SEND_FLAG;
        canfd_data.frame.__res0 = LOBYTE(nDelayTime);
        canfd_data.frame.__res1 = HIBYTE(nDelayTime);
    }

}

void CZlgcanDlg::OnBnClickedButtonAddAutosend()
{
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);
    if (0 == protocol_index_)
    {
        //CAN
        AddAutosendCan(1);
    }
    else
    {
        // CANFD
        AddAutosendCanfd(1);
    }
}

void CZlgcanDlg::OnBnClickedButtonGetDevAutosend()
{
    // TODO: Add your control notification handler code here

    CString csText;
    UINT nCount = 0;
    char path[50] = {0};

    //CAN
    sprintf_s(path, "%d/get_auto_send_can_count/1", channel_index_);
    const char* pRet = (const char*)ZCAN_GetValue(device_handle_,path);
    if (pRet)
    {
        nCount = *(int*)pRet;
        csText.Format(_T("CAN定时发送数量:%d"), nCount);
        AddData(csText);
        if (nCount > 0)
        {
            sprintf_s(path, "%d/get_auto_send_can_data/1", channel_index_);
            pRet = (const char*) ZCAN_GetValue(device_handle_,path);
            if (pRet)
            {
                const ZCAN_AUTO_TRANSMIT_OBJ* pData = (ZCAN_AUTO_TRANSMIT_OBJ*)pRet;
                for (UINT i = 0; i < nCount; i++)
                {
                    csText.Format(_T("CAN 定时 索引:%d 间隔:%d ms ID:0x%08x"), pData[i].index, pData[i].interval, pData[i].obj.frame.can_id);
                    AddData(csText);
                }
            }
            else
            {
                csText.Format(_T("获取CAN定时发送数据失败!"));
                AddData(csText);
                return;
            }
        }
    }
    else
    {
        csText.Format(_T("获取CAN定时发送数量失败!"));
        AddData(csText);
        return;
    }


    //CANFD
    sprintf_s(path, "%d/get_auto_send_canfd_count/1", channel_index_);
    pRet = (const char*)ZCAN_GetValue(device_handle_,path);
    if (pRet)
    {
        nCount = *(int*)pRet;
        csText.Format(_T("CANFD定时发送数量:%d"), nCount);
        AddData(csText);
        if (nCount > 0)
        {
            sprintf_s(path, "%d/get_auto_send_canfd_data/1", channel_index_);
			pRet = (const char*)ZCAN_GetValue(device_handle_, path);
            if (pRet)
            {
                const ZCANFD_AUTO_TRANSMIT_OBJ* pData = (ZCANFD_AUTO_TRANSMIT_OBJ*)pRet;
                for (UINT i = 0; i < nCount; i++)
                {
                    csText.Format(_T("CANFD 定时 索引:%d 间隔:%d ms ID:0x%08x"), pData[i].index, pData[i].interval, pData[i].obj.frame.can_id);
                    AddData(csText);
                }
            }
            else
            {
                csText.Format(_T("获取CANFD定时发送数据失败!"));
                AddData(csText);
                return;
            }
        }
    }
    else
    {
        csText.Format(_T("获取CANFD定时发送数量失败!"));
        AddData(csText);
        return;
    }
}


void CZlgcanDlg::OnCbnSelchangeComboDeviceIndex()
{
	// TODO:  在此添加控件通知处理程序代码
}
