📄 cansenddlg.cpp
字号:
// CANSendDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CANSend.h"
#include "CANSendDlg.h"
#include "..\..\include\adscan.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef struct {
UCHAR rtr; /* EFF RTR 0 0 DLC.3 DLC.2 DLC.1 DLC.0 */
ULONG id; /* not present in standard frames */
UCHAR dlen;
UCHAR data[8];
} CAN_MSG_T, *PCAN_MSG_T;
/////////////////////////////////////////////////////////////////////////////
// CCANSendDlg dialog
CCANSendDlg::CCANSendDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCANSendDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCANSendDlg)
m_nPattern = 0;
m_dwPakTotalCnt = 8;
m_nDevNum = 0;
m_strCount = _T("");
m_bOpen = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCANSendDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCANSendDlg)
DDX_Control(pDX, IDC_REC_LST, m_lstDisplay);
DDX_Text(pDX, IDC_PATTERN, m_nPattern);
DDX_Text(pDX, IDC_PAK_TOTAL_CNT_EDT, m_dwPakTotalCnt);
DDX_Text(pDX, IDC_DEV_NUM, m_nDevNum);
DDX_Text(pDX, IDC_COUNT_STA, m_strCount);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCANSendDlg, CDialog)
//{{AFX_MSG_MAP(CCANSendDlg)
ON_BN_CLICKED(IDC_CLEAR_LIST_BTN, OnClearListBtn)
ON_BN_CLICKED(IDC_START_BTN, OnStartBtn)
ON_BN_CLICKED(IDC_INIT_BTN, OnInitBtn)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCANSendDlg message handlers
BOOL CCANSendDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// initialization value
m_nBTR0 = 0x03; // set baud rate to 125K
m_nBTR1 = 0x1c; // the detailed information, please refer to help file.
m_nAcpCode = 0; // set accept code is 0
m_nAcpMask = 0xff; // set mask value is 0xff
m_nIntMask = 0x0; // set interrupt disable for receive and transmit
m_nOutCtrlCode = 0xfa; // set output control code to 0xfa
return TRUE; // return TRUE unless you set the focus to a control
}
void CCANSendDlg::OnClearListBtn()
{
// TODO: Add your control notification handler code here
m_lstDisplay.ResetContent();
}
void CCANSendDlg::OnStartBtn()
{
// TODO: Add your control notification handler code here
UINT i, j;
CString s;
CAN_MSG_T msg;
UINT nStartPoint;
// MMM: Rose test
DWORD dwRet;
TCHAR szTemp[100];
// initilize message data
UpdateData (TRUE);
msg.id = 0x51;
msg.rtr = 0;
msg.dlen = 8;
BOOL bReady = FALSE;
nStartPoint = m_nPattern;
m_lstDisplay.ResetContent();
for (i = 0; i < m_dwPakTotalCnt; i++)
{
for( j=0; j < 8; j++ )
msg.data[j] = (UCHAR)nStartPoint++;
// try to write something to bus
dwRet = CANWriteFile(m_nPort, (BOOL *) &bReady, (PVOID) &msg);
if ( !bReady )
{
wsprintf(szTemp, _T("CAN send message error code = %d"), dwRet);
MessageBox(szTemp, NULL, MB_OK);
MessageBox(_T( "CAN send message FAILURE!" ), NULL, MB_OK);
return;
}
else
{
s.Format(_T( "SEND : ID = %u, Len = %u, %2.2XH %2.2XH %2.2XH %2.2XH %2.2XH %2.2XH %2.2XH %2.2XH" ),
msg.id,
msg.dlen,
msg.data[0],
msg.data[1],
msg.data[2],
msg.data[3],
msg.data[4],
msg.data[5],
msg.data[6],
msg.data[7]);
AddStrToLst(s);
s.Format(_T( "%lu" ), i + 1);
m_strCount = s;
}
}
UpdateData(FALSE);
MessageBox(_T( "CAN send message OK!" ), NULL, MB_OK);
}
void CCANSendDlg::OnInitBtn()
{
// TODO: Add your control notification handler code here
UpdateData (TRUE);
// Before using driver, we need to call CANPortOpen
if (CANPortOpen(m_nDevNum, (WORD *) &m_nPort, (WORD *) &m_nHostID, (WORD *) &m_nPreBaudRate) != SUCCESS)
{
MessageBox(_T( "CAN port open error!" ), NULL, MB_ICONERROR);
return;
}
// Set protocol usage is 2.0A
if (CANSetProtocolType(m_nPort, CANBUS_PROTOCOL_20A) != SUCCESS)
{
MessageBox(_T( "CANSetProtocol error!" ), NULL, MB_ICONERROR);
CANPortClose(m_nPort);
return;
}
// Initialize baud rate and interrupt bits
if (CANInit(m_nPort, m_nBTR0, m_nBTR1, (UCHAR) m_nIntMask) != SUCCESS)
{
MessageBox(_T( "CAN port init error!" ), NULL, MB_ICONERROR);
CANPortClose(m_nPort);
return;
}
// Set output control code
if (CANSetOutCtrl(m_nPort, m_nOutCtrlCode) != SUCCESS)
{
MessageBox(_T( "CAN out ctrl code error!" ), NULL, MB_ICONERROR);
CANPortClose(m_nPort);
return;
}
// Set accept code for 2.0A
if (CANSetAcp(m_nPort, m_nAcpCode, m_nAcpMask) != SUCCESS)
{
MessageBox(_T( "CAN set acp code error!" ), NULL, MB_ICONERROR);
CANPortClose(m_nPort);
return;
}
// Set baud rate again
if (CANSetBaud(m_nPort, m_nBTR0, m_nBTR1) != SUCCESS)
{
MessageBox(_T( "CAN set baud rate error!" ), NULL, MB_ICONERROR);
CANPortClose(m_nPort);
return;
}
// Set to operation mode
if (CANSetNormal(m_nPort) != SUCCESS)
{
MessageBox(_T( "CAN set normal error!" ), NULL, MB_ICONERROR);
CANPortClose(m_nPort);
return;
}
// Everything is OK, then update the flag
m_bOpen = TRUE;
}
void CCANSendDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
// if m_bOpen is true, then close port usage
if (m_bOpen)
{
if (CANPortClose(m_nPort) != SUCCESS)
{
MessageBox(_T( "CANPortClose error!" ), NULL, MB_ICONERROR);
}
m_bOpen = FALSE;
}
CDialog::OnClose();
}
void CCANSendDlg::AddStrToLst(CString& s)
{
int nItemsCount = 0;
nItemsCount = m_lstDisplay.GetCount();
if (nItemsCount >= 1000)
m_lstDisplay.ResetContent();
m_lstDisplay.AddString(s);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -