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

📄 stm32+i

📁 STM32烧写
💻
📖 第 1 页 / 共 2 页
字号:
/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name          : CAN.cpp
* Author             : MCD Application Team
* Version            : v2.0.0
* Date               : 03/07/2009
* Description        : Implements the CCAN class for COM communication 
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

#include <windows.h>
#include <stdio.h>
#include "CAN.h" 

inline string GetPathFromFilename( const string& filename )
{
	return filename.substr( 0, filename.rfind("\\")+1 );
}

char* STAUSToChar(CAN_STATUS status)
{
	switch(status)
	{
		case STATUS_SUCCESS : return  "[SUCCESS]";
		case STATUS_FAIL    : return  "[FAIL]";
		case STATUS_INFO    : return  "[INFO]";
		case STATUS_UNDEFINED    : return  "";
		case STATUS_INPUT   : return "[INPUT]";
		default      : return  "[UNKNOWN]";
	}
}

void CCAN::print_debug(char* messtype, char* mess, CAN_STATUS status)
{
	fprintf(log, "\n %s \t %s \t %s \n", messtype, mess, STAUSToChar( status));
}

///  set serial communication over COM1 with 115200 Bauds, 8 bitand no parity.  
CCAN::CCAN()
{
	this->Handle = NULL;	
	this->bufferSize = 2048;	
	this->speedInBaud = 125000; 
	this->isConnected = FALSE;
	this->timeout = 1000;

	log = fopen("log.txt","w+");
}

CCAN::~CCAN()
{
	if(this->Handle != NULL)
		closeCAN();

	if(log)
		fclose(log);
}


void CCAN::SetComSettings(long _speedInBaud, int DLC)
{
	//---------------------------------------------------------
	// Scan for baudrate value
	//---------------------------------------------------------
    char _baudrate[6];
	print_debug(STAUSToChar(STATUS_INPUT), "Please type the baudrate: ", STATUS_UNDEFINED);
	itoa(_speedInBaud, _baudrate, 6);
	TDataTreePosition Baudrate = Tdt_ItemName2Position (ifcConfig, "/DrvParams/Baudrate");
	while(strcmp(Baudrate->name, "Baudrate") != 0)
	{
		Baudrate = Tdt_GetNextItem (Baudrate, "/DrvParams/Baudrate");
	}
	strcpy(Baudrate->value->value, _baudrate);
    //---------------------------------------------------------
	// Scan for baudrate default ID
	//---------------------------------------------------------
	char _defaultid[6];
	print_debug(STAUSToChar(STATUS_INPUT), "Please type the default id: ", STATUS_UNDEFINED);

	_defaultid[0] = '0';
	_defaultid[1] = 'x';

	itoa(DLC, &_defaultid[2], 6);
	TDataTreePosition DefaultID = Tdt_ItemName2Position (ifcConfig, "/DrvParams/DefaultID");
	while(strcmp(DefaultID->name, "DefaultID") != 0)
	{
		DefaultID = Tdt_GetNextItem (DefaultID, "/DrvParams/DefaultID");
	}
	strcpy(DefaultID->value->value, _defaultid);
}

