Click here to Skip to main content
14,984,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one i am working on serialport programm , but while running programmin its give the following error. Can any one please help me out please

C++
/** Serial.cpp
 *
 *
 */

#include <iostream>
using namespace std;
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>

#include "serialtestdll.h"

int WINAPI _tWinMain
          (
           HINSTANCE /*hInst*/, 
           HINSTANCE /*hInstPrev*/, 
           LPTSTR    /*lptszCmdLine*/, 
          int       /*nCmdShow*/
          ){
}

Serial::Serial(tstring &commPortName, int baudeRate)
{
	commHandle = CreateFile(commPortName.c_str(), GENERIC_READ|GENERIC_WRITE, 0,NULL, OPEN_EXISTING, 
		0, NULL);

	if(commHandle == INVALID_HANDLE_VALUE) 
	{
		throw("ERROR: Could not open com port");
	}
	else 
	{
		// set timeouts
		COMMTIMEOUTS cto = { MAXDWORD, 0, 10,100, 500};
		DCB dcb;
		if(!SetCommTimeouts(commHandle,&cto))
		{
			Serial::~Serial();
			throw("ERROR: Could not set com port time-outs");
		}

		// set DCB
		memset(&dcb,0,sizeof(dcb));
		dcb.DCBlength = sizeof(dcb);
		dcb.BaudRate = 9600;
		dcb.fBinary = 1;
		dcb.fDtrControl = DTR_CONTROL_ENABLE;
		dcb.fRtsControl = RTS_CONTROL_ENABLE;

		dcb.Parity = NOPARITY;
		dcb.StopBits = ONESTOPBIT;
		dcb.ByteSize = 8;

		if(!SetCommState(commHandle,&dcb))
		{
			Serial::~Serial();
			throw("ERROR: Could not set com port parameters");
		}
	}
}

Serial::~Serial()
{
	CloseHandle(commHandle);
}

double Serial::write(const char *buffer)
{
	char cmd[] = "!GV\r";
	DWORD numWritten;
	WriteFile(commHandle, cmd, 5, &numWritten, NULL); 

	return numWritten;
}

int Serial::write(const char *buffer, int buffLen)
{
	char cmd[] = "!GV\r";
	DWORD numWritten;
	
	WriteFile(commHandle, cmd, 5, &numWritten, NULL); 

	return numWritten;
}

int Serial::read(char *buffer, int buffLen, bool nullTerminate)
{
	DWORD numRead;
	if(nullTerminate)
	{
	--buffLen;
	}

	BOOL ret = ReadFile(commHandle, buffer, buffLen, &numRead, NULL);

	if(!ret)
	{
		return 0;
	}

	if(nullTerminate)
	{
		buffer[numRead] = '\r';
	}

	return numRead;
}

#define FLUSH_BUFFSIZE 10

void Serial::flush()
{
	char buffer[FLUSH_BUFFSIZE];
	int numBytes = read(buffer, FLUSH_BUFFSIZE, false);
	while(numBytes != 0)
	{
		numBytes = read(buffer, FLUSH_BUFFSIZE, false);
	}
}
Posted
Updated 8-Sep-15 22:39pm
v2
Comments
Jochen Arndt 9-Sep-15 4:40am
   
What error?
"native has exited with code 0(0x0)" tells you that your program has terminated with code zero which usually indicates success.
CPallini 9-Sep-15 5:04am
   
Your program does nothing. However, it does nothing successfully.

Hey Thankyou every one i realised my error and found my solution
   
Hello every one i am very new to programming language in c++ please do let me know how can i cerate an instance
   
Comments
CPallini 9-Sep-15 5:15am
   
You should post another question. Anyway I stringly suggest you to read a good tutorial or book on C++ programming language.
To add to what Jochen Arndt says: when you get an app return code of zero (as you do) that means "Program completed successfully" - any non-zero value is an error code.

Your application entry point _tWinMain does nothing, so it completes successfully and you get a zero return code. Defining a class does not create an instance of it, nor does any of the code in that class get executed unless your code specifically instructs that it should.
   
Comments
CPallini 9-Sep-15 5:03am
   
And yes again, 5.
Your _tWinMain function is empty and returns immediately. You must add some code to the function body.
   
Comments
OriginalGriff 9-Sep-15 4:52am
   
:thumbsup:
CPallini 9-Sep-15 5:03am
   
Yes, 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900