Welcome!

PowerBuilder Authors: Dan Joe Barry, Carmen Gonzalez, Ian Thain, Yakov Werde, Paul Slater

Related Topics: PowerBuilder

PowerBuilder: Article

Adding Caller ID to Your PowerBuilder Applications

A how-to

How did I know the values of the hexadecimal constants to begin with? There are many sources but one I find useful is a tool called ApiViewer. It's invaluable when it comes to prototyping Win32 API calls. A link to it can be found at the bottom in the references section.

A further look at the prototype for CreateFile sheds some light onto the other constants, in particular 3, 0, 3. Again, if we look at the prototype for CreateFile, we see that these represent the ShareMode, SecurityAttributes, and CreateDisposition, respectively. For the ShareMode we are interested in FILE_SHARE_READ | FILE_SHARE_WRITE. Again, look at APIViewer to find out their hex values, or them together and convert to yield decimal 3. For the SecurityAttributes, this represents a pointer to a SECURITY_ATTRIBUTES structure that determines whether or not the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited. This is what is desired in our case, thus the zero. Finally, a word about the CreateDisposition, which is an action to take on files that do and do not exist. With respect to a comport, we expect it to exist. Therefore, the value OPEN_EXISTING is in order. Again, use the ApiViewer tool to see the numeric values of this constant.

Back to our project, the next order of business is to set the modem up to capture caller-ID information. The command for doing this may vary depending on your manufacture; consult your modem documentation. I was using a Toshiba software modem. With respect to the call to WriteFile, ls_write is the text to send to the modem, ll_write is the length of the text sent to the modem, and ll_written is the length actually written to the modem.

//turn on caller-ID

String ls_write
Long ll_write
Long ll_written
Long ll_rc

    ls_write = "AT#CID=1" + Char(13)
    ll_write = Len(ls_write)

    ll_rc = WriteFile( il_hcon, ls_write, ll_write, ll_written, Long(0) )

    Return True

Now all we have to do is wait for an incoming phone call and pick off the data. A couple of points are important here. One, the string that indicates the information is the phone number can vary depending on your particular modem. Again, experiment or consult your modem documentation. The second thing is that the data is returned with carriage returns and line feeds. While nice to look at, it can make parsing more difficult. Therefore, I've replaced them with spaces. With respect to the call to ReadFile, it follows the same logic as WriteFile(see above). Here is what the function to pick off the caller id looks like:

String ls_read
Long ll_Read
Integer ll_rc
Integer ll_i
String ls_Char
String ls_Result
String ls_Number

do while not Match(ls_result, 'NMBR')
    ls_read = SPACE(100)

    ll_rc = ReadFile( il_hcon, ls_read, len(ls_read), ll_read, Long(0) )

    //do some parsing
    For ll_i = 1 to Len(Trim(ls_read))
      ls_Char = Mid(ls_read, ll_i, 1)
      If ls_Char = Char(10) Or ls_Char = Char(13) Then
        ls_Result += ' '
      Else
        ls_Result += ls_Char
      End If
    Next

Loop
//here we got our string so pick off number
ls_Number = Mid(ls_Result, Pos(ls_Result, 'NMBR') + 5, 10)

sle_number.Text = ls_Number

Finally, in the close event we will need to make sure we close the open comm port.

CloseHandle(il_hCon)

Return True

That's it! Incredibly simple but powerful coupled with a database of phone numbers you can query to personalize your applications for your end users.
Happy Coding!

Resources

  • How does Caller ID work? How is the caller's phone number sent to my phone?
    http://electronics.howstuffworks.com/question409.htm
  • Toshiba V.92 56k Internal Modem User's Guide
    http://cdgenp01.csd.toshiba.com/content/support/downloads/v_92_manual_20020815.pdf
  • Sybase Developer Network/CodeExchange www.sybase.com/sdn
  • Chance's Blog http://chance.pbdjmagazine.com/
  • More Stories By Deanne M. Chance

    Ms. Chance graduated in 1996 with a degree in computer science from the University of Illinois. She has been a frequent contributor to the PowerBuilder Developer's Journal and gave a key presentation at Sybase TechWave 2005 entitled "A Real-Time Physical Inventory Solution Using PocketBuilder ASA and a WiFi Connection." She has held several engineering positions, starting a career at Motorola where she focused on mobile I.P. by doing real-time embedded programming for the base radio controller group as part of the iDEN/Nextel project.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.