bool CCAN::open()
{	
	WORD Result = UCRET_SUCCESS ;

	string ExePath(*__argv);
	string Path   = GetPathFromFilename(ExePath);

    size_t size = Path.size() + 1;
    char * buffer = new char[ size ];
    strncpy( buffer, Path.c_str(), size );


	//The UniCOM initialization : UCT_RETVAL uc_Prologue  ( pZTSTRING  libPath   )  "
	//libPath  Pointer to DLL Driver Pool path (zero temrinated string). 
	print_debug(STAUSToChar(STATUS_INFO), "uc_Prologue - Initializing UNICOM", STATUS_UNDEFINED);
    Result = uc_Prologue(/*""*/buffer);
	if(Result != UCRET_SUCCESS)
	{
		print_debug(STAUSToChar(STATUS_INFO), "Could not initialize UNICOM", STATUS_FAIL);
		delete [] buffer;
		return FALSE;
	}

	delete [] buffer;

	//Get UNICOM signature
	//UINT32 uc_GetUnicomSignature  ( void    ) 
	print_debug(STAUSToChar(STATUS_INFO), "uc_GetUnicomSignature - Get UNICOM signature", STATUS_UNDEFINED);
    Result = uc_GetUnicomSignature();
	if(Result != UCRET_SUCCESS)
	{
		print_debug(STAUSToChar(STATUS_INFO), "Could not Get UNICOM signature", STATUS_FAIL);
		return FALSE;
	}
	else
	{
		char* message = (char*)malloc(256);
		sprintf(message, "unicom signature %i", Result); 
		print_debug(STAUSToChar(STATUS_INFO), message, STATUS_UNDEFINED);
	}


	//Get UNICOM info
	print_debug(STAUSToChar(STATUS_INFO), "uc_GetUnicomInfo - Get UNICOM info", STATUS_UNDEFINED);
	char* info = uc_GetUnicomInfo  ( )  ;
	char* message = (char*)malloc(256);
	sprintf(message, "unicom info %s", info); 
	print_debug(STAUSToChar(STATUS_INFO), message, STATUS_UNDEFINED);

	//loading devPaths with a list of all the UniCOM interfaces that are actually available in the system. 
	//UCT_RETVAL uc_GetInterfaceList  ( TDataTree  devPaths   )  
	//devPaths  Handle to TDataTree object.
	print_debug(STAUSToChar(STATUS_INFO), "uc_GetInterfaceList - Get UNICOM interfaces", STATUS_UNDEFINED);
	TDataTree datatree = Tdt_Create();
    Result = uc_GetInterfaceList  (datatree);
	if(Result != UCRET_SUCCESS)
	{
		print_debug(STAUSToChar(STATUS_INFO), "Could not Get interface list", STATUS_FAIL);
	    return FALSE;
	}

	//Display avilable COMBox Devices/Interfaces
	printf("defaultPath %s\n", datatree->defaultPath);
	TDataTreeEntity *dataTreeentity = datatree->root; 
	//while(dataTreeentity != NULL)
	//{
		dataTreeentitydesc = datatree->root->desc;
		//while(dataTreeentitydesc)
		//{
		    printf("----------------------------------------\n");
			printf("Device description\n");
			printf("	path %s\n", dataTreeentitydesc->value->value);
			printf("	name %s\n", dataTreeentitydesc->name);
			printf("----------------------------------------\n");
			//dataTreeentitydesc = dataTreeentitydesc->next;
		//}
		//dataTreeentitydesc = dataTreeentitydesc->next;
	//}

	//Validet
	//UCT_RETVAL uc_ValidateInterfaceHandle  ( UCT_IFCHANDLE  ifcHandle   )  

    //Device initialization UCT_RETVAL uc_StartupDevice  ( pZTSTRING  devPath   )  
	print_debug(STAUSToChar(STATUS_INFO), "uc_StartupDevice - Initializing the device", STATUS_UNDEFINED);
    Result = uc_StartupDevice  (dataTreeentitydesc->value->value);
	if(Result != UCRET_SUCCESS)
	{
		print_debug(STAUSToChar(STATUS_INFO), "Could not initialize the device", STATUS_FAIL);
		return FALSE;
	}
	
    //Get Dafault settings
	//UCT_RETVAL uc_GetDefaultSettings  ( pZTSTRING  devPath,  TDataTree  defSettings ) 
	print_debug(STAUSToChar(STATUS_INFO), "uc_GetDefaultSettings - Get Dafault settings", STATUS_UNDEFINED);
	ifcConfig = Tdt_Create();
	Result = uc_GetDefaultSettings  (dataTreeentitydesc->value->value, ifcConfig);
    if(Result != UCRET_SUCCESS)
	{
		print_debug(STAUSToChar(STATUS_INFO), "Could not Get default settings", STATUS_FAIL);
		return FALSE;
	}

	//this->setSpeed(125000);
	//---------------------------------------------------------
	// Scan for baudrate value
	//---------------------------------------------------------
	print_debug(STAUSToChar(STATUS_INPUT), "Setting default baudrate 125Kb/s", STATUS_UNDEFINED);
	TDataTreePosition Baudrate = Tdt_ItemName2Position (ifcConfig, "/DrvParams/Baudrate");
	while(strcmp(Baudrate->name, "Baudrate") != 0)
	{
		Baudrate = Tdt_GetNextItem (Baudrate, "/DrvParams/Baudrate");
	}
	strcpy(Baudrate->value->value, "125000");
	//---------------------------------------------------------
	// Scan for data format
	//---------------------------------------------------------
	print_debug(STAUSToChar(STATUS_INPUT), "Setting DataFormat FORMATTED", STATUS_UNDEFINED);
	TDataTreePosition DataFormat = Tdt_ItemName2Position (ifcConfig, "/Control/DataFormat");
	while(strcmp(DataFormat->name, "DataFormat") != 0)
	{
		DataFormat = Tdt_GetNextItem (DataFormat, "/Control/DataFormat");
	}
	strcpy(DataFormat->value->value, "FORMATTED");

	
	print_debug(STAUSToChar(STATUS_INFO), "uc_OpenInterface - Opening the interface handle", STATUS_UNDEFINED);

⌨️ 快捷键说明

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