-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathparserascii.cpp
More file actions
74 lines (63 loc) · 1.54 KB
/
Copy pathparserascii.cpp
File metadata and controls
74 lines (63 loc) · 1.54 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
#include "parserascii.h"
ParserAscii::ParserAscii()
{
state=IDLE;
}
//accept lines of text input messages of the form
//53 A9 6A A9 AA 5A 55 96 A9 AA 9A AA 95 69 A9 A5 A9 99 AA AA A6 AA A6 96 6A A9 55 35
void ParserAscii::inputBytes(QByteArray in)
{
for(int i=0; i<in.size(); i++) {
decodeChar(in.at(i));
}
}
quint8 ParserAscii::charToInt(char n)
{
if (n >= '0' && n <= '9')
return (n-'0');
else
if (n >= 'A' && n <= 'F')
return (n-'A'+10);
else
return 0;
}
//reformat to binary equivalent
void ParserAscii::decodeChar(char c)
{
static quint8 byte;
//ignore any spaces
if(c==' ')
return;
switch(state)
{
case IDLE:
if(c=='5') state=HEAD;
break;
case HEAD:
if(c=='3') state=PAYLOAD1;
//prefix the message as it would have been
//from the binary data
data.append(0x33);
data.append(0x55);
data.append(0x53);
break;
case PAYLOAD1:
//first character of payload
byte = charToInt(c) * 16;
state = PAYLOAD2;
break;
case PAYLOAD2:
//second character of payload
byte += charToInt(c);
data.append(byte);
//look for end of message
if(byte==0x35) {
emit outputBytes(data);
data.clear();
state=IDLE;
} else {
state=PAYLOAD1;
}
break;
}
}