Hi.
I using this Thread "Serial port communication using C++ = "http://www.daniweb.com/forums/thread56329.html"
and I change it for solved the some error in program.
This is my source code:

serial.h
#

define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04 
#include <stdio.h>
#include <time.h> 
#define VC_EXTRALEAN 
#include <string.h> 
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13 
using namespace std;
HANDLE SerialInit(char*, int);
char SerialGetc(HANDLE*);
void SerialPutc(HANDLE*, char);

serial.cpp

//#include <stdafx.h>
//#include <iostream>
//#include <conio.h>
//#include <stdio.h>
//#include <time.h>
//#include <windows.h>
//#include <string.h>
//#include "serial.h"  
//#define FC_DTRDSR 0x01
//#define FC_RTSCTS 0x02
//#define FC_XONXOFF 0x04  
//#define ASCII_BEL 0x07
//#define ASCII_BS 0x08
//#define ASCII_LF 0x0A
//#define ASCII_CR 0x0D
//#define ASCII_XON 0x11
//#define ASCII_XOFF 0x13
//#define CreateFile CreateFileW; // I added this line, for solved this error: error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'	


HANDLE* ptr; // I write this line for solved this error: Run-Time Check Failure #3 - The variable 'ptr' is being used without being defined.

BOOL bPortReady;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
HANDLE SerialInit(char *ComPortName, int BaudRate)  
{
	HANDLE hCom;
	hCom = CreateFile(ComPortName, GENERIC_READ | GENERIC_WRITE,0, NULL,OPEN_EXISTING, 0, NULL);
	bPortReady = SetupComm(hCom, 2, 128); 
	bPortReady = GetCommState(hCom, &dcb);
	dcb.BaudRate = BaudRate;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	dcb.fAbortOnError = TRUE; 
	dcb.fOutX = FALSE; 
	dcb.fInX = FALSE; 
	
	dcb.fOutxCtsFlow = TRUE; 
	dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; 
    
	dcb.fOutxDsrFlow = FALSE; 
	dcb.fDtrControl = DTR_CONTROL_ENABLE; 
	
	
	bPortReady = SetCommState(hCom, &dcb);
	bPortReady = GetCommTimeouts (hCom, &CommTimeouts); 
	CommTimeouts.ReadIntervalTimeout = 5000;
	CommTimeouts.ReadTotalTimeoutConstant = 5000;
	CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
	CommTimeouts.WriteTotalTimeoutConstant = 5000;
	CommTimeouts.WriteTotalTimeoutMultiplier = 1000; 
	bPortReady = SetCommTimeouts (hCom, &CommTimeouts); 
	return hCom;
}
char SerialGetc(HANDLE *hCom)
{
	char rxchar;
	BOOL bReadRC;
	static DWORD iBytesRead;
	bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL); 
	return rxchar;
} 
void SerialPutc(HANDLE *hCom, char txchar)
{
	BOOL bWriteRC;
	static DWORD iBytesWritten;
	bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL); 
	return;
}
int main()
{
	HANDLE my=SerialInit("com2",1200);
	//HANDLE* ptr; 
	char letter;
	while(1)
	{
	
	*ptr=my; // this line have error: Unhandled exception at 0x00411754 in serial.exe: 0xC0000005: Access violation writing location 0x00000000..

	letter=SerialGetc(ptr);
	printf("",letter);
	
	
	
	}
	getch(); 
	return 0; 
}

And I change "*ptr=my" to "ptr=&my" and error is solved.
but I don't sure this change solved my problem beacase I don't resive any thinge.
please help me for solved my problem.
I very very need to solve this problem.

TNX FOR ALL.........

Recommended Answers

All 2 Replies

>>//#define CreateFile CreateFileW; // I added this line, for solved this error: error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'


DON'T DO THAT! CreateFile is declared in windows.h. Fix the error instead of trying to code around it. If you want to use char* then don't compile your program for UNICODE. To do that select Project --> Project Options (bottom of the menu list) --> Configuration Properties --> General. Then on the right side of the screen change Character Set dropdown to Not Set.

Dear Ancient Dragon ,
Thank you so much for your kind cooperation.
please be informed that the problem has been solved and I am realy appreciate you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.