Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArRobotPacketSender.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArDeviceConnection.h"
00029 #include "ArRobotPacketSender.h"
00030 
00037 AREXPORT ArRobotPacketSender::ArRobotPacketSender(unsigned char sync1,
00038                           unsigned char sync2) :
00039   myPacket(sync1, sync2)
00040 {
00041   myDeviceConn = NULL;
00042 }
00043 
00050 AREXPORT ArRobotPacketSender::ArRobotPacketSender(
00051     ArDeviceConnection *deviceConnection, unsigned char sync1,
00052     unsigned char sync2) :
00053   myPacket(sync1, sync2)
00054 {
00055   myDeviceConn = deviceConnection;
00056 }
00057 
00058 AREXPORT ArRobotPacketSender::~ArRobotPacketSender()
00059 {
00060 
00061 }
00062 
00063 AREXPORT void ArRobotPacketSender::setDeviceConnection(
00064     ArDeviceConnection *deviceConnection)
00065 {
00066   myDeviceConn = deviceConnection;
00067 }
00068 
00069 AREXPORT ArDeviceConnection *ArRobotPacketSender::getDeviceConnection(void)
00070 {
00071   return myDeviceConn;
00072 }
00073 
00074 bool ArRobotPacketSender::connValid(void)
00075 {
00076   return (myDeviceConn != NULL && 
00077       myDeviceConn->getStatus() == ArDeviceConnection::STATUS_OPEN);
00078 }
00079 
00084 AREXPORT bool ArRobotPacketSender::com(unsigned char number)
00085 {
00086   if (!connValid())
00087     return false;
00088 
00089   myPacket.empty();
00090   myPacket.setID(number);
00091 
00092   myPacket.finalizePacket();
00093   
00094   return myDeviceConn->write(myPacket.getBuf(), myPacket.getLength());
00095 }
00096 
00102 AREXPORT bool ArRobotPacketSender::comInt(unsigned char command, 
00103                       short int argument)
00104 {
00105 
00106   if (!connValid())
00107     return false;
00108 
00109   myPacket.empty();
00110   myPacket.setID(command);
00111   if (argument >= 0) 
00112   {
00113     myPacket.uByteToBuf(INTARG);
00114   }
00115   else 
00116   {
00117     myPacket.uByteToBuf(NINTARG);
00118     argument = -argument;
00119   }
00120   myPacket.uByte2ToBuf(argument);
00121 
00122   myPacket.finalizePacket();
00123 
00124   if (myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00125     return true;
00126 
00127   return false;
00128 
00129 }
00130 
00137 AREXPORT bool ArRobotPacketSender::com2Bytes(unsigned char command, char high,
00138                          char low)
00139 {
00140   return comInt(command, ((high & 0xff)<<8) + (low & 0xff));
00141 }
00142 
00149 AREXPORT bool ArRobotPacketSender::comStr(unsigned char command, 
00150                       const char *argument)
00151 {
00152   size_t size;
00153   if (!connValid())
00154     return false;
00155   size = strlen(argument);
00156   if (size > 199) // 200 - 1 byte for length
00157     return false;
00158 
00159   myPacket.empty();
00160   
00161   myPacket.setID(command);
00162   myPacket.uByteToBuf(STRARG);
00163   myPacket.uByteToBuf(size);
00164   myPacket.strToBuf(argument);
00165   
00166   myPacket.finalizePacket();
00167 
00168   if (myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00169     return true;
00170 
00171   return false;
00172   
00173 }
00174 
00183 AREXPORT bool ArRobotPacketSender::comStrN(unsigned char command, 
00184                        const char *str, int size)
00185 {
00186   if (!connValid())
00187     return false;
00188 
00189   if(size > 199) return false;   // 200 - 1 byte for length
00190 
00191   myPacket.empty();
00192   
00193   myPacket.setID(command);
00194   myPacket.uByteToBuf(STRARG);
00195 
00196   myPacket.uByteToBuf(size);
00197   myPacket.strNToBuf(str, size);
00198   
00199   myPacket.finalizePacket();
00200 
00201   if (myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00202     return true;
00203 
00204   return false;
00205   
00206 }
00207 
00208 AREXPORT bool ArRobotPacketSender::comDataN(unsigned char command, const char* data, int size)
00209 {
00210   if(!connValid()) return false;
00211   if(size > 200) return false;
00212   myPacket.empty();
00213   myPacket.setID(command);
00214   myPacket.uByteToBuf(STRARG);
00215   myPacket.strNToBuf(data, size);
00216   myPacket.finalizePacket();
00217   if(myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00218       return true;
00219   return false;
00220 }
00221 
00222 

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0