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 "ArSickPacket.h"
00029 #include "stdio.h"
00030
00031 AREXPORT ArSickPacket::ArSickPacket(unsigned char sendingAddress) :
00032 ArBasePacket(2048, 4)
00033 {
00034 mySendingAddress = sendingAddress;
00035 }
00036
00037 AREXPORT ArSickPacket::~ArSickPacket()
00038 {
00039 }
00040
00047 AREXPORT void ArSickPacket::setSendingAddress(unsigned char address)
00048 {
00049 mySendingAddress = address;
00050 }
00051
00058 AREXPORT unsigned char ArSickPacket::getSendingAddress(void)
00059 {
00060 return mySendingAddress;
00061 }
00062
00069 AREXPORT unsigned char ArSickPacket::getReceivedAddress(void)
00070 {
00071 int len = myReadLength;
00072 unsigned char address;
00073
00074
00075 myReadLength = 1;
00076 address = bufToUByte();
00077 myLength = len;
00078 return address;
00079 }
00080
00081 AREXPORT ArTypes::UByte ArSickPacket::getID(void)
00082 {
00083 if (myLength >= 5)
00084 return myBuf[4];
00085 else
00086 return 0;
00087 }
00088
00089 AREXPORT void ArSickPacket::resetRead(void)
00090 {
00091 myReadLength = myHeaderLength + 1;
00092 }
00093
00094 AREXPORT void ArSickPacket::finalizePacket(void)
00095 {
00096 int len = myLength;
00097 int chkSum;
00098
00099
00100 myLength = 0;
00101
00102 uByteToBuf(0x02);
00103
00104 uByteToBuf(mySendingAddress);
00105
00106 uByte2ToBuf(len - myHeaderLength);
00107 myLength = len;
00108
00109
00110 chkSum = calcCRC();
00111 byteToBuf(chkSum & 0xff );
00112 byteToBuf((chkSum >> 8) & 0xff );
00113
00114
00115
00116 }
00117
00123 AREXPORT void ArSickPacket::duplicatePacket(ArSickPacket *packet)
00124 {
00125 myLength = packet->getLength();
00126 myReadLength = packet->getReadLength();
00127 myTimeReceived = packet->getTimeReceived();
00128 mySendingAddress = packet->getSendingAddress();
00129 memcpy(myBuf, packet->getBuf(), myLength);
00130
00131 }
00132
00133 AREXPORT ArTypes::Byte2 ArSickPacket::calcCRC(void)
00134 {
00135 unsigned short uCrc16;
00136 unsigned char abData[2];
00137 unsigned int uLen = myLength;
00138 unsigned char * commData = (unsigned char *)myBuf;
00139
00140 uCrc16 = 0;
00141 abData[0] = 0;
00142 while (uLen--)
00143 {
00144 abData[1] = abData[0];
00145 abData[0] = *commData++;
00146 if (uCrc16 & 0x8000)
00147 {
00148 uCrc16 = (uCrc16 & 0x7fff) << 1;
00149 uCrc16 ^= 0x8005;
00150 }
00151 else
00152 {
00153 uCrc16 <<= 1;
00154 }
00155 uCrc16 ^= ((unsigned short) abData[0] |
00156 ((unsigned short)(abData[1]) << 8));
00157 }
00158 return uCrc16;
00159 }
00160
00161 AREXPORT bool ArSickPacket::verifyCRC(void)
00162 {
00163 int readLen = myReadLength;
00164 int len = myLength;
00165 ArTypes::Byte2 chksum;
00166 unsigned char c1, c2;
00167
00168 myReadLength = myLength - 2;
00169
00170 if (myReadLength < myHeaderLength)
00171 return false;
00172
00173 c1 = bufToByte();
00174 c2 = bufToByte();
00175 myReadLength = readLen;
00176 chksum = (c1 & 0xff) | (c2 << 8);
00177
00178 myLength = myLength - 2;
00179 if (chksum == calcCRC()) {
00180 myLength = len;
00181 return true;
00182 } else {
00183 myLength = len;
00184 return false;
00185 }
00186
00187 }
00188
00189 AREXPORT ArTime ArSickPacket::getTimeReceived(void)
00190 {
00191 return myTimeReceived;
00192 }
00193
00194 AREXPORT void ArSickPacket::setTimeReceived(ArTime timeReceived)
00195 {
00196 myTimeReceived = timeReceived;
00197 }