Sensor data read in PC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
BOOL ReadByte(BYTE  *resp)
{
    BOOL bReturn = TRUE;
    BYTE rx;
    DWORD dwBytesTransferred=0;
 
    if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
    {
        if (dwBytesTransferred == 1)
        {
            *resp=rx;
            bReturn  = TRUE;
		
        }
        else bReturn = FALSE;
    }
    else    bReturn = FALSE;
   
	return bReturn;
}
 
BOOL ReadString(uint8_t *outstring, int *length)
{
    BYTE data;
    BYTE dataout[4096]={0};
    int index = 0;
    while(ReadByte(&data)== TRUE)
    {
        dataout[index++] = data;
		
    }
	
    memcpy(outstring, dataout, index);

    *length = index;
	
    return TRUE;

}
 
void ClosePort()
{
    CloseHandle(hPort);
    return;
}
 
typedef struct UM6_packet_struct
{
     uint8_t Address;
     uint8_t PT;
     uint16_t Checksum;

     uint8_t data_length;
     uint8_t data[30];
} UM6_packet;

uint8_t parse_serial_data( uint8_t* rx_data, uint8_t rx_length, UM6_packet* packet )
{
     uint8_t index;
	 
     if( rx_length < 7 )
     {
          return 1;
     }

    for( index = 0; index < (rx_length - 2); index++ )
     {
          
          if( rx_data[index] == 's' && rx_data[index+1] == 'n' && rx_data[index+2] == 'p' ) 
          {
               break;
          }
     }

     uint8_t packet_index = index;

     if( packet_index == (rx_length - 2) )
     {
          return 2;
     }

     if( (rx_length - packet_index) < 7 )
     {
          return 3;
     }

     uint8_t PT = rx_data[packet_index + 3];
     
     uint8_t packet_has_data = (PT >> 7) & 0x01;     // Check bit 7 (HAS_DATA)
     uint8_t packet_is_batch = (PT >> 6) & 0x01;      // Check bit 6 (IS_BATCH)
     uint8_t batch_length = (PT >> 2) & 0x0F;            // Extract the batch length (bits 2 through 5)
// Now  the actual packet length
     uint8_t data_length = 0;
     if( packet_has_data )
     {
          if( packet_is_batch )
          {
               // Packet has data and is a batch.  This means it contains ‘batch_length'  registers, each
               // of which has a length of 4 bytes
               data_length = 4*batch_length;
          }
          else     // Packet has data but is not a batch.  This means it contains one register (4 bytes)
          {
               data_length = 4;
          }
     }
     else     // Packet has no data
     {
          data_length = 0;
     }

     // At this point, we know exactly how long the packet is.  Now we can check to make sure
     // we have enough data for the full packet.
     if( (rx_length - packet_index) < (data_length + 5) )
     {
          return 3;
     }

     packet->Address = rx_data[packet_index + 4];
     packet->PT = PT;
    
     // Get the data bytes and compute the checksum all in one step
     packet->data_length = data_length;
     uint16_t computed_checksum = 's' + 'n' + 'p' + packet->PT + packet->Address;
     for( index = 0; index < data_length; index++ )
     {
          // Copy the data into the packet structure’s data array
          packet->data[index] = rx_data[packet_index + 5 + index];
          // Add the new byte to the checksum
          computed_checksum += packet->data[index];
		  
     }

     // Now see if our computed checksum matches the received checksum
     // First extract the checksum from the packet
     uint16_t received_checksum = (rx_data[packet_index + 5 + data_length] << 8);
     received_checksum |= rx_data[packet_index + 6 + data_length];

     // Now check to see if they don’t match
     if( received_checksum != computed_checksum )
     {
          return 4; 
		   }
	 
     return 0;
}

