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