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

📄 win32progman.cpp

📁 KaOS is a real-time operating system that has been implemented with the basic real-time constraints
💻 CPP
字号:
// AtmelProgrammer.cpp//

#include <iostream>
#include <tchar.h>
#include <windows.h>
using namespace std;
void PrintLastError(void);
int HexToInt(char* string);
bool ReadBytes(HANDLE hCom, char* buffer, int numBytes, int timeout);

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hCom;
	FILE *hFile;
	DCB dcb;
	long lInst;
	int i, iFlashSize, iBootSize, iStartAddr;
	OFSTRUCT ofs;
	LPTSTR lpBuffer;
	char	readbuffer[256], writebuffer[256], *instbuffer;
	DWORD	readsize,writesize;

	for (i = 0; i < 256; i++)
	{
		writebuffer[i] = readbuffer[i] = 0;
	}

	iStartAddr = 0x1234;

	hCom	= CreateFile("COM2",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

	if (hCom == INVALID_HANDLE_VALUE)
	{
		PrintLastError();
		exit(0);
	}

	//hFile	= (HANDLE)OpenFile("E:/Documents and Settings/Nicholas Clark/My Documents/Cornell - Spring 2005/ECE 476/lab1.rom", &ofs, OF_READ);
printf("\r\n%s\r\n", argv[1]);
	hFile	= fopen(argv[1], "r");

	GetCommState(hCom, &dcb);
	dcb.BaudRate = CBR_19200;     // set the baud rate
	dcb.ByteSize = 8;             // data size, xmit, and rcv
	dcb.Parity = NOPARITY;        // no parity bit
	dcb.StopBits = ONESTOPBIT;    // one stop bit

	SetCommState(hCom, &dcb);
    /*for (int i = 1; i < 255; i++)
    {
        writebuffer[0] = i;
        writebuffer[1] = 0;
        printf("writing %d\r\n", i);
        WriteFile(hCom, writebuffer, 1, &writesize, NULL);
	    ReadBytes(hCom, readbuffer, 1, 100000);
        Sleep(100);
    }
    scanf("\r\n");*/

	fseek (hFile , 0 , SEEK_END);
	lInst = ftell(hFile) / 13;
    printf("#inst: %d\r\n", lInst);
	rewind(hFile);

	// send the hello
	sprintf(writebuffer, "+");
	WriteFile(hCom, writebuffer, 1, &writesize, NULL);

	// get the hello
	ReadBytes(hCom, readbuffer, 1, 10000);
    printf("%c\r\n", readbuffer[0]);

    // send the length
    writebuffer[0] = (lInst >> 8);
    writebuffer[1] = (lInst >> 0);
    writebuffer[2] = 0;
    WriteFile(hCom, writebuffer, 2, &writesize, NULL);

	// get the OK to write
	ReadBytes(hCom, readbuffer, 1, 10000);
    printf("%c\r\n", readbuffer[0]);

	// start sending the instructions
	i = 0;
	while (fread(&readbuffer, 1, 12, hFile) == 12)
	{
		++i;
		readbuffer[11] = 0;

		int temp = HexToInt(&readbuffer[7]);
        char cbuff[15];
        sprintf(cbuff, "%s", &readbuffer[7]);
		writebuffer[0] = (temp >> 8) & 0xFF;
		writebuffer[1] = (temp >> 0) & 0xFF;
		writebuffer[2] = 0;

		// Send the instruction to the mega32
		WriteFile(hCom, writebuffer, 2, &writesize, NULL);
		FlushFileBuffers(hCom);

		// Wait for acknowledgement
		ReadBytes(hCom, readbuffer, 1, 10000);

		switch (readbuffer[0])
		{
			case '+' :
			default :
				// everything is ok
				printf("Success writing instruction #%d [%s]\r\n", i, cbuff);
				break;
			case '-' :
				// there was an error, try it again
				fseek(hFile, -12, SEEK_CUR);
				printf("Error writing instruction #%d [%s]\r\n", i, cbuff);
				break;
		}
	}
	fclose(hFile);
	// send the Finished
	writebuffer[0] = 0x01;
	writebuffer[1] = 0xFF;
	writebuffer[2] = 0;
	WriteFile(hCom, writebuffer, 2, &writesize, NULL);

	CloseHandle(hCom);
	return 0;
}

// not sure if ReadFile is blocking..If it is not, we need to poll
bool ReadBytes(HANDLE hCom, char* readbuffer, int numBytes, int timeout)
{
	int i = 0;
	DWORD readsize;

	do
	{
		if (i > 0) Sleep(500);
		ReadFile(hCom, readbuffer, 1, &readsize, NULL);
	}
	while (readsize != numBytes && ++i < timeout);

	return (i < timeout);
}
int HexToInt(char* string)
{
	int temp = 0;
	int i = 0;

	//printf("hextoint: %s\r\n", string);
	while (string[i])
	{
		char ch = string[i];

		//printf("hextoint: %c\r\n", ch);
		if (ch >= 'A' && ch <= 'F')
		{
			ch = ch - 'A' + 10;
		}
		else if (ch >= 'a' && ch <= 'f')
		{
			ch = ch - 'a' + 10;
		}
		else
		{
			ch = ch - '0';
		}
		//printf("hextoint: %d\r\n", ch);
		temp = (temp << 4) | (ch & 0xF);
		++i;
	}
	return temp;
}
void PrintLastError()
{
	LPTSTR lpBuffer;

	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		GetLastError(),
		LANG_USER_DEFAULT,
		(LPTSTR)&lpBuffer,
		0,
		NULL );
	cout << lpBuffer;
}

⌨️ 快捷键说明

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