📄 hmican_usb.cpp
字号:
// HMIcan_usb.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "HMIcan_usb.h"
#include "HMIcan_usbDlg.h"
#include "ControlCAN.h" ////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHMIcan_usbApp
BEGIN_MESSAGE_MAP(CHMIcan_usbApp, CWinApp)
//{{AFX_MSG_MAP(CHMIcan_usbApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHMIcan_usbApp construction
CHMIcan_usbApp::CHMIcan_usbApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CHMIcan_usbApp object
CHMIcan_usbApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CHMIcan_usbApp initialization
BOOL CHMIcan_usbApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CHMIcan_usbDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
//发送数据函数
//frmid为传入的帧ID参数为8位
//SndChar 发送数据的字节
void CHMIcan_usbApp::SendData(CString frmid,__Char64bits SndChar)
{
if(m_connect==0)
return;
VCI_CAN_OBJ frameinfo;
char szFrameID[9];
unsigned char FrameID[4]={0,0,0,0};
memset(szFrameID,'0',9);
unsigned char Data[8];
CString SendDataStr = "01 02 03 04 05 06 07 08";
char szData[25];
BYTE datalen=0;
if(frmid.GetLength()==0) //////////ID为空
{
//MessageBox("帧ID为空");
return;
}
if(frmid.GetLength()!=8)
{
//MessageBox("帧ID长度不等于8位");
return;
}
memcpy(&szFrameID[8-frmid.GetLength()],(LPCTSTR)frmid,frmid.GetLength());
int jhb=frmid.GetLength();
strtodata((unsigned char*)szFrameID,FrameID,4,0);
datalen=(SendDataStr.GetLength()+1)/3;
strcpy(szData,(LPCTSTR)SendDataStr);
strtodata((unsigned char*)szData,Data,datalen,1);
frameinfo.DataLen=datalen;
memcpy(&frameinfo.Data,Data,datalen);
frameinfo.RemoteFlag=0;//帧格式:数据帧0
frameinfo.ExternFlag=1;//帧类型:扩展帧1
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=0;//发送格式:自发自收2
///////////////////////////
unsigned char tempData[8];
tempData[0]=SndChar.c7;
tempData[1]=SndChar.c6;
tempData[2]=SndChar.c5;
tempData[3]=SndChar.c4;
tempData[4]=SndChar.c3;
tempData[5]=SndChar.c2;
tempData[6]=SndChar.c1;
tempData[7]=SndChar.c0;
//此处是从高位到低位
memcpy(&frameinfo.Data,tempData,datalen);
///////////////////////////////////
if(VCI_Transmit(m_devtype,m_devind,m_cannum,&frameinfo,1)==1)
{
}
else
{
//MessageBox("数据写入失败");
}
Sleep(50);
}
/////////////////////////////////
//-----------------------------------------------------
//参数:
//str:要转换的字符串
//data:储存转换过来的数据串
//len:数据长度
//函数功能:字符串转换为数据串
//-----------------------------------------------------
int CHMIcan_usbApp::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 CHMIcan_usbApp::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;
}
//////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -