// serialport.cpp : Defines the entry point for the console application.
#define _CRT_SECURE_NO_DEPRECATE
#include "stdafx.h"
#include <stdio.h>
#include "windows.h"
#include <string.h>
#include <iostream>
#include <time.h>
#include <fstream>
using namespace std;

#define default_port "COM7"
#define special_char		'$'
#define	start_of_frame		's'				// An exoume thn akolou8ia 255 1 tote start of frame
#define mhden_code			'0'				// An exoume thn akolou8ia 255 2 tote no=0
#define	special_char_code	'^'				// An exoume thn akolou8ia 255 3 tote no=255

//http://msdn.microsoft.com/en-us/library/ms810467.aspx

//Global variables
unsigned char szBuff[101];
DWORD dwBytesRead;
DWORD dwBytesWrite;



HANDLE initiate_serial(int argc, char* argv)
{
	int match=12;
	char comport[80];
	if(argc>1){
		strcpy_s(comport,argv);
		printf("Comport Selected= %s\n",comport);
	}
	else{
		strcpy_s(comport,default_port);
		printf("Default comport = %s\n",comport);
	}

	HANDLE hSerial;
//	hSerial = CreateFile(comport,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	hSerial = CreateFile(comport,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); //http://forums.msdn.microsoft.com/en/vcgeneral/thread/fe930513-fa09-48cc-bd51-d77fda017d7b/
	

	if(hSerial==INVALID_HANDLE_VALUE){
		if(GetLastError()==ERROR_FILE_NOT_FOUND){
		//serial port does not exist. Inform user.
			printf("serial port does not exist\n");
		}
		printf("some other error occurred\n");
	//some other error occurred. Inform user.
	}


	DCB dcbSerialParams = {0};
	dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
	if (!GetCommState(hSerial, &dcbSerialParams)) {
		printf("error getting state\n");
		//error getting state
	}
	dcbSerialParams.BaudRate=CBR_38400;
	dcbSerialParams.ByteSize=8;
	dcbSerialParams.StopBits=ONESTOPBIT;
	dcbSerialParams.Parity=NOPARITY;
	if(!SetCommState(hSerial, &dcbSerialParams)){
		printf("error setting serial port state\n");
		//error setting serial port state
	}
	
	COMMTIMEOUTS timeouts={0};
	timeouts.ReadIntervalTimeout=MAXDWORD;//http://www.mombu.com/microsoft/windows-programmer-win32/t-non-blocking-serial-reads-in-winxp-109667.html
	timeouts.ReadTotalTimeoutConstant=0;//50;
	timeouts.ReadTotalTimeoutMultiplier=0;//10;
	timeouts.WriteTotalTimeoutConstant=0;
	timeouts.WriteTotalTimeoutMultiplier=0;
	if(!SetCommTimeouts(hSerial, &timeouts)){
		printf("Timeout error occureed. Inform user\n");
	//printf("error occureed. Inform user")
	}
	//int n=200;
	//unsigned char *szBuff;
	//szBuff= new unsigned char[101];
	return hSerial;
}


void write_read_serial(HANDLE hSerial,unsigned char tx_data[32]){
	//unsigned char szBuff[101];
	
	char * pch;

	//DWORD dwBytesRead = 0;
	//DWORD dwBytesWrite = 0;
	
	unsigned char output[65];
	//unsigned char tx_data[32];
	
	unsigned char servo=64;
	unsigned char offset=0;
	unsigned char temp_char='a';
	int output_counter=0;
	int tx_counter=2;
	// time
	time_t start,end;
	double time_passed;
	
	//FILE * pFile;
	int n;
	char name [100];
	//pFile = fopen ("myfile.txt","w");

	time (&start);
	time (&end);
	time_passed = difftime (end,start);

	output[0]=special_char;
	output[1]=start_of_frame;
	tx_data[0]=special_char;
	tx_data[1]=start_of_frame;
	//tx_data[2]=75;
	//tx_data[3]=75;
	//tx_data[4]=75;
	//tx_data[5]=75;
	//tx_data[6]=75;
	//tx_data[7]=75;
	//tx_data[8]=75;
	//tx_data[9]=75;

	/*
	tx_counter=2;
	output_counter=2;
	while(tx_counter<3){
		switch (tx_data[tx_counter]){
			case 0:
				output[output_counter]=special_char;
				output_counter++;
				output[output_counter]=mhden_code;
			break;		
			case special_char:
				output[output_counter]=special_char;
				output_counter++;
				output[output_counter]=special_char_code;
			break;
			default:
				output[output_counter]=tx_data[tx_counter];
			break;
				
		}// end switch
		tx_counter++;
		output_counter++;
	}

	
	if(!WriteFile(hSerial, output, 60, &dwBytesWrite, NULL)){
		//error occurred. Report to user.
		//printf("Write error\n");			
	}
	//printf("dwBytesWrite=%d\n",dwBytesWrite);
	*/
	int k=0;
	for(k=0;k<101;k++)
		szBuff[k]=0;	
	if(!ReadFile(hSerial, szBuff, 70, &dwBytesRead, NULL)){
		//error occurred. Report to user.
		//printf("Read error\n");				
	}

	//fprintf (pFile, "%s1\n",szBuff);
	//printf ("%s\n",szBuff);
	//CloseHandle(hSerial);		
	//fclose (pFile);	// Close File handle

}




/*

 Default  Non-blocking serial reads in WinXP

In article <1128904190.655698.80830@g14g2000cwa.googlegroups. com>,
john.puzzled@gmail.com (john.puzzled) writes:


I do it all the time; I'll describe how my approach differs from yours.


Change FILE_FLAG_NO_BUFFERING to FILE_ATTRIBUTE_NORMAL.

Change MAXWORD to MAXDWORD - it might not make a difference,
but it's the proper thing to do according to the SDK.

I don't bother calling GetCommTimeouts() to get the existing
settings; I just make up a brand new set:

memset ((void *) &commTimeouts, 0, sizeof (commTimeouts));
commTimeouts.ReadIntervalTimeout = MAXDWORD;
commTimeouts.ReadTotalTimeoutMultiplier = 0;
commTimeouts.readTotalTimeoutConstant = 0;
commTimeouts.WriteTotalTimeoutMultiplier = 0;
commTimeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (comPort, &commTimeouts);

That way I know that everything is exactly the way I want it.

If "len" isn't a DWORD, cast it just to be safe. I presume
"bytesRead" is a DWORD as well.


Try the suggestions above. It works fine for me.

-- 
/~\ cgibbs@kltpzyxm.invalid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!
*/