Skip to content

Commit e8a032b

Browse files
committed
Added a crc mpeg2 python test script using crcchecker, also added a test c file for transmitdata using some test values.
1 parent a1e835f commit e8a032b

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

crcmpeg2Test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import crccheck
2+
3+
4+
crcmpeg = crccheck.crc.Crc32Mpeg2()
5+
6+
7+
if __name__ == "__main__":
8+
data = bytearray.fromhex('0000102120423063408450a560c670e79129a14ab16bc18cd1ade1cef1ef123132732252')
9+
crchex = crcmpeg.calchex(data)
10+
print('0x'+crchex)

test_new_message.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
5+
#define IMU_SERIAL_MSG_SIZE (36)
6+
#define START_FLAG (0xF0)
7+
#define END_FLAG (0XF0)
8+
#define F0_ESCAPE (0xF1F2)
9+
#define F1_ESCAPE (0xF1F3)
10+
11+
static const int8_t IMU_HEADER_BYTE = 0x31;
12+
13+
uint8_t* Encode(uint8_t* message, int length);
14+
void writeInt32ToArray(uint8_t* array, int startIndex, int32_t value);
15+
16+
int main()
17+
{
18+
//use dummy data to test if encoding and construction of final send buffer are correct.
19+
int32_t accelX = 0x1234F0F1;
20+
int32_t accelY = 0xF1F2F3F1;
21+
int32_t accelZ = 0x98765432;
22+
int32_t gyroX = 0xF0F0F0F0;
23+
int32_t gyroY = 0xF1F1F1F1;
24+
int32_t gyroZ = 0xF3F2F1F0;
25+
int32_t magnetoX = 0xABCDEF71;
26+
int32_t magnetoY = 8;
27+
int32_t magnetoZ = 0xF0;
28+
uint32_t crc = 0xABCD1234;
29+
30+
uint8_t message[IMU_SERIAL_MSG_SIZE] = { 0 };
31+
int messageindex = 0;
32+
33+
writeInt32ToArray(message, messageindex, accelX); messageindex += 4;
34+
writeInt32ToArray(message, messageindex, accelY); messageindex += 4;
35+
writeInt32ToArray(message, messageindex, accelZ); messageindex += 4;
36+
writeInt32ToArray(message, messageindex, gyroX); messageindex += 4;
37+
writeInt32ToArray(message, messageindex, gyroY); messageindex += 4;
38+
writeInt32ToArray(message, messageindex, gyroZ); messageindex += 4;
39+
writeInt32ToArray(message, messageindex, magnetoX); messageindex += 4;
40+
writeInt32ToArray(message, messageindex, magnetoY); messageindex += 4;
41+
writeInt32ToArray(message, messageindex, magnetoZ); messageindex += 4;
42+
43+
for (int n = 0; n < IMU_SERIAL_MSG_SIZE; n++)
44+
{
45+
if (n % 4 == 0)
46+
printf("\noriginal message at [%d to %d]:", n, n + 4);
47+
printf("%x", message[n]);
48+
}
49+
50+
uint8_t* encoded_message = Encode(message, IMU_SERIAL_MSG_SIZE);
51+
52+
int encoded_length = encoded_message[0];
53+
int buffer_length = encoded_length + 10;
54+
for (int m = 1; m < buffer_length; m++)
55+
{
56+
if ((m-1) % 4 == 0)
57+
printf("\nencoded message at [%d to %d]:", m-1, m + 4 -1);
58+
printf("%x", encoded_message[m]);
59+
}
60+
61+
62+
uint8_t* buffer = malloc(buffer_length * sizeof(uint8_t));
63+
buffer[0] = START_FLAG;
64+
buffer[1] = IMU_HEADER_BYTE;
65+
for (int i = 0; i < encoded_length; i++)
66+
{
67+
buffer[i] = encoded_message[i];
68+
}
69+
buffer[buffer_length - 1] = END_FLAG;
70+
71+
for (int o = 1; o < buffer_length; o++)
72+
{
73+
if (o % 4 == 0)
74+
printf("\nfinal buffer at [%d to %d]:", o , o + 4);
75+
printf("%x", buffer[o]);
76+
}
77+
78+
79+
return 0;
80+
}
81+
82+
uint8_t* Encode(uint8_t* message, int length)
83+
{
84+
uint8_t* buffer = malloc(2 * sizeof(uint8_t) * length);
85+
int bufferindex = 0;
86+
for (int i = 0; i < length; i++)
87+
{
88+
//printf("Encoding %x ...\n", message[i]);
89+
if (message[i] == 0xF0)
90+
{
91+
buffer[1+bufferindex++] = 0xF0;
92+
buffer[1+bufferindex++] = 0xF1;
93+
}
94+
else if (message[i] == 0xF1)
95+
{
96+
buffer[1+bufferindex++] = 0xF1;
97+
buffer[1+bufferindex++] = 0xF2;
98+
}
99+
else
100+
{
101+
buffer[1+bufferindex++] = message[i];
102+
}
103+
}
104+
buffer[0] = bufferindex+1;
105+
printf("\n\nFinal buffer size = %d\n", buffer[0]);
106+
return buffer;
107+
}
108+
109+
void writeInt32ToArray(uint8_t* array, int startIndex, int32_t value)
110+
{
111+
//printf("Writing %d ...\n", value);
112+
113+
//printf("Writing %d to index+0\n", (value >> 24) & 0xFF);
114+
array[startIndex + 0] = (value >> 24) & 0xFF;
115+
116+
//printf("Writing %d to index+1\n", (value >> 16) & 0xFF);
117+
array[startIndex + 1] = (value >> 16) & 0xFF;
118+
119+
//printf("Writing %d to index+2\n", (value >> 8) & 0xFF);
120+
array[startIndex + 2] = (value >> 8) & 0xFF;
121+
122+
//printf("Writing %d to index+3\n", value & 0xFF);
123+
array[startIndex + 3] = (uint8_t)(value & 0xFF);
124+
}

0 commit comments

Comments
 (0)