利用c++深究周立功usbcan盒的学习
根据某新能源电动车辆的通讯协议,利用c++开发上位机软件,通过周立功usbcan盒将开发的上位机与车辆OBD口连接,从而可以开发更多的上位机,用来对新能源车辆的监控。
以下为部分C++代码主要实现上位机读取信息的过程:
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_(0)
, send_type_index_(0)
, acc_code_(T(“00000000”))
, acc_mask(T(“FFFFFFFF”))
, id(T(“00000001”))
, datas(T(“00 11 22 33 44 55 66 77”))
, custom_baudrate(T(""))
, filter_mode(1)
, net_mode_index_(0)
, baud_index_(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
device_opened_ = FALSE;
start_ = 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_CBIndex(pDX, IDC_COMBO_NET_MODE, net_mode_index_);
}
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)
END_MESSAGE_MAP()
以下为波特率的设置:
if (custom_baud_enable_)
{
if (!SetCustomBaudrate())
{
AddData(_T(“设置自定义波特率失败!”));
return;
}
}
else
{
if (!canfdDevice && !SetBaudrate())
{
AddData(_T(“设置波特率失败!”));
return;
}
}