Скачиваний:
35
Добавлен:
15.09.2014
Размер:
1.31 Кб
Скачать
#include <stdio.h>
#include "qwert.cpp"
#include <conio.h>
#include <tchar.h>
SerialGate sg;

DWORD WINAPI Read(LPVOID k)
{
	char buf[20];
	int dwBytesRead = 0;
	bool terminate  = false;

	while(!terminate)
	{		
		Sleep(10);
		dwBytesRead = sg.Recv(buf, sizeof(buf));		

		for(int i = 0; i < dwBytesRead; i++)
		{
			printf("%c", buf[i]);
			if(buf[i] == 13) printf("\n");
			if(buf[i] == '#')
			{
				terminate = true;
				break;
			}
		}	
	}

	return 0;
}

int _tmain(int argc,TCHAR * argv[])
{
	DWORD thID;
	wchar_t port[15];
	if(argc == 1)
	{
		wcscpy(port,L"COM4");
	}
	if(argc == 8)
	{
		wcscpy(port,L"COM5");
	}
	int rate = 57600;

	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	GetStartupInfo(&si);


	bool res = sg.Open((LPTSTR)port, rate);
	if(res == false)
	{
		printf("Open Error..\n");
		getch();
		return 0;
	}
	else
	{
		printf("Port is ready to post messages\n");
		if(argc == 1) 
			CreateProcess(L"ComPort.exe",L"qgwgweg qgfqwg qwg qwg qwg qw gqwg qwg",NULL,NULL,NULL,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
	}
	CreateThread(NULL,0,Read,NULL,0,&thID);


	while(true)
	{
		char c = getch();
		printf("%c", c);

		sg.Send(&c, sizeof(c));
		if(c == 13) printf("\n");
		if(c == '#') break;
	}


	getch();
	return 0;
}