00001
00002
00003
00004
00005
00006
00008
00021 #include "BHand.h"
00022
00023 #include "BHandCANDriver.h"
00024 #ifdef BH8_262_HARDWARE
00025 #include "BHandSerialDriver.h"
00026 #endif
00027
00029
00030
00031
00033
00034 const int BHAND_NUM_ERROR_MESSAGES = 22;
00035
00039 const static char _ErrorMessage[BHAND_NUM_ERROR_MESSAGES][80] = {
00040 "No error",
00041 "An instance of class BHand has already been initialized",
00042 "Open comm port failed",
00043 "Get comm port state failed",
00044 "Set comm port state failed",
00045 "Set comm timeouts failed",
00046 "Unable to start hand i/o thread",
00047 "Unable to set hand i/o thread priority",
00048 "Error writing to comm port (or write timeout)",
00049 "Error reading from comm port (or read timeout)",
00050 "Hand sent incorrect sequence of characters - possible loss of data",
00051 "Clear comm buffer failed",
00052 "Request to hand i/o thread timed out",
00053 "Processing of previous request is not completed",
00054 "Request pending - in asynch mode",
00055 "No instances of class BHand have been initialized",
00056 "Unrecognized parameter name in call to Set",
00057 "String too long",
00058 "Parameter value out of range (0-30000)",
00059 "Specified motor was not activated in RTStart",
00060 "Hand error - check hand manual",
00061 "Invalid error number"
00062 };
00063
00064
00065 #ifndef LINUX
00066 unsigned int BHand::m_handCount = 0;
00067 #endif
00068
00070
00072
00079 BHand::BHand() : BHandSupervisoryRealtime(this),
00080 m_driver(NULL), m_hardwareDesc(NULL)
00081 {
00082
00083 bhandCreateHardwareDesc();
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 setHardwareDesc(0);
00103
00104 #ifndef LINUX
00105 if (++m_handCount == 1)
00106 {
00107
00108 TIMECAPS tc;
00109 timeGetDevCaps(&tc, sizeof(TIMECAPS));
00110 m_timerRes = MIN(MAX(tc.wPeriodMin, 0), tc.wPeriodMax);
00111 timeBeginPeriod(m_timerRes);
00112 }
00113 #endif
00114 }
00115
00116
00122 BHand::~BHand()
00123 {
00124 #ifndef LINUX
00125 if (--m_handCount == 0)
00126 {
00127
00128 timeEndPeriod(m_timerRes);
00129 }
00130 #endif
00131
00132
00133 bhandDestroyHardwareDesc();
00134 }
00135
00136
00138
00140
00148 int BHand::setDeviceDriver(BHandDriver *driver)
00149 {
00150 if (m_driver == NULL && driver != NULL)
00151 {
00152 m_driver = driver;
00153 return 0;
00154 }
00155 else
00156 {
00157 return -1;
00158 }
00159 }
00160
00161
00167 BHandDriver * BHand::getDeviceDriver()
00168 {
00169 return m_driver;
00170 }
00171
00173
00175
00180 unsigned int BHand::getHardwareNumModels()
00181 {
00182 return bhandGetHardwareNumModels();
00183 }
00184
00190 BHandHardware * BHand::getHardwareDesc(unsigned int hwIndex)
00191 {
00192 return bhandGetHardwareDesc(hwIndex);
00193 }
00194
00200 int BHand::setHardwareDesc(unsigned int hwIndex)
00201 {
00202 if (hwIndex < getHardwareNumModels())
00203 m_hardwareDesc = getHardwareDesc(hwIndex);
00204 else
00205 return -1;
00206
00207 return 0;
00208 }
00209
00214 BHandHardware * BHand::getHardwareDesc()
00215 {
00216 return m_hardwareDesc;
00217 }
00218
00219
00221
00223
00242 void BHand::SetDefaultBaud(unsigned int baud)
00243 {
00244 #ifdef BH8_262_HARDWARE
00245 BHandSerialDriver::SetDefaultBaud(baud);
00246 #endif
00247 }
00248
00249
00257 const char* BHand::ErrorMessage(int err)
00258 {
00259 return (err > 0) ? _ErrorMessage[20] : _ErrorMessage[(err > -20) ? -err : 21];
00260 }