Page 209 - PDA Robotics Using Your Personal Digital Assistant to Control Your Robot
P. 209

PDA 09  5/27/03  8:50 AM  Page 185
                                            Chapter 9 / PDA Robot Software for Pocket PC 2002
                                   // to read the data by calling ioctlsocket. This will indicate how much data
                                   // is in the buffer as well.
                                   //
                                    ret = ioctlsocket (Infrared_Socket, FIONREAD, &numbytes);
                                    if( (ret == 0) && (numbytes > 0) )
                                    {
                                      //
                                      // Receive what is in the buffer and set the
                                      // range edit box
                                      //
                                      ret = recv ( Infrared_Socket, &irda_buffer[0], 26, 0);
                                      this->m_range.SetWindowText( (LPCTSTR) CString(irda_buffer));
                                    }
                                 }
                                 To close the IrDA link, press the Disconnect button and the following
                                 function is called. It, in turn, calls CloseIrdaSocket listed below.

                                 void CPDABotDlg::OnCloseIrda()
                                 {
                                   CloseIrdaSocket();
                                 }

                                 void CPDABotDlg::CloseIrdaSocket()
                                 {

                                   //
                                   // Purge the receive buffer and close the Socket to disconnect.
                                   //

                                   char irda_buffer[128];
                                   int ret;
                                   u_long numbytes;

                                   //
                                   // Ensure that we won't be blocked waiting here on the function
                                   // to read the data by calling ioctlsocket. This will indicate how much data
                                   // is in the buffer as well.
                                   //

                                   ret = ioctlsocket (Infrared_Socket, FIONREAD, &numbytes);
                                   if( (ret == 0) && (numbytes > 0) )
                                   {
                                      ret = recv ( Infrared_Socket, &irda_buffer[0], numbytes, 0);
                                   }
                                   ret = closesocket(Infrared_Socket);

                                   //
                                   // Set the member variable of this class that we use to  determine our status
                                   // of the link

                                                                                               185
   204   205   206   207   208   209   210   211   212   213   214