Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
#include <stdio.h>
#include <windows.h>

void main()
{
	DWORD dwBytesRead, dwBytesWritten, dwPos;
	char c[7];
	LPWSTR port_name=malloc(5);
	int cnt,i,len;
	DCB obj_dcb;
	int m_baud_rate=CBR_110,  m_byte_size=1;

	HANDLE fd= CreateFile(port_name,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
	
	
	if (fd == NULL)
		{ 
			printf("file doesnt exist");
		}
	else
	{
		if(GetCommState(fd,&obj_dcb)==0)
			{
				printf("get has problem");
			//	return FALSE;
		}
obj_dcb.BaudRate						= m_baud_rate;

	obj_dcb.DCBlength						= sizeof(obj_dcb);

	obj_dcb.Parity                          = 0;
	obj_dcb.ByteSize                        = m_byte_size;
	obj_dcb.StopBits                        = ONESTOPBIT ;
	obj_dcb.fBinary                         = TRUE;
	obj_dcb.fParity                         = FALSE;
	obj_dcb.fDsrSensitivity                 = TRUE;
	obj_dcb.fTXContinueOnXoff               = FALSE;
	obj_dcb.fOutX                           = FALSE;
	obj_dcb.fInX                            = FALSE;
	obj_dcb.fErrorChar                      = FALSE;
	obj_dcb.fNull                           = TRUE;
	obj_dcb.fRtsControl                     = RTS_CONTROL_ENABLE;
	obj_dcb.fAbortOnError                   = TRUE;
	if(SetCommState(fd,&obj_dcb)==0)
		{	printf("eror in port");
//			return FALSE;
		}
		
			for(i=0;i<=4 ;i++)
					{	
						c[i] = getchar();	
					}
						c[i] = '\0';

				dwBytesWritten=strlen(c) ;
						
				WriteFile (fd,c,dwBytesWritten,&dwBytesWritten, NULL);
			
		
	}
			c[0] = '\0';

	
				CloseHandle(fd);
		
				fd= CreateFile(TEXT("d:\\temp.txt"),GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
	
	if(fd == NULL)
		{	
			printf("do exist");
		}
	else
		{
				dwBytesRead=sizeof(char);
				ReadFile (fd, c, 1, &dwBytesRead, NULL);			
				printf("%s",c);
		}
	
}


[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 18-Apr-12 23:07pm
v4
Comments
Prasad_Kulkarni 19-Apr-12 5:00am    
Where's error & Where's your question??
OriginalGriff 19-Apr-12 5:00am    
And the problem is?
I see your subject, I see a code dump.
You forgot to tell us what the problem is.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
OriginalGriff 19-Apr-12 5:02am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
heenagoyal11 19-Apr-12 5:06am    
the error is it is giving the output of get has eror and port has error

This line contains multiple errors:
C++
CreateFile(port_name,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);


  1. port_name is not initialized
  2. dwCreationDisposition must be OPEN_EXISTING for serial ports
  3. dwFlagsAndAttributes should be 0 for serial ports

Try
C++
LPWSTR port_name = L"COM1";
CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

I did not check the remaining code for other errors.

[UPDATE checking for more errors]

The byte size is a bit count:
C++
// Wrong
//int m_byte_size = 1;
// Correct is 7 or 8
int m_byte_size = 8;

CreateFile returns INVALID_HANDLE_VALUE upon errors:
C++
// Don't use this
//if (fd == NULL)
// But this
if (fd == INVALID_HANDLE_VALUE)
 
Share this answer
 
v2
Comments
heenagoyal11 19-Apr-12 5:24am    
ok.i have made these chnges.now Output is pot has problem.
Jochen Arndt 19-Apr-12 5:34am    
See my updated solution.
You did not assign a port name.
You just do
C++
LPWSTR port_name=malloc(5);
and some lines later you try to open the port without assigning a Com-Port.

For the first test on COM1 just try
C++
HANDLE fd = CreateFile( _T("\\\\.\\COM1"),  
                    GENERIC_READ | GENERIC_WRITE, 
                    0, 
                    0, 
                    OPEN_EXISTING,
                    0,
                    0);


Take a look at CreateFile function[^]

and Serial Communications in Win32[^]

Hope it helps..
 
Share this answer
 
Comments
heenagoyal11 19-Apr-12 5:28am    
what is_T?
Andy411 19-Apr-12 5:40am    
It's a macro like TEXT
heenagoyal11 19-Apr-12 6:04am    
but is creating error in my code?
Andy411 20-Apr-12 3:07am    

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