void del()
{
     //Checking for queue underflow
     if (front == -1)
     {
	cout<<"\nQueue Underflow\n";
	
	return;
     }
     //cout<<"\nElement deleted from queue is:"<<cqueue_arr[front]<<"\n";
    
     if (front == rear) /* queue has only one element */
     {
	front = -1;
	rear = -1;
     }
     else
     if(front == MAX-1)
	      front = 0;
     else
	      front = front + 1;
}/*End of del()*/
//Function to display the elements in the queue
void display()
{
     char front_pos = front,rear_pos = rear;
     //Checking whether the circular queue is empty or not
     if (front == -1)
     {
	cout<<"\nQueue is empty\n";
	
	return;
     }
     //Displaying the queue elements
    // cout<<"\nQueue elements:\n";
     
     if(front_pos <= rear_pos )
     while(front_pos <= rear_pos)
     {
		     cout<<cqueue_arr[front_pos];
		     
		     //cout<<"\t";
		     front_pos++;
     }
     else
     {
	 while(front_pos <= MAX-1)
	 {
			 cout<<cqueue_arr[front_pos];
			
			 //cout<<"\t";
			 front_pos++;
	 }
	 front_pos = 0;
	/* while(front_pos <= rear_pos)
	 {
			 cout<<cqueue_arr[front_pos]<<"\t ";
			 
			 //cout<<"\t";
			 front_pos++;
	 }*/
     }/*End of else*/
     
     //cout<<"\n";
}/*End of display() */

void insert(BYTE &added_item)
{
     
     //Checking for overflow condition
     if ((front == 0 && rear == MAX-1) || (front == rear +1))
     {

		cout<<"\nQueue Overflow \n";
		return;
		
     }
     if (front == -1) /*If queue is empty */
     {
		front = 0;
		rear = 0;
     }
     else
     if (rear == MAX-1)/*rear is at last position of queue */
	rear = 0;
     else
	rear = rear + 1;
         
     cqueue_arr[rear] = added_item;
	 display();

	 //del();
}/*End of insert()*/



int main(void)
{
	 
/*************************************/
	//configuring serial port in COM1
    hPort = ConfigureSerialPort(L"COM1");
    if(hPort == NULL)
    {
        cout<<"Com port configuration failed\n";
		return -1;
    }
	

	/*
	uint8_t message[100];
	int sizeofbuf = 100;
	cout << "Enter your value \n";
	cin>>message;

	WriteString(message,sizeofbuf);

	Sleep(10);

	uint8_t buf[100];

	ReadString(buf,&sizeofbuf);

	cout<<buf;

	*/



//*******************************************
	//declare valriable for sending message to sensor

	cout<<"Writing operation is done for Raw Gyro Z : ";
	uint8_t tx_data[20];

	tx_data[0] = 's';
	tx_data[1] = 'n';
	tx_data[2] = 'p';
	tx_data[3] = 0x00;        // Packet Type byte
	tx_data[4] = 0x59;       // Address of raw data from the Z axis accelerometer register
	tx_data[5] = 0x01;        // Checksum high byte
	tx_data[6] = 0xFB; 
	
	//USART1_transmit(  tx_data, 7 );
	WriteString(tx_data,20);
	Sleep (1000);

	cout<<"Reading from the sensor raw data of Z axis accelerometer";
	BYTE data;
    uint8_t dataout[4096]={0};
    int index = 0;
	UM6_packet new_packet;
	int *ACC_Z[5]={0};
	
try{
    if(ReadByte(&data)== TRUE)
	 {
		Sleep(100); 
		dataout[index++] = data;
		//cout<<dataout<<"\t";
					
		if(parse_serial_data( &data, 20, &new_packet ))
			{
     
			 if( new_packet.Address == UM6_ACCEL_RAW_Z)
				{
				ACC_Z[] = new_packet.data[0];
                                ACC_Z[1] = new_packet.data[1];
                                ACC_Z[2] = new_packet.data[2];
                                ACC_Z[3] = new_packet.data[3];
                                ACC_Z[4] = new_packet.data[4];
					
				cout<<	ACC_Z;
				 }
				 
		
		}
	 }
	}
	catch (BYTE data1){
	cout<<"Exception raised: "<<data1<<endl;
}

system("PAUSE");
    ClosePort();
    return 0;
}


Please help!!! In my opinion the problem is in parse_serial_data function..
Last edited on
the problem is in parse_serial_data function
What problem?
Hello Mr kbw
Thanks for your reply.

cout<<dataout<<"\t"; at this point the data shows in PC but after that
if(parse_serial_data( &data, 20, &new_packet )) this function does not shows anything on the screen. I am really confused where is the problem ...
cout<<dataout: gives the packet packet data..but if If i parse this value to this function it stop and also does not show anything...wish you vary good day and summer

Topic archived. No new replies allowed.