• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

xid_device_scanner_t.cpp

Go to the documentation of this file.
00001 /* Copyright (c) 2010, Cedrus Corporation
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are
00006  * met:
00007  *
00008  * Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.  
00010  *
00011  * Redistributions in binary form must reproduce the above copyright
00012  * notice, this list of conditions and the following disclaimer in the
00013  * documentation and/or other materials provided with the distribution.
00014  *
00015  * Neither the name of Cedrus Corporation nor the names of its
00016  * contributors may be used to endorse or promote products derived from
00017  * this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include "xid_device_scanner_t.h"
00033 #include "xid_con_t.h"
00034 
00035 #include <boost/shared_ptr.hpp>
00036 
00037 cedrus::xid_device_scanner_t::xid_device_scanner_t(void)
00038 {
00039     load_available_com_ports();
00040 }
00041 
00042 
00043 cedrus::xid_device_scanner_t::~xid_device_scanner_t(void)
00044 {
00045 }
00046 
00047 void cedrus::xid_device_scanner_t::load_available_com_ports()
00048 {
00049     available_com_ports_.clear();
00050     for(int i=1; i < MAX_PORTS; ++i)
00051     {
00052         wchar_t port_name[10];
00053         wsprintf(port_name, L"COM%d", i);
00054 
00055         cedrus::xid_con_t conn(port_name);
00056 
00057         int status = conn.open();
00058         
00059         if(status == NO_ERR)
00060         {
00061             conn.close();
00062             available_com_ports_.push_back(port_name);
00063         }
00064     }
00065 }
00066 
00067 int cedrus::xid_device_scanner_t::detect_valid_xid_devices()
00068 {
00069     load_available_com_ports();
00070 
00071     rb_connections_.clear();
00072     st_connections_.clear();
00073     int devices = 0;
00074 
00075     for(std::vector<std::wstring>::iterator iter = available_com_ports_.begin(),
00076                                               end = available_com_ports_.end();
00077         iter != end; ++iter)
00078     {
00079         bool device_found = false;
00080         const int baud_rate[] = { 115200, 19200, 9600, 57600, 38400 };
00081         const int num_bauds   = sizeof(baud_rate)/sizeof(int);
00082         
00083         for(int i = 0; i < num_bauds && !device_found; ++i)
00084         {
00085             boost::shared_ptr<cedrus::xid_con_t> xid_con(
00086                 new xid_con_t(*iter, baud_rate[i]));
00087 
00088             if(xid_con->open() == NO_ERR)
00089             {
00090                 char return_info[200];
00091                 xid_con->flush_input();
00092                 xid_con->flush_output();
00093                 xid_con->send_xid_command(
00094                     "_c1",
00095                     0,
00096                     return_info,
00097                     sizeof(return_info),
00098                     5,
00099                     1000,
00100                     100);
00101 
00102                 std::string info;
00103                 if(return_info[0] == NULL)
00104                 {
00105                     // there's a possibility that the device is in E-Prime mode.
00106                     // Go through the buffer and discard NULL characters, and
00107                     // only keep the non NULL characters.
00108                     for(int j = 0; j < sizeof(return_info); ++j)
00109                     {
00110                         if(return_info[j] != NULL)
00111                         {
00112                             info.append(&return_info[j], 1);
00113                         }
00114                     }
00115                 }
00116                 else
00117                 {
00118                     info = std::string(return_info);
00119                 }
00120                 
00121                 if(strstr(info.c_str(), "_xid"))
00122                 {
00123                     // found an XID device
00124                     ++devices;
00125 
00126                     device_found = true;
00127 
00128                     if(strcmp(info.c_str(), "_xid0") != 0)
00129                     {
00130                         // device is not in XID mode.  Currently this library
00131                         // only supports XID mode so we issue command 'c10' to
00132                         // set the device into XID mode
00133                         char empty_return[10];
00134                         xid_con->send_xid_command(
00135                             "c10",
00136                             0,
00137                             empty_return,
00138                             sizeof(empty_return),
00139                             0);
00140 
00141                         xid_con->flush_input();
00142                         xid_con->flush_output();
00143                     }
00144 
00145                     char dev_type[10];
00146                     xid_con->send_xid_command(
00147                         "_d2",
00148                         0,
00149                         dev_type,
00150                         sizeof(dev_type),
00151                         1,
00152                         1000);
00153 
00154                     if(dev_type[0] == 'S')
00155                     {
00156                         st_connections_.push_back(xid_con);
00157                     }
00158                     else
00159                     {
00160                         rb_connections_.push_back(xid_con);
00161                     }
00162                 }
00163             }
00164             
00165             xid_con->close();
00166         }
00167     }
00168 
00169     return devices;
00170 }
00171 
00172 boost::shared_ptr<cedrus::xid_con_t> 
00173 cedrus::xid_device_scanner_t::response_device_connection_at_index(unsigned int i)
00174 {
00175     if(i >= rb_connections_.size())
00176         return boost::shared_ptr<xid_con_t>();
00177 
00178     return rb_connections_[i];
00179 }
00180 
00181 boost::shared_ptr<cedrus::xid_con_t>
00182 cedrus::xid_device_scanner_t::stimtracker_connection_at_index(unsigned int i)
00183 {
00184     if( i > st_connections_.size())
00185         return boost::shared_ptr<xid_con_t>();
00186 
00187     return st_connections_[i];
00188 }
00189 
00190 int cedrus::xid_device_scanner_t::rb_device_count() const
00191 {
00192     return rb_connections_.size();
00193 }
00194 
00195 int cedrus::xid_device_scanner_t::st_device_count() const
00196 {
00197     return st_connections_.size();
00198 }

Generated on Wed Dec 15 2010 13:17:12 for XID device library by  doxygen 1.7.2