00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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)
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;
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