STM32 IAP升级工具

1、内部包含了crc_mode的检验

2、文件包含了头尾、长度的数据,确保升级操作的可靠性

3、win32串口查找功能GetSerialList(); 

4、(RegKey.Open(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), KEY_READ) == ERROR_SUCCESS)     win7可以默认方式打开,win10不可以必须以KEY_READ方式打开

 5、包含以十六进制方式保存文件

https://download.csdn.net/download/u010261063/11961366




void CSTM32_IAP_ToolsDlg::GetSerialList()
{

	CRegKey RegKey;
	int nCount = 0;
	CString str;
	if (RegKey.Open(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), KEY_READ) == ERROR_SUCCESS)
	{
		while (true)
		{
			WCHAR ValueName[_MAX_PATH];
			unsigned char ValueData[_MAX_PATH];
			DWORD nValueSize = _MAX_PATH;
			DWORD nDataSize = _MAX_PATH;
			DWORD nType;
			if (::RegEnumValue(HKEY(RegKey), nCount++, ValueName, &nValueSize, NULL, &nType, ValueData, &nDataSize) == ERROR_NO_MORE_ITEMS)
			{
				break;
			}
			str.Format(_T("%s"), ValueData);
			m_serial_list.InsertString(nCount - 1, str);
		}

	}
	m_serial_list.SetCurSel(0);
}


void CSTM32_IAP_ToolsDlg::OnOpenfile()
{
	// TODO:  在此添加控件通知处理程序代码
	TCHAR szFilter[] = _T("文本文件(*.bin)|*.bin|所有文件(*.*)|*.*||");
	CFileDialog fileDlg(TRUE, _T("bin"), NULL, 0, szFilter, this);
	CString strFilePath;

	// 显示打开文件对话框   
	if (IDOK == fileDlg.DoModal())
	{
		// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里   
		strFilePath = fileDlg.GetPathName();
		//SetDlgItemText(IDC_OPEN_EDIT, strFilePath);
		m_file_name.SetWindowText(strFilePath);
	}

}
void  CSTM32_IAP_ToolsDlg::WritSerialData(string data)
{
	HANDLE hComm;
	CString com;
	CString filename;
	boost::asio::io_service service;
	m_serial_list.GetWindowTextW(com);
	
	m_file_name.GetWindowText(filename);
	hComm = CreateFile(com,
		GENERIC_READ | GENERIC_WRITE,
		0,
		0,
		OPEN_EXISTING,
		NULL,
		0);
	if (hComm != INVALID_HANDLE_VALUE)
	{
		DCB  dcb;

		int rate = 115200;

		memset(&dcb, 0, sizeof(dcb));//在一段内存块中填充某个给定的值,是对较大的结构//体或数组进行清零操作的一种最快方法

		//if (!GetCommState(hComm, &dcb))//获取当前DCB配置



		//	return FALSE;

		// set DCB to configure the serial port

		dcb.DCBlength = sizeof(dcb);

		/* ---------- Serial Port Config ------- */

		dcb.BaudRate = rate;

		dcb.Parity = NOPARITY;

		dcb.fParity = 0;

		dcb.StopBits = ONESTOPBIT;

		dcb.ByteSize = 8;

		dcb.fOutxCtsFlow = 0;

		dcb.fOutxDsrFlow = 0;

		dcb.fDtrControl = DTR_CONTROL_DISABLE;

		dcb.fDsrSensitivity = 0;

		dcb.fRtsControl = RTS_CONTROL_DISABLE;

		dcb.fOutX = 0;

		dcb.fInX = 0;

		/* ----------------- misc parameters ----- */

		dcb.fErrorChar = 0;

		dcb.fBinary = 1;

		dcb.fNull = 0;

		dcb.fAbortOnError = 0;

		dcb.wReserved = 0;

		dcb.XonLim = 2;

		dcb.XoffLim = 4;

		dcb.XonChar = 0x13;

		dcb.XoffChar = 0x19;

		dcb.EvtChar = 0;

		// set DCB

		SetCommState(hComm, &dcb);
		unsigned long  relen = 0;
		WriteFile(hComm, data.c_str(), data.size(), &relen, NULL);
		DWORD err = GetLastError();
		CloseHandle(hComm);
	}
}

