Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

In my app, I need to detect the device connected to PC via Serial port. In my code, I am checking for all communication port available on PC at 8 different Baudrates. Here is the code below.

C++
HANDLE  hComHandle;
char	szBaudRate[][7] = {"9600","38400", "115200", "57600", 
			   "19200", "4800", "2400", "1200"};
char	cCommand[][3] = {"k" ,"~k"}; 
char	SerialNumberCommand[][4] = {"Ks" ,"~Ks"};
char	cProduct [ ][10] = {"Kproduct", "~Kproduct"} ;
int 	nPortNum;
int 	nTmp;
int     nControlChar;
WCHAR 	szPortName[10];
char 	cReadBuffer;
char	cReadModel[20] = {"Apache"};
char 	cReadSerialNumber [ 22 ];
DWORD 	lpNumberOfBytesWritten = 0;
DWORD 	lpNumberOfBytesRead = 0;
int     nSerialPortPrinter = 0;
int     nPortNumber = 0;
int     nCounter = 0;
int  	nFlag = 0;


DCB     dcbConfig;
COMMTIMEOUTS commTimeout;


for (nPortNum = 0; nPortNum < m_nSerialPorts; nPortNum++ )
{
    MultiByteToWideChar (CP_ACP, 0, cPortNames [nPortNum], 
                         sizeof(cPortNames[nPortNum]),
                         szPortName, sizeof (cPortNames [nPortNum]));
				
    for ( nTmp = 0; nTmp < 8; nTmp++ )
    {
	hComHandle = CreateFileW ((LPCWSTR)szPortName,
                                   GENERIC_READ | GENERIC_WRITE,
                                   0,                  /* Shared Mode */
                                   NULL,               /* Security */
			   	   OPEN_EXISTING,
			   	   FILE_FLAG_OVERLAPPED,
                                   NULL);
	if ( hComHandle == INVALID_HANDLE_VALUE)
	{
	    break;
	}
			
	if(!SetupComm(hComHandle, 1024, 1024))
	{
	    continue;
	}
	
	if(GetCommState(hComHandle, &dcbConfig))
	{
	    dcbConfig.BaudRate = (DWORD)atoi (szBaudRate [ nTmp ]);
	    dcbConfig.ByteSize = 8;
	    dcbConfig.Parity = NOPARITY;
	    dcbConfig.StopBits = 0;
	    dcbConfig.fBinary = TRUE;
	    dcbConfig.fParity = TRUE;
	}

	else
	    continue;

	if(!SetCommState(hComHandle, &dcbConfig))
	    continue;
	
        if(GetCommTimeouts(hComHandle, &commTimeout))
	{
	    commTimeout.ReadIntervalTimeout = 50;
	    commTimeout.ReadTotalTimeoutConstant = 50;
	    commTimeout.ReadTotalTimeoutMultiplier = 0;
	    commTimeout.WriteTotalTimeoutConstant = 50;
	    commTimeout.WriteTotalTimeoutMultiplier = 0;
	}

	else
	    continue;

	if(!SetCommTimeouts(hComHandle, &commTimeout))
	    continue;

	for (nControlChar = 0; nControlChar < 2; nControlChar++)
	{
	    Sleep ( 2 );
	    WriteFile (hComHandle, cCommand [nControlChar], 
                       2, &lpNumberOfBytesWritten, NULL );
	    Sleep ( 5 ); 
	    ReadFile (hComHandle, &cReadBuffer, 1, &lpNumberOfBytesRead, NULL);
	    
            if (lpNumberOfBytesRead > 0 && cReadBuffer == 'Y' )
	    {
                nFlag = 1;
            }
        }
   }
}

This code works smoothly on 32 Bit Platforms but not on 64 Bit platform. Functions such as SetupComm(),GetCommState(), SetCommState(), SetCommTimeOutS() fails.
Posted
Comments
Fresher16 2-Jul-13 5:06am    
I am not able to find out what is going wrong?
Please help me out....!!!!!

1 solution

Quote:
This code works smoothly on 32 Bit Platforms but not on 64 Bit platform. Functions such as SetupComm(),GetCommState(), SetCommState(), SetCommTimeOutS() fails.
This is not good info. You should check the error codes you are getting (that is: call GetLastError on failure) and, if you wish, report them here.

Please note on Windows API function failure, you should always perform error checking.
 
Share this answer
 

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