Click here to Skip to main content
6,630,586 members and growing! (19,612 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

How to establish communication between mobile phone and PC

By nitzbajaj

This article deals with building a communication link between your mobile and your PC. It also deals with the ways to execute commands on your mobile phone interpreter from your PC.
VC6, Windows, Win Mobile, Mobile, Dev
Posted:31 Oct 2004
Views:109,181
Bookmarked:73 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 3.90 Rating: 2.61 out of 5
10 votes, 32.3%
1
2 votes, 6.5%
2
1 vote, 3.2%
3
8 votes, 25.8%
4
10 votes, 32.3%
5

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:

  1. serial port (requires a data cable).
  2. infra red port (requires infrared port on your mobile and PC).
  3. Bluetooth (requires Bluetooth wireless connector).

(Note: I have just worked on serial port and infrared.)

Explanation

Initialize and open port:

/* portNm =COM1,COM2 etc which ever COM port your phone is connected */

bool OpenPort( char *portNm )  
{    
if( portNm != NULL ) // check to if port exists

    {
/* hComm is handle that makes a buffer file pointin to your
   COM port for read and write of port data */

    hComm = CreateFile( portNm,GENERIC_READ | GENERIC_WRITE, 
                        0, 
                        0, 
                        OPEN_EXISTING,
                        NULL,
                        0);

    if (hComm == INVALID_HANDLE_VALUE)
        {
        //error occured alert user about error            

        return false;
        }
    else
            // port opened successfully alert user

       return true;
    }

else
    {
      // specified port missing alert user

          return false;
    }

}

Write To Port:

BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
   OVERLAPPED osWrite = {0};
   DWORD dwWritten;
   BOOL fRes;

   // Issue write.

   if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, NULL)) 
   {
      if (GetLastError() != ERROR_IO_PENDING) 
      {  // WriteFile failed, but it isn't delayed. Report error and abort.

      
          fRes = FALSE;
        }
      else
      {
         // Write is pending.


         if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
         {     
             fRes = FALSE;}
         else
         {  // Write operation completed successfully.


             fRes = TRUE;}
      }
   }
   else
      // WriteFile completed immediately.


      fRes = TRUE;
  
   return fRes;
}

Read Port:

void ReadPort()

{
    DWORD dwRead;        
    char  chRead[250];  

// remember to adjust 250 according to the total data read

       
         char *count = NULL;
     
/* flush the old values and fill with some arbitary symbol 
   thats most unlikely to appear in your data to know
   the end point after reading */

    memset(chRead,'\x91',250);  

    Sleep( 5L);

    ReadFile(hComm, chRead, 250, &dwRead, NULL); 

/* now chRead contains data . you can manipulate
   it accoring to your needs now. */

}

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"))  // opens COM4 port

{
  DCB dcb
 
        dcb.fOutxCtsFlow = false;    // Disable CTS monitoring

        dcb.fOutxDsrFlow = false;    // Disable DSR monitoring

    dcb.fDtrControl = DTR_CONTROL_DISABLE;    // Disable DTR monitoring

    dcb.fOutX = false;        // Disable XON/XOFF for transmission

    dcb.fInX = false;        // Disable XON/XOFF for receiving

    dcb.fRtsControl = RTS_CONTROL_DISABLE;    // Disable RTS (Ready To Send)

    
    

   COMMTIMEOUTS timeouts;

    timeouts.ReadIntervalTimeout = 20; 
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.ReadTotalTimeoutConstant = 100;
    timeouts.WriteTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 100;

    if (!SetCommTimeouts(hComm, &timeouts))
    {
      // error with time outs .alert user and exit

    }

}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

nitzbajaj


Member

Some projects of interest completed :

1> AUTOMATED SMS RESPONSE SYSTEM : A complete software in VC++ . Replies(sending an SMS via mobile phone connected at serial port) automatically to the user who queries(received as SMS on mobile phone connected at serial port) by searching the database and sending back the appropriate reply.User can query from anywhere in the world.Can be used for various concepts like query airline reservations status,movie tickets,weather reports etc.

2> SMART HOME - THORUGH SMS : The concept of SMART HOME implemented through SMS. Automatically swicthes off/on any electric equipment connected to PC(designed a hardware relay circuit for that) just by sending an SMS from anywhere to the mobile phone connected to your PC

For any kind of help or guidance plz free to contact me at nitzbajaj@yahoo.com
Location: United States United States

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
Generalplease help me in this project Pinmemberarivu_web16:52 18 Feb '09  
Generalproblem in reading in data from mobile through serial com Pinmembermanju23reddy21:07 4 Sep '08  
Generalhelpme in sms bluetooth Pinmembermohammed qaid8:37 29 Apr '08  
QuestionError in code PinmemberD. PAUL23:10 20 Nov '07  
GeneralFor Project PinmemberNkeyur9:33 29 Jun '07  
GeneralAT command help! PinmemberLeo_13314:28 21 Jun '07  
Questionpleas help me Pinmemberwaseem_brhoom1236:58 12 May '07  
QuestionMY project iscontrolling home via sms [modified] Pinmembernaveed_kaan10:29 1 May '07  
Generalhelp me please!!!!! Pinmemberdinhtrungac23:26 17 Apr '07  
Generalmy project is smart house through sms message Pinmember16:38 1 Mar '07  
Generalhelp me PinmemberNkeyur9:53 29 Jun '07  
Generalcommunication b/w mobile n pc thru sms Pinmembersushant_865:36 30 Jan '07  
GeneralSending SMS's using this code Pinmemberxaml.net3:25 13 Jan '07  
Generalhelp, for code in C, for mobile to PC communication Pinmembersadafp8:16 30 Jul '06  
QuestionHelp in communication b/w mobile n PC PinmemberNauman Rahim21:08 26 Jul '06  
AnswerRe: Help in communication b/w mobile n PC Pinmembersadafp8:18 30 Jul '06  
QuestionRe: Help in communication b/w mobile n PC Pinmembersmah802:27 20 Dec '06  
AnswerRe: Help in communication b/w mobile n PC Pinmembergururaghu13:00 24 Feb '07  
GeneralHelp in communication b/w mobile n PC PinmemberNkeyur9:43 29 Jun '07  
GeneralHow to detect the IR port Pinmemberrahul_s99915:00 6 Apr '06  
Generaldatabase access from a PC using a smartphone or pda or pocket PC via bluetooth Pinmembernishitkumar0:43 4 Apr '06  
Generalcommunication using data cable PinmemberBHARATH RAJ M N17:38 4 Mar '06  
GeneralHow to detect an incomming call on Smart Phones PinmemberZubair Mindfire1:22 4 Mar '06  
Questionhelp needed in project Pinmemberashish_raghu23:05 28 Feb '06  
Generalinfo required Pinmemberadithya K.N23:44 23 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by nitzbajaj
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project