-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_crstrlib.c
More file actions
74 lines (60 loc) · 2.32 KB
/
test_crstrlib.c
File metadata and controls
74 lines (60 loc) · 2.32 KB
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
/*
* 此文件为crstrlib的单元测试文件, 可以通过测试样例,了解crstrlib库的用法。
*
* Copyright (c) 2017/2017 ShunkaiXing@杭州云江科技 <xingshunkai@qq.com>
* This work is licensed under the MIT license, see the file LICENSE for details.
*
* 获取更多OpenMV相关教程,加QQ群 凡哥带你玩转OpenMV < 564048763 >
**/
#include "crstrlib.h"
int main(){
// testFindNextInt();
// testFindNextFloat();
diyProtocal();
}
/* csv通信协议模拟 x,y为整数,数值为浮点数 */
void diyProtocal(){
char *data = "HEAD,12,3,62.31,TAIL"; // 模拟单片机的数据
int dataLen = CRStrLib_strLen(data); // 获取字符串长度
int dataIdx = 0; // 记录字符串读取的下标
int x; // x的值 整数
int y; // y的值 整数
float value; // 传感器的模拟值, 小数.
/* 校验帧头帧尾 */
if (CRStrLib_isValidData(data, "HEAD", "TAIL") == FALSE){
/* 数据丢失无效数据 */
printf("Invailid data\n");
return;
}
/* 根据不同数据类型,使用不同函数, 依次读入数据 */
CRStrLib_findNextInt(data, dataLen, &dataIdx, &x);
CRStrLib_findNextInt(data, dataLen, &dataIdx, &y);
CRStrLib_findNextFloat(data, dataLen, &dataIdx, &value);
printf("data read success\n");
printf("x: %d, y:%d, value:%.3f", x, y, value);
}
void testFindNextInt(){
char *data = "dsaserxc122,xaDSAAhg-232,--adaf897";
int strLen = CRStrLib_strLen(data);
int strIdx = 0;
int nextInt;
int status;
printf("data length : %d\n", strLen);
/* 获取所有的整数 */
while(CRStrLib_findNextInt(data, strLen, &strIdx, &nextInt)!=STATUS_NOT_FOUND){
printf("Next int is %d , strIdx : %d\n", nextInt, strIdx);
}
}
/* 测试寻找下一个浮点数 */
void testFindNextFloat(){
char *data = "dsaserxc122.21,xaDSAAhg-232.,--adaf897.123asdf asdfa888,asdf-0.423, 0.0";
int strLen = CRStrLib_strLen(data);
int strIdx = 0;
float nextFloat;
int status;
printf("data length : %d\n", strLen);
/* 获取所有的整数 */
while(CRStrLib_findNextFloat(data, strLen, &strIdx, &nextFloat)!=STATUS_NOT_FOUND){
printf("Next flaot is %.3f , strIdx : %d\n", nextFloat, strIdx);
}
}