PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
ChrSerialPacket.h
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #ifndef __ChrSerialPacket_h
8 #define __ChrSerialPacket_h
9 
10 #include <vector>
11 
13 {
14 public:
16  {
17  m_Address = 0;
18  m_PacketDescriptor=0;
19  m_Checksum = 0;
20  m_Data.resize(16*4);
21  }
22 
23  unsigned char GetAddress()
24  {
25  return m_Address;
26  }
27 
28  void SetAddress(unsigned char address)
29  {
30  m_Address=address;
31  }
32 
33  void SetPacketDescriptor(unsigned char packetDescriptor)
34  {
35  m_PacketDescriptor=packetDescriptor;
36  }
37 
39  {
40  if( m_PacketDescriptor & 0x40 )
41  return true;
42  else
43  return false;
44  }
45 
46  void SetBatchEnable(bool enable)
47  {
48  if( enable )
49  {
50  m_PacketDescriptor |= 0x40;
51  }
52  else
53  {
54  m_PacketDescriptor |= 0x40;
55  m_PacketDescriptor ^= 0x40;
56  }
57  }
58 
59  bool GetHasData()
60  {
61  if( m_PacketDescriptor & 0x80 )
62  return true;
63  else
64  return false;
65  }
66 
67  void SetHasData( bool has_data )
68  {
69  if( has_data )
70  {
71  m_PacketDescriptor |= 0x80;
72  }
73  else
74  {
75  m_PacketDescriptor |= 0x80;
76  m_PacketDescriptor ^= 0x80;
77  }
78  }
79 
80  unsigned char GetBatchLength()
81  {
82  return (m_PacketDescriptor >> 2) & 0x0F;
83  }
84 
85  void SetBatchLength( unsigned char length )
86  {
87  length &= 0x0F;
88  // Clear batch length bits
89  m_PacketDescriptor |= (0x0F << 2);
90  m_PacketDescriptor ^= (0x0F << 2);
91  // Set batch length bits
92  m_PacketDescriptor |= (length << 2);
93  }
94 
95  unsigned char GetCommandFailed()
96  {
97  return m_PacketDescriptor & 0x01;
98  }
99  void SetCommandFailed( unsigned char failed)
100  {
101  failed &= 0x01;
102  m_PacketDescriptor |= 0x01;
103  m_PacketDescriptor ^= 0x01;
104  m_PacketDescriptor |= failed;
105  }
106 
107  unsigned char GetDataLength()
108  {
109  if( !GetHasData() )
110  {
111  return 0;
112  }
113  if( GetBatchEnable() )
114  {
115  return 4*GetBatchLength();
116  }
117  return 4;
118  }
119 
120  unsigned char GetPacketLength()
121  {
123  }
124 
125  unsigned char GetPacketByte( int index )
126  {
127  if (index<GetHeaderLength())
128  {
129  return GetHeaderByte(index);
130  }
131  index-=GetHeaderLength();
132  if (index<GetDataLength())
133  {
134  return GetDataByte(index);
135  }
136  index-=GetDataLength();
137  if (index<GetChecksumLength())
138  {
139  return GetChecksumByte(index);
140  }
141  return 0;
142  }
143 
144  unsigned char GetDataByte( int index )
145  {
146  return m_Data[index];
147  }
148  void SetDataByte( int index, unsigned char value )
149  {
150  m_Data[index] = value;
151  }
152 
153  unsigned char GetHeaderByte( int index )
154  {
155  switch (index)
156  {
157  case 0: return 's';
158  case 1: return 'n';
159  case 2: return 'p';
160  case 3: return m_PacketDescriptor;
161  case 4: return m_Address;
162  default:
163  return 0;
164  }
165  }
166 
167  unsigned char GetHeaderLength()
168  {
169  return 5;
170  }
171 
172  unsigned char GetChecksumLength()
173  {
174  return 2;
175  }
176 
177  unsigned char GetChecksumByte( int index )
178  {
179  switch (index)
180  {
181  case 0: return (m_Checksum >> 8);
182  case 1: return (m_Checksum & 0x0FF);
183  default:
184  return 0;
185  }
186  }
187 
188  void ComputeChecksum( void )
189  {
190  unsigned short int checksum;
191 
192  checksum = 0;
193 
194  checksum += (unsigned char)'s';
195  checksum += (unsigned char)'n';
196  checksum += (unsigned char)'p';
197  checksum += m_PacketDescriptor;
198  checksum += m_Address;
199 
200  unsigned char dataLength=GetDataLength();
201  for( int i = 0; i < dataLength; i++ )
202  {
203  checksum += m_Data[i];
204  }
205 
206  m_Checksum = checksum;
207  }
208 
209 private:
210  unsigned char m_Address;
211  unsigned char m_PacketDescriptor;
212  unsigned short m_Checksum;
213 
214  std::vector<unsigned char> m_Data;
215 };
216 
217 #endif
unsigned char GetDataLength()
unsigned char GetBatchLength()
void SetBatchEnable(bool enable)
void SetHasData(bool has_data)
unsigned char GetChecksumByte(int index)
for i
unsigned char GetPacketByte(int index)
void SetPacketDescriptor(unsigned char packetDescriptor)
void SetAddress(unsigned char address)
unsigned char GetDataByte(int index)
void SetBatchLength(unsigned char length)
unsigned char GetHeaderLength()
const char * address
Definition: phidget22.h:2552
void SetDataByte(int index, unsigned char value)
void ComputeChecksum(void)
unsigned char GetHeaderByte(int index)
unsigned char GetPacketLength()
void SetCommandFailed(unsigned char failed)
const char const char * value
Definition: phidget22.h:5111
unsigned char GetAddress()
unsigned char GetChecksumLength()
unsigned char GetCommandFailed()