⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cantest2dlg.cpp

📁 CanTest2
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CanTest2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "CanTest2.h"
#include "CanTest2Dlg.h"
#include "ControlCAN.h"
#include "CanSetting.h"
#include "XianShi.h"
#include "CSpreadSheet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

unsigned long nextrow;
int StopFlag=0;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCanTest2Dlg dialog

CCanTest2Dlg::CCanTest2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCanTest2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCanTest2Dlg)
	m_strZhenGeShi = _T("标准帧");
	m_strSendData = _T("11 22 33 44 55 66 77 88");
	m_Receive = FALSE;
	m_SelfCheck = FALSE;
	m_nTongDao = 0;
	m_nGongZuoMoShi = 0;
	m_nGuoLvMoShi = 0;
	m_nZhenLeiXing = 0;
	m_nZhenMuDi = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCanTest2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCanTest2Dlg)
	DDX_Control(pDX, IDC_COMBO_MUDI, m_ctrlZhenMuDi);
	DDX_Control(pDX, IDC_SELFCHECK, m_ctrlSelfCheck);
	DDX_Control(pDX, IDC_LIST_RECEIVE, m_list);
	DDX_Control(pDX, IDC_COMBO_LEIXING, m_ctrlZhenLeiXing);
	DDX_Text(pDX, IDC_EDIT_GESHI, m_strZhenGeShi);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSendData);
	DDX_Check(pDX, IDC_RECEIVE, m_Receive);
	DDX_Check(pDX, IDC_SELFCHECK, m_SelfCheck);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCanTest2Dlg, CDialog)
	//{{AFX_MSG_MAP(CCanTest2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_OPEN, OnOpen)
	ON_COMMAND(ID_CLOSE, OnClose)
	ON_COMMAND(ID_SHEZHI, OnShezhi)
	ON_COMMAND(ID_CANSHU, OnCanshu)
	ON_COMMAND(ID_GUANYU, OnGuanyu)
	ON_CBN_SELCHANGE(IDC_COMBO_LEIXING, OnSelchangeComboLeixing)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_RECEIVE, OnReceive)
	ON_BN_CLICKED(IDC_SELFCHECK, OnSelfcheck)
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(IDC_COMBO_MUDI, OnSelchangeComboMudi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCanTest2Dlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

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

	// 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
	
	// TODO: Add extra initialization here
	m_ctrlZhenLeiXing.SetCurSel(m_nZhenLeiXing);
	m_ctrlZhenMuDi.SetCurSel(m_nZhenMuDi);

	m_list.InsertColumn(0,"序号");
	m_list.SetColumnWidth(0,40);
	m_list.InsertColumn(1,"时间");
	m_list.SetColumnWidth(1,85);
	m_list.InsertColumn(2,"CAN通道");
	m_list.SetColumnWidth(2,60);
	m_list.InsertColumn(3,"传输方向");
	m_list.SetColumnWidth(3,60);
	m_list.InsertColumn(4,"帧源");
	m_list.SetColumnWidth(4,60);
	m_list.InsertColumn(5,"帧目的地");
	m_list.SetColumnWidth(5,100);
	m_list.InsertColumn(6,"帧类型");
	m_list.SetColumnWidth(6,50);
	m_list.InsertColumn(7,"帧格式");
	m_list.SetColumnWidth(7,50);
	m_list.InsertColumn(8,"数据长度");
	m_list.SetColumnWidth(8,60);
	m_list.InsertColumn(9,"数据信息");
	m_list.SetColumnWidth(9,200);

	m_ctrlSelfCheck.EnableWindow(false);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// 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 CCanTest2Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

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

int HexChar(char c)
{
	if((c>='0') && (c<='9'))
		return c-0x30;
	else if((c>='A') && (c<='F'))
		return c-'A'+10;
	else if((c>='a') && (c<='f'))
		return c-'a'+10;
	else
		return 0x10;
}

int Str2Hex(CString str)
{	
	int len = str.GetLength();
	if(len == 2)
	{
		int a= HexChar(str[0]);
		int b =HexChar(str[1]);
		if(a==16 || b==16 )
		{
			AfxMessageBox("Format error");
			return 256;
		}
		else
		{
			return a*16+b;
			
		}		
	}
	else
	{
		AfxMessageBox("input length must be 2");
		return 256;
	}
}

void CCanTest2Dlg::OnOpen() 
{
	// TODO: Add your command handler code here
	if(VCI_OpenDevice(DEV_USBCAN200,0,0)!=1)
	{
		MessageBox("连接设备失败!");

		VCI_INIT_CONFIG InitInfo[1];
	    InitInfo->AccCode=0x80000008;           //过滤验收码设置
	    InitInfo->AccMask=0xFFFFFFFF;           //过滤屏蔽码设置
		InitInfo->kCanBaud=12;                  //CAN波特率设置,12为500波特率
		InitInfo->Timing0=0x00;                 //Timing0(BTR0)设置
		InitInfo->Timing1=0x1C;                 //Timing1(BTR1)设置
		InitInfo->Mode=0;                       //工作模式设置,0为正常模式,1为自检模式
		InitInfo->Filter=0;                     //接受滤波方式,0为单滤波,1为双滤波
		InitInfo->CanRx_IER=1;                  //CAN接受中断设置,1为开启,0为关闭
		if(VCI_InitCAN(DEV_USBCAN200,0,0,InitInfo)!=1)
		{
			MessageBox("初始化失败!");
			return;
		}
		return;
	}
	if(VCI_StartCAN(DEV_USBCAN200,0,0)!=1)
	{
		MessageBox("打开设备失败!");
		return;
	}
	else
	{
		MessageBox("打开成功!");
		m_ctrlSelfCheck.EnableWindow(true);
		return;
	}
}

void CCanTest2Dlg::OnClose() 
{
	// TODO: Add your command handler code here
	if(VCI_CloseDevice(DEV_USBCAN200,0)!=1)
	{
		MessageBox("关闭失败!");
		return;
	}
	else
	{
		MessageBox("关闭成功!");
		m_ctrlSelfCheck.EnableWindow(false);
	}
}

void CCanTest2Dlg::OnShezhi() 
{
	// TODO: Add your command handler code here
	CCanSetting CanSetting;
	CanSetting.m_nTongDao=m_nTongDao;
	CanSetting.m_nGuoLvMoShi=m_nGuoLvMoShi;

	if(VCI_StartCAN(DEV_USBCAN200,0,0)!=1)
	{
		MessageBox("设备未打开");
	}
    else if(CanSetting.DoModal()==IDOK)
	{
		m_nTongDao=CanSetting.m_nTongDao;
        m_nGuoLvMoShi=CanSetting.m_nGuoLvMoShi;
	}
}

void CCanTest2Dlg::OnCanshu() 
{
	// TODO: Add your command handler code here
	CXianShi XianShi;
	CCanSetting CanSetting;
	XianShi.DoModal();
	XianShi.m_TongDao.Format("%s",CanSetting.str);
}

void CCanTest2Dlg::OnGuanyu() 
{
	// TODO: Add your command handler code here
	CAboutDlg AboutDlg;
	AboutDlg.DoModal();
}

void CCanTest2Dlg::OnSelchangeComboLeixing() 
{
	// TODO: Add your control notification handler code here
	m_nZhenLeiXing=m_ctrlZhenLeiXing.GetCurSel();
}

void CCanTest2Dlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	VCI_CAN_OBJ sendbuf[1];

	UpdateData(TRUE);
	CString str5[32];
	BYTE buf[50];
	CString strtemp,strtemp1;
	CString str1;
	int len,datanum=0,IDnum=0,newflag=1,i;

	newflag=1;
	len=m_strSendData.GetLength();
	for(i=0; i<len;i++)
	{
		strtemp=m_strSendData.GetAt(i);
		if(strtemp==" ")
			newflag=1;
		else if(newflag==1)
		{	
			newflag=0;
			strtemp=m_strSendData.GetAt(i);		
			if(i==(len-1))
			{
				str5[datanum]="0"+strtemp;
			}
			else
			{
				strtemp1=m_strSendData.GetAt(i+1);

				if(strtemp1 == " ")
				{
					str5[datanum]="0"+strtemp;
				}
				else
					str5[datanum]=strtemp+strtemp1;			
			}
			buf[datanum]=Str2Hex(str5[datanum]);
			datanum++;
			if(datanum>=8)
				break;
		}
	}

	CString string;
	sendbuf->ExternFlag=0;
	sendbuf->DataLen=8;
/*	sendbuf->BiaoShiFu[2]=0x00;

	switch(m_nZhenMuDi)
	{
	case 0:	
		sendbuf->BiaoShiFu[1]=0x00;
		m_ctrlZhenMuDi.GetWindowText(string);
		break;
	case 1:
        sendbuf->BiaoShiFu[1]=0x01;
		m_ctrlZhenMuDi.GetWindowText(string);
		break;
	case 2:
		sendbuf->BiaoShiFu[1]=0x02;
		m_ctrlZhenMuDi.GetWindowText(string);
		break;
	case 3:
		sendbuf->BiaoShiFu[1]=0x03;
		m_ctrlZhenMuDi.GetWindowText(string);
		break;
	case 4:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -