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

ArRobotPacketSender.cpp

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
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 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArDeviceConnection.h"
00030 #include "ArRobotPacketSender.h"
00031 
00038 ArRobotPacketSender::ArRobotPacketSender(unsigned char sync1,
00039                                                   unsigned char sync2) :
00040   myPacket(sync1, sync2)
00041 {
00042   myDeviceConn = NULL;
00043 }
00044 
00051 ArRobotPacketSender::ArRobotPacketSender(
00052         ArDeviceConnection *deviceConnection, unsigned char sync1,
00053         unsigned char sync2) :
00054   myPacket(sync1, sync2)
00055 {
00056   myDeviceConn = deviceConnection;
00057 }
00058 
00059 ArRobotPacketSender::~ArRobotPacketSender()
00060 {
00061 
00062 }
00063 
00064 void ArRobotPacketSender::setDeviceConnection(
00065         ArDeviceConnection *deviceConnection)
00066 {
00067   myDeviceConn = deviceConnection;
00068 }
00069 
00070 ArDeviceConnection *ArRobotPacketSender::getDeviceConnection(void)
00071 {
00072   return myDeviceConn;
00073 }
00074 
00075 bool ArRobotPacketSender::connValid(void)
00076 {
00077   return (myDeviceConn != NULL && 
00078           myDeviceConn->getStatus() == ArDeviceConnection::STATUS_OPEN);
00079 }
00080 
00085 bool ArRobotPacketSender::com(unsigned char number)
00086 {
00087   if (!connValid())
00088     return false;
00089 
00090   myPacket.empty();
00091   myPacket.setID(number);
00092 
00093   myPacket.finalizePacket();
00094   
00095   return myDeviceConn->write(myPacket.getBuf(), myPacket.getLength());
00096 }
00097 
00103 bool ArRobotPacketSender::comInt(unsigned char command, 
00104                                           short int argument)
00105 {
00106 
00107   if (!connValid())
00108     return false;
00109 
00110   myPacket.empty();
00111   myPacket.setID(command);
00112   if (argument >= 0) 
00113   {
00114     myPacket.uByteToBuf(INTARG);
00115   }
00116   else 
00117   {
00118     myPacket.uByteToBuf(NINTARG);
00119     argument = -argument;
00120   }
00121   myPacket.uByte2ToBuf(argument);
00122 
00123   myPacket.finalizePacket();
00124 
00125   if (myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00126     return true;
00127 
00128   return false;
00129 
00130 }
00131 
00138 bool ArRobotPacketSender::com2Bytes(unsigned char command, char high,
00139                                              char low)
00140 {
00141   return comInt(command, ((high & 0xff)<<8) + (low & 0xff));
00142 }
00143 
00149 bool ArRobotPacketSender::comStr(unsigned char command, 
00150                                           const char *argument)
00151 {
00152   size_t size;
00153   if (!connValid())
00154     return false;
00155 
00156   myPacket.empty();
00157   
00158   myPacket.setID(command);
00159   myPacket.uByteToBuf(STRARG);
00160 
00161   size = strlen(argument);
00162   if (size > 200)
00163     return false;
00164 
00165   myPacket.uByteToBuf(size);
00166   myPacket.strToBuf(argument);
00167   
00168   myPacket.finalizePacket();
00169 
00170   if (myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00171     return true;
00172 
00173   return false;
00174   
00175 }
00176 
00183 bool ArRobotPacketSender::comStrN(unsigned char command, 
00184                                            const char *str, int size)
00185 {
00186   if (!connValid())
00187     return false;
00188 
00189   myPacket.empty();
00190   
00191   myPacket.setID(command);
00192   myPacket.uByteToBuf(STRARG);
00193 
00194   myPacket.uByteToBuf(size);
00195   myPacket.strNToBuf(str, size);
00196   
00197   myPacket.finalizePacket();
00198 
00199   if (myDeviceConn->write(myPacket.getBuf(), myPacket.getLength()) >= 0)
00200     return true;
00201 
00202   return false;
00203   
00204 }
00205 
00206 

Generated on Wed Oct 19 12:56:36 2005 for Aria by  doxygen 1.4.0