void CSTM32_IAP_ToolsDlg::OnBnClickedSendupdatefile()
{
	// TODO:  在此添加控件通知处理程序代码
	USES_CONVERSION;
	CString com;
	
	/*boost::asio::io_service service;
	m_serial_list.GetWindowTextW(com);
	SerialPort serial(service, W2A(com), 0);*/
	
	CString filename;
	m_file_name.GetWindowText(filename);
	string data = GetAppData(W2A(filename));
	data = addheadtail(data);
	writeHexToLog(data);
	//serial.write_to_serial_sync(data);
	WritSerialData(data);
	MessageBox(_T("发送完成"));
}

std::string CSTM32_IAP_ToolsDlg::GetAppData(string filename)
{

		int size = 0;
		string data;
		ifstream f(filename, ifstream::binary);
		if (f.is_open())
		{
			f.seekg(0, std::ios_base::end);
			size = int(f.tellg());
			f.seekg(0, std::ios_base::beg);
			char *buf = new char[size];
			f.read(buf, size);
			data.append(string(buf, size));
			delete[]buf;
			f.close();
		}
		else
		{
			cout << "open file fial file= " << filename << endl;
			//ToolFunction::writeCharacterLog(" open file fial file = " + filename);

		}
		return data;
	
}
std::string CSTM32_IAP_ToolsDlg::inttostring(char*p, int len)
{
	string str;
	char *ptr = p;
	for (int i = 0; i < len; i++)
	{
		str.push_back(*(ptr + i));
		cout << hex << (unsigned char)(*(ptr + i)) << endl;
	}
	//str.erase(str.begin() + sizeof(len)-1, str.end());
	return str;
}
std::string CSTM32_IAP_ToolsDlg::addheadtail(std::string &str)
{
	std::string temp;
	int len = str.size();
	temp.append(inttostring((char*)&len,4));
	temp.append(str);
	temp.insert(temp.begin(), char(0xaa));
	temp.insert(temp.begin(), char(0xaa));
	temp.insert(temp.begin(), char(0xaa));
	temp.insert(temp.begin(), char(0xaa));
	temp.push_back(char(0xee));
	temp.push_back(char(0xee));
	temp.push_back(char(0xee));
	temp.push_back(char(0xee));
	uint16_t crc = GetCRC((int8_t*)temp.c_str(), temp.size());
	temp.append(inttostring((char*)&crc, 2));
	return temp;
}

uint16_t CSTM32_IAP_ToolsDlg::GetCRC(int8_t * data, uint32_t len)
{
	unsigned short wCRCin = 0x0000;
	unsigned short wCPoly = 0x1021;
	unsigned char wChar = 0;
	while (len--)
	{
		wChar = *(data++);
		wCRCin ^= (wChar << 8);
		for (int i = 0; i < 8; i++)
		{
			if (wCRCin & 0x8000)
			{
				wCRCin = (wCRCin << 1) ^ wCPoly;
			}
			else
			{
				wCRCin = wCRCin << 1;
			}
		}
	}
	return (wCRCin);
}

string CSTM32_IAP_ToolsDlg::writeHexToLog(const string& str) //transfer string to hex-string
{
	string result;
	string tmp;
	ofstream out("data.log", std::ios_base::app);
	for (unsigned int i = 0; i < str.size(); i++)
	{
		out << setw(2) << setfill('0') << hex << ((unsigned int)str[i] & 0x000000ff);
	}
	std::string strTime = boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time());
	out << '\t' << strTime;
	out << '\n';
	out << endl;
	out << dec << "\n" << endl;
	return result;
}
发布了136 篇原创文章 · 获赞 22 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u010261063/article/details/102947762
今日推荐