Introduction
The article provides a step by step tutorial about the method to connect your mobile phone to your PC using serial communication. It describes the methods to execute phone specific commands via your PC.
PC and mobile phone communication can be established by three ways:
- serial port (requires a data cable).
- infra red port (requires infrared port on your mobile and PC).
- Bluetooth (requires Bluetooth wireless connector).
(Note: I have just worked on serial port and infrared.)
Explanation
Initialize and open port:
bool OpenPort( char *portNm )
{
if( portNm != NULL ) {
hComm = CreateFile( portNm,GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
NULL,
0);
if (hComm == INVALID_HANDLE_VALUE)
{
return false;
}
else
return true;
}
else
{
return false;
}
}
Write To Port:
BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
OVERLAPPED osWrite = {0};
DWORD dwWritten;
BOOL fRes;
if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, NULL))
{
if (GetLastError() != ERROR_IO_PENDING)
{
fRes = FALSE;
}
else
{
if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
{
fRes = FALSE;}
else
{
fRes = TRUE;}
}
}
else
fRes = TRUE;
return fRes;
}
Read Port:
void ReadPort()
{
DWORD dwRead;
char chRead[250];
char *count = NULL;
memset(chRead,'\x91',250);
Sleep( 5L);
ReadFile(hComm, chRead, 250, &dwRead, NULL);
}
Special setting:
After you call OpenPort
, you must assign the DCB and connection timeout to the port. DCB is used to adjust communication settings of the port. You must set your port according to your needs depending upon your communication requirement.
Connection timeouts are used to assign the time for which port must forcibly stop reading data from your mobile phone to prevent abnormality in case of very long data.
if(OpenPort("COM4")) {
DCB dcb
dcb.fOutxCtsFlow = false; dcb.fOutxDsrFlow = false; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fOutX = false; dcb.fInX = false; dcb.fRtsControl = RTS_CONTROL_DISABLE;
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 20;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 100;
if (!SetCommTimeouts(hComm, &timeouts))
{
}
}
The above piece of code can be used to establish effective communication between a PC and a mobile phone.
You can issue phone specific commands to the phone interpreter which will do the processing and will return data on same port after processing. Hayes AT commands can be used with most of the phones (these are phone specific, so please check for the compatibility of these commands with your phone ). Nokia AT command set is available at Nokia official website.
AT commands can be used to perform various functions like sending/receiving SMS to your PC connected to mobile phone, synchronizing the mobile phone book, changing phone settings, and lots more.
Hope the article proves to be useful, and for any other query, you can always contact me at nitzbajaj@yahoo.com.