📄 testdlg.cpp
字号:
// testDlg.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include "ControlCAN.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_EditFilterParam = _T("");
m_EditReadGroup = _T("");
m_EditSendData = _T("");
m_EditSendFrmID = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_connect=0;
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Control(pDX, IDC_LIST_INFO, m_ListInfo);
DDX_Control(pDX, IDC_COMBO_SENDTYPE, m_ComboSendType);
DDX_Control(pDX, IDC_COMBO_SENDFRAMETYPE, m_ComboSendFrmType);
DDX_Control(pDX, IDC_COMBO_SENDFRAMEFORMAT, m_ComboSendFrmFmt);
DDX_Control(pDX, IDC_COMBO_232BAUD, m_Combo232Baud);
DDX_Control(pDX, IDC_COMBO_CANBAUD, m_ComboCANBaud);
DDX_Control(pDX, IDC_COMBO_BAUD, m_ComboBaud);
DDX_Control(pDX, IDC_COMBO_PORT, m_ComboPort);
DDX_Text(pDX, IDC_EDIT_FILTERPARAMETER, m_EditFilterParam);
DDX_Text(pDX, IDC_EDIT_READGROUP, m_EditReadGroup);
DDX_Text(pDX, IDC_EDIT_SENDDATA, m_EditSendData);
DDX_Text(pDX, IDC_EDIT_SENDFRAMEID, m_EditSendFrmID);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_CANINFO, OnButtonCaninfo)
ON_BN_CLICKED(IDC_BUTTON_STARTCAN, OnButtonStartcan)
ON_BN_CLICKED(IDC_BUTTON_RESETCAN, OnButtonResetcan)
ON_BN_CLICKED(IDC_BUTTON_CHGCANBAUD, OnButtonChgcanbaud)
ON_BN_CLICKED(IDC_BUTTON_CHG232BAUD, OnButtonChg232baud)
ON_BN_CLICKED(IDC_BUTTON_SETFILTER, OnButtonSetfilter)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_COMMAND(ID_MENU_REFRESH, OnMenuRefresh)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::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_ComboPort.SetItemData(m_ComboPort.AddString("COM1"),0);
m_ComboPort.SetItemData(m_ComboPort.AddString("COM2"),1);
m_ComboPort.SetItemData(m_ComboPort.AddString("COM3"),2);
m_ComboPort.SetItemData(m_ComboPort.AddString("COM4"),3);
m_ComboPort.SetCurSel(0);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("2.4Kbps"),2400);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("4.8Kbps"),4800);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("9.6Kbps"),9600);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("14.4Kbps"),14400);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("19.2Kbps"),19200);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("28.8Kbps"),28800);
m_ComboBaud.SetItemData(m_ComboBaud.AddString("57.6Kbps"),57600);
m_ComboBaud.SetCurSel(6);
m_ComboSendType.SetCurSel(2);
m_ComboSendFrmType.SetCurSel(1);
m_ComboSendFrmFmt.SetCurSel(0);
m_EditSendFrmID="00000080";
m_EditSendData="01 02 03 04 05 06 07 08 ";
UpdateData(false);
InitializeCriticalSection(&m_Section);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDlg::OnCancel()
{
// TODO: Add extra cleanup here
m_connect=0;
Sleep(100);
if(m_connect)
VCI_CloseDevice(VCI_CAN232,m_devind);;
DeleteCriticalSection(&m_Section);
CDialog::OnCancel();
}
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
m_connect=0;
Sleep(100);
if(m_connect)
VCI_CloseDevice(VCI_CAN232,m_devind);;
DeleteCriticalSection(&m_Section);
CDialog::OnOK();
}
void CTestDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
DWORD port=m_ComboPort.GetItemData(m_ComboPort.GetCurSel());
DWORD baud=m_ComboBaud.GetItemData(m_ComboBaud.GetCurSel());
if(m_connect==0)
{
if(VCI_OpenDevice(VCI_CAN232,port,baud)!=STATUS_OK)
{
MessageBox("连接失败!","错误",MB_OK|MB_ICONSTOP);
}
else
{
m_connect=1;
m_devind=port;
GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("断开");
GetDlgItem(IDC_BUTTON_STARTCAN)->EnableWindow(true);
AfxBeginThread(ReceiveThread,this);
}
}
else
{
m_connect=0;
Sleep(100);
VCI_CloseDevice(VCI_CAN232,m_devind);
GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("连接");
}
}
void CTestDlg::OnButtonCaninfo()
{
// TODO: Add your control notification handler code here
CString str;
DWORD addr=0;
char tmp;
GetDlgItem(IDC_EDIT_READGROUP)->GetWindowText(str);
if(str.GetLength()==0)
{
ShowInfo("请输入数据",1);
return;
}
str.MakeUpper();
str=str.Left(2);
for(int i=0;i<str.GetLength();i++)
{
addr<<=4;
tmp=str.GetAt(i);
if(tmp-'A'>=0&&'F'-tmp>=0)
addr+=tmp-'A'+10;
else if(tmp-'0'>=0&&'9'-tmp>=0)
addr+=tmp-'0';
}
BYTE Info[14];
Info[0]=addr;
CString str1;
str="滤波器信息:";
if(VCI_GetReference(VCI_CAN232,m_devind,0,1,Info)==1)
{
for(int i=0;i<14;i++)
{
str1.Format(" %02x",Info[i]);
str+=str1;
}
ShowInfo(str,0);
}
else
{
str="读取错误";
ShowInfo(str,2);
}
}
void CTestDlg::OnButtonStartcan()
{
// TODO: Add your control notification handler code here
if(VCI_StartCAN(VCI_CAN232,m_devind,0)==1)
{
//GetDlgItem(IDC_BUTTON_STARTCAN)->EnableWindow(false);
ShowInfo("启动成功",0);
}
else
{
CString str;
str="启动失败";
ShowInfo(str,2);
}
}
void CTestDlg::OnButtonResetcan()
{
// TODO: Add your control notification handler code here
if(VCI_ResetCAN(VCI_CAN232,m_devind,0)==1)
{
//GetDlgItem(IDC_BUTTON_STARTCAN)->EnableWindow(true);
ShowInfo("复位成功",0);
}
else
{
CString str;
str="复位失败";
ShowInfo(str,2);
}
}
void CTestDlg::OnButtonChgcanbaud()
{
// TODO: Add your control notification handler code here
if(m_ComboCANBaud.GetCurSel()==CB_ERR)
{
ShowInfo("请选择要更改的波特率",1);
}
else
{
DWORD i=m_ComboCANBaud.GetCurSel();
if(VCI_SetReference(VCI_CAN232,m_devind,0,1,(PVOID)&i)==1)
{
ShowInfo("更改成功",0);
}
else
{
CString str;
str="更改失败";
ShowInfo(str,2);
}
}
}
void CTestDlg::OnButtonChg232baud()
{
// TODO: Add your control notification handler code here
if(m_Combo232Baud.GetCurSel()==CB_ERR)
{
ShowInfo("请选择要更改的波特率",1);
}
else
{
BYTE i=m_Combo232Baud.GetCurSel()+1;
if(VCI_SetReference(VCI_CAN232,m_devind,0,3,(PVOID)&i)==1)
{
ShowInfo("更改成功",0);
}
else
{
CString str;
str="更改失败";
ShowInfo(str,2);
}
}
}
void CTestDlg::OnButtonSetfilter()
{
// TODO: Add your control notification handler code here
CString str;
BYTE para[12];
int len;
char szpara[40];
GetDlgItem(IDC_EDIT_FILTERPARAMETER)->GetWindowText(str);
if(str.GetLength()==0)
{
ShowInfo("请输入数据",1);
return;
}
len=str.GetLength();
if(len!=36&&len!=35)
{
ShowInfo("数据长度不对,长度应为12个字节",1);
return;
}
strcpy(szpara,(LPCTSTR)str);
strtodata((unsigned char *)szpara,para,12,1);
if(VCI_SetReference(VCI_CAN232,m_devind,0,2,para)==1)
{
ShowInfo("设置成功",0);
}
else
{
str="设置失败";
ShowInfo(str,2);
}
}
void CTestDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
VCI_CAN_OBJ frameinfo;
char szFrameID[9];
unsigned char FrameID[4]={0,0,0,0};
memset(szFrameID,'0',9);
unsigned char Data[8];
char szData[25];
BYTE datalen=0;
UpdateData(true);
if(m_EditSendFrmID.GetLength()==0||
(m_EditSendData.GetLength()==0&&m_ComboSendFrmType.GetCurSel()==0))
{
ShowInfo("请输入数据",1);
return;
}
if(m_EditSendFrmID.GetLength()>8)
{
ShowInfo("ID值超过范围",1);
return;
}
if(m_EditSendData.GetLength()>24)
{
ShowInfo("数据长度超过范围,最大为8个字节",1);
return;
}
if(m_ComboSendFrmType.GetCurSel()==0)
{
if(m_EditSendData.GetLength()%3==1)
{
ShowInfo("数据格式不对,请重新输入",1);
return;
}
}
memcpy(&szFrameID[8-m_EditSendFrmID.GetLength()],(LPCTSTR)m_EditSendFrmID,m_EditSendFrmID.GetLength());
strtodata((unsigned char*)szFrameID,FrameID,4,0);
datalen=(m_EditSendData.GetLength()+1)/3;
strcpy(szData,(LPCTSTR)m_EditSendData);
strtodata((unsigned char*)szData,Data,datalen,1);
UpdateData(false);
frameinfo.DataLen=datalen;
memcpy(&frameinfo.Data,Data,datalen);
frameinfo.RemoteFlag=m_ComboSendFrmFmt.GetCurSel();
frameinfo.ExternFlag=m_ComboSendFrmType.GetCurSel();
if(frameinfo.ExternFlag==1)
{
frameinfo.ID=((DWORD)FrameID[0]<<24)+((DWORD)FrameID[1]<<16)+((DWORD)FrameID[2]<<8)+
((DWORD)FrameID[3]);
}
else
{
frameinfo.ID=((DWORD)FrameID[2]<<8)+((DWORD)FrameID[3]);
}
frameinfo.SendType=m_ComboSendType.GetCurSel();
if(VCI_Transmit(VCI_CAN232,m_devind,0,&frameinfo,1)==1)
{
ShowInfo("写入成功",0);
}
else
{
ShowInfo("写入失败",2);
}
}
void CTestDlg::ShowInfo(CString str, int code)
{
m_ListInfo.InsertString(m_ListInfo.GetCount(),str);
m_ListInfo.SetCurSel(m_ListInfo.GetCount()-1);
}
//-----------------------------------------------------
//参数:
//str:要转换的字符串
//data:储存转换过来的数据串
//len:数据长度
//函数功能:字符串转换为数据串
//-----------------------------------------------------
int CTestDlg::strtodata(unsigned char *str, unsigned char *data,int len,int flag)
{
unsigned char cTmp=0;
int i=0;
for(int j=0;j<len;j++)
{
if(chartoint(str[i++],&cTmp))
return 1;
data[j]=cTmp;
if(chartoint(str[i++],&cTmp))
return 1;
data[j]=(data[j]<<4)+cTmp;
if(flag==1)
i++;
}
return 0;
}
//-----------------------------------------------------
//参数:
//chr:要转换的字符
//cint:储存转换过来的数据
//函数功能:字符转换为数据
//-----------------------------------------------------
int CTestDlg::chartoint(unsigned char chr, unsigned char *cint)
{
unsigned char cTmp;
cTmp=chr-48;
if(cTmp>=0&&cTmp<=9)
{
*cint=cTmp;
return 0;
}
cTmp=chr-65;
if(cTmp>=0&&cTmp<=5)
{
*cint=(cTmp+10);
return 0;
}
cTmp=chr-97;
if(cTmp>=0&&cTmp<=5)
{
*cint=(cTmp+10);
return 0;
}
return 1;
}
UINT CTestDlg::ReceiveThread(void *param)
{
CTestDlg *dlg=(CTestDlg*)param;
CListBox *box=(CListBox *)dlg->GetDlgItem(IDC_LIST_INFO);
VCI_CAN_OBJ frameinfo[50];
int len=1;
int i=0;
CString str,tmpstr;
while(1)
{
Sleep(1);
if(dlg->m_connect==0)
break;
len=VCI_Receive(VCI_CAN232,dlg->m_devind,0,frameinfo,50,200);
if(len>0)
{
for(i=0;i<len;i++)
{
str="接收到数据帧: ";
if(frameinfo[i].TimeFlag==0)
tmpstr="时间标识:无 ";
else
tmpstr.Format("时间标识:%08x ",frameinfo[i].TimeStamp);
str+=tmpstr;
tmpstr.Format("帧ID:%08x ",frameinfo[i].ID);
str+=tmpstr;
str+="帧格式:";
if(frameinfo[i].RemoteFlag==0)
tmpstr="数据帧 ";
else
tmpstr="远程帧 ";
str+=tmpstr;
str+="帧类型:";
if(frameinfo[i].ExternFlag==0)
tmpstr="标准帧 ";
else
tmpstr="扩展帧 ";
str+=tmpstr;
box->InsertString(box->GetCount(),str);
if(frameinfo[i].RemoteFlag==0)
{
str="数据:";
if(frameinfo[i].DataLen>8)
frameinfo[i].DataLen=8;
for(int j=0;j<frameinfo[i].DataLen;j++)
{
tmpstr.Format("%02x ",frameinfo[i].Data[j]);
str+=tmpstr;
}
//EnterCriticalSection(&(dlg->m_Section));
//LeaveCriticalSection(&(dlg->m_Section));
box->InsertString(box->GetCount(),str);
}
}
box->SetCurSel(box->GetCount()-1);
}
}
return 0;
}
void CTestDlg::OnMenuRefresh()
{
// TODO: Add your command handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -