CAN 通信vc实例DOCWord文档下载推荐.docx
- 文档编号:21725409
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:59
- 大小:27.64KB
CAN 通信vc实例DOCWord文档下载推荐.docx
《CAN 通信vc实例DOCWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《CAN 通信vc实例DOCWord文档下载推荐.docx(59页珍藏版)》请在冰豆网上搜索。
HWNDhwnd=0;
////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);
//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
IDD)
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
DoDataExchange(CDataExchange*pDX)
CDialog:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CDemoCANDlgdialog
CDemoCANDlg:
CDemoCANDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CDemoCANDlg:
IDD,pParent)
//{{AFX_DATA_INIT(CDemoCANDlg)
m_nSendFrameFormat=0;
m_nSendFrameType=0;
m_strSendData=_T(Sanbuf);
m_strSendID=_T("
00000088"
);
m_radioIDFormat=1;
m_bCanRxEn=FALSE;
m_nCanIndex=0;
m_nDevType=0;
m_box1=1;
m_box2=0;
m_box3=2;
m_box4=0;
m_box11=_T("
传感器输出电压》1V----3V"
m_bCanRxEn1=FALSE;
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->
LoadIcon(IDR_MAINFRAME);
voidCDemoCANDlg:
//{{AFX_DATA_MAP(CDemoCANDlg)
DDX_Control(pDX,IDC_COMB12,m_COMBOX12);
DDX_Control(pDX,IDC_COMBO11,m_box111);
DDX_Control(pDX,IDC_LIST1,m_list);
DDX_CBIndex(pDX,IDC_COMBO_SENDFRAMEFORMAT,m_nSendFrameFormat);
DDX_CBIndex(pDX,IDC_COMBO_SENDFRAMETYPE,m_nSendFrameType);
DDX_Text(pDX,IDC_EDIT_SEND_DATA,m_strSendData);
DDX_Text(pDX,IDC_EDIT_SEND_ID,m_strSendID);
DDX_Radio(pDX,IDC_RADIO_ID_FORMAT,m_radioIDFormat);
DDX_Check(pDX,IDC_CHECK_CANRX_EN,m_bCanRxEn);
DDX_CBIndex(pDX,IDC_COMBO_CAN_INDEX,m_nCanIndex);
DDX_CBIndex(pDX,IDC_COMBO_DEVTYPE,m_nDevType);
DDX_CBIndex(pDX,IDC_COMBO1,m_box1);
DDX_CBIndex(pDX,IDC_COMBO2,m_box2);
DDX_CBIndex(pDX,IDC_COMBO3,m_box3);
DDX_CBIndex(pDX,IDC_COMBO4,m_box4);
DDX_Text(pDX,IDC_COMBO11,m_box11);
DDX_Check(pDX,IDC_CHECK_CANRX_EN2,m_bCanRxEn1);
BEGIN_MESSAGE_MAP(CDemoCANDlg,CDialog)
//{{AFX_MSG_MAP(CDemoCANDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPEN_DEVICE,OnButtonOpenDevice)
ON_BN_CLICKED(IDC_BUTTON_SEND,OnButtonSend)
ON_BN_CLICKED(IDC_CHECK_CANRX_EN,OnCheckCanrxEn)
ON_BN_CLICKED(IDC_BUTTON_CLEAR,OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_CLOSE_DEVICE,OnButtonCloseDevice)
ON_CBN_EDITCHANGE(IDC_COMBO_SENDFRAMEFORMAT,OnEditchangeComboSendframeformat)
ON_CBN_EDITCHANGE(IDC_COMBO_DEVTYPE,OnEditchangeComboDevtype)
ON_BN_CLICKED(IDC_BUTTON1,OnButton1)
ON_BN_CLICKED(IDC_BUTTON3,OnButton3)
ON_BN_CLICKED(IDC_BUTTON4,OnButton4)
ON_BN_CLICKED(IDC_BUTTON5,OnButton5)
ON_CBN_EDITCHANGE(IDC_COMBO11,OnEditchangeCombo5)
ON_BN_CLICKED(IDC_BUTTON6,OnButton6)
ON_BN_CLICKED(IDC_CHECK_CANRX_EN2,OnCheckCanrxEn2)
//CDemoCANDlgmessagehandlers
BOOLCDemoCANDlg:
OnInitDialog()
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
//Setsmallicon
m_box111.SetCurSel(0);
m_COMBOX12.SetCurSel
(1);
SetDlgItemText(IDC_EDIT1,"
未启用"
SetDlgItemText(IDC_EDIT2,"
SetDlgItemText(IDC_EDIT3,"
SetDlgItemText(IDC_EDIT4,"
SetDlgItemText(IDC_EDIT5,"
SetDlgItemText(IDC_EDIT6,"
SetDlgItemText(IDC_EDIT7,"
SetDlgItemText(IDC_EDIT8,"
SetDlgItemText(IDC_EDIT9,"
SetDlgItemText(IDC_EDIT10,"
SetDlgItemText(IDC_EDIT11,"
SetDlgItemText(IDC_EDIT12,"
SetDlgItemText(IDC_EDIT13,"
//信息显示列表初始化
m_list.InsertColumn(0,"
序号"
m_list.SetColumnWidth(0,50);
m_list.InsertColumn(1,"
系统时间"
m_list.SetColumnWidth(1,95);
m_list.InsertColumn(2,"
CANIndex"
m_list.SetColumnWidth(2,70);
m_list.InsertColumn(3,"
Tx-Rx"
m_list.SetColumnWidth(3,70);
m_list.InsertColumn(4,"
设备ID"
m_list.SetColumnWidth(4,70);
m_list.InsertColumn(5,"
结构"
m_list.SetColumnWidth(5,60);
m_list.InsertColumn(6,"
类型"
m_list.SetColumnWidth(6,70);
m_list.InsertColumn(7,"
DLC"
m_list.SetColumnWidth(7,40);
m_list.InsertColumn(8,"
数据"
m_list.SetColumnWidth(8,170);
m_list.InsertColumn(9,"
设置状态"
m_list.SetColumnWidth(9,70);
//信息显示列表初始化完毕
m_strSendID="
12345678"
//SetDlgItemText(IDC_COMBO1,"
1"
//TODO:
Addextrainitializationhere
UpdateData(FALSE);
returnTRUE;
//returnTRUEunlessyousetthefocustoacontrol
OnSysCommand(UINTnID,LPARAMlParam)
if((nID&
0xFFF0)==IDM_ABOUTBOX)
CAboutDlgdlgAbout;
dlgAbout.DoModal();
else
CDialog:
OnSysCommand(nID,lParam);
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
OnPaint()
if(IsIconic())
CPaintDCdc(this);
//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&
rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
OnPaint();
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCDemoCANDlg:
OnQueryDragIcon()
return(HCURSOR)m_hIcon;
//一位十六进制转换为十进制
intHexChar(charc)
if((c>
='
0'
)&
&
(c<
9'
))
returnc-0x30;
elseif((c>
A'
F'
returnc-'
+10;
a'
f'
return0x10;
//两位十六进制数转换为十进制
intStr2Hex(CStringstr)
{
intlen=str.GetLength();
if(len==2)
inta=HexChar(str[0]);
intb=HexChar(str[1]);
if(a==16||b==16)
AfxMessageBox("
格式错误!
"
return256;
else
returna*16+b;
AfxMessageBox("
inputlengthmustbe2"
return256;
//打开设备
OnButtonOpenDevice()
UpdateData(TRUE);
intzcc=0;
charszText[4]={0};
GetDlgItemText(IDC_COMBO_CAN_INDEX,szText,4);
zcc=atoi(szText);
m_DevIndex=0;
m_nDevType=zcc;
m_DevType=m_nDevType==1?
VCI_USBCAN2:
VCI_USBCAN1;
DWORDReserved=0;
//打开设备
if(VCI_OpenDevice(m_DevType,m_DevIndex,Reserved)!
=1)
MessageBox("
openfailed"
return;
if(Mode1==5){
打开失败,请先设置CAN参数!
VCI_INIT_CONFIGInitInfo[1];
InitInfo->
Timing0=Timing00;
Timing1=Timing11;
Filter=Filter1;
AccCode=AccCode1;
AccMask=AccMask1;
Mode=Mode1;
//初始化通道0
if(VCI_InitCAN(m_DevType,m_DevIndex,0,InitInfo)!
=1)//can-0
Init-CANfailed!
Sleep(100);
if(VCI_StartCAN(m_DevType,m_DevIndex,0)!
Start-CANfailed!
//初始化通道1
if(m_nDevType==1)
if(VCI_InitCAN(m_DevType,m_DevIndex,1,InitInfo)!
=1)//can-1
MessageBox("
return;
if(VCI_StartCAN(m_DevType,m_DevIndex,1)!
MessageBox("
Opensuccessfule!
\nStartCANOK!
//关闭设备
OnButtonCloseDevice()
if(VCI_CloseDevice(m_DevType,m_DevIndex)!
关闭失败!
关闭成功!
intintcstring(CStringstext){
charstext3[16]={0};
sprintf(stext3,"
%s"
stext);
intlen=strlen(stext3)+1;
intnum=0;
intjol=0;
for(len;
len>
2;
len--){
if(stext3[len-1]=='
||stext3[len-1]=='
B'
C'
D'
E'
){
intaa=0,pow=1;
aa=stext3[len-1]-'
for(pow;
pow<
jol;
pow++){
aa=aa*16;
}
num=num+aa;
b'
||stext3[le
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CAN 通信vc实例DOC 通信 vc 实例 DOC