1 |
torben |
49 |
//#ifdef _MSC_VER
|
2 |
torben |
51 |
#include "stdafx.h"
|
3 |
torben |
49 |
//#endif
|
4 |
torben |
38 |
|
5 |
torben |
62 |
#include <stdexcept>
|
6 |
|
|
|
7 |
torben |
44 |
#include "SlipSerial.h"
|
8 |
|
|
|
9 |
torben |
38 |
#include <iomanip>
|
10 |
torben |
62 |
#include <iostream>
|
11 |
torben |
38 |
enum SlipState{
|
12 |
|
|
SlipNormal,
|
13 |
|
|
SlipEscaped,
|
14 |
|
|
SlipError,
|
15 |
|
|
SlipStopped
|
16 |
|
|
};
|
17 |
|
|
|
18 |
|
|
#define SLIP_END 192
|
19 |
|
|
#define SLIP_ESC 219
|
20 |
|
|
#define SLIP_ESCAPED_END 220
|
21 |
|
|
#define SLIP_ESCAPED_ESC 221
|
22 |
|
|
|
23 |
|
|
|
24 |
torben |
54 |
CSlipSerial::CSlipSerial(char* port, Baudrate bitrate)
|
25 |
torben |
38 |
: CSerial(port,bitrate)
|
26 |
|
|
{
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
CSlipSerial::CSlipSerial()
|
30 |
|
|
: CSerial()
|
31 |
|
|
{
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
CSlipSerial::~CSlipSerial(void)
|
35 |
|
|
{
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
std::vector<unsigned char> CSlipSerial::readFrame()
|
40 |
|
|
{
|
41 |
|
|
std::vector<unsigned char> buf;
|
42 |
|
|
|
43 |
|
|
int state = SlipNormal;
|
44 |
|
|
|
45 |
torben |
62 |
int now;
|
46 |
|
|
int start = time(0);
|
47 |
|
|
|
48 |
torben |
38 |
while ( /*bytesReady() >0 &&*/ state != SlipStopped)
|
49 |
|
|
{
|
50 |
torben |
62 |
now = time(0);
|
51 |
|
|
if ( (now - start)>3)
|
52 |
|
|
throw timeout_error("readFrame() timed out");
|
53 |
|
|
|
54 |
torben |
47 |
int c = readByte();
|
55 |
|
|
if (c == -1)
|
56 |
|
|
{
|
57 |
|
|
Sleep(1);
|
58 |
|
|
continue;
|
59 |
|
|
}
|
60 |
|
|
unsigned char data = (unsigned char) c;
|
61 |
torben |
38 |
switch (state)
|
62 |
|
|
{
|
63 |
|
|
case SlipNormal:
|
64 |
|
|
if (data == SLIP_END)
|
65 |
|
|
state = SlipStopped;
|
66 |
|
|
else if (data == SLIP_ESC)
|
67 |
|
|
state = SlipEscaped;
|
68 |
|
|
else
|
69 |
|
|
buf.push_back(data);
|
70 |
|
|
break;
|
71 |
|
|
case SlipEscaped:
|
72 |
|
|
if (data == SLIP_ESCAPED_END)
|
73 |
|
|
buf.push_back(SLIP_END);
|
74 |
|
|
else if (data == SLIP_ESCAPED_ESC)
|
75 |
|
|
buf.push_back(SLIP_ESC);
|
76 |
|
|
else
|
77 |
|
|
state = SlipError;
|
78 |
|
|
break;
|
79 |
|
|
default: //empty default case
|
80 |
|
|
break;
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
if (state != SlipStopped || state == SlipError) //in case we ran out of data before we recieved af SLIP_END character or there was an error
|
85 |
|
|
{
|
86 |
|
|
buf.clear();
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
return buf;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
void CSlipSerial::writeFrame(std::vector<unsigned char> data)
|
93 |
|
|
{
|
94 |
|
|
for (int i=0; i<data.size(); i++)
|
95 |
|
|
{
|
96 |
|
|
writeSlipByte( data[i] );
|
97 |
|
|
}
|
98 |
|
|
//send the frame termination byte
|
99 |
|
|
writeByte(SLIP_END);
|
100 |
|
|
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
void CSlipSerial::writeFrame(unsigned char buf[], int len)
|
104 |
|
|
{
|
105 |
|
|
for (int i=0; i<len; i++)
|
106 |
|
|
{
|
107 |
|
|
writeSlipByte( buf[i] );
|
108 |
|
|
}
|
109 |
|
|
writeByte(SLIP_END);
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
void CSlipSerial::writeSlipByte(unsigned char out)
|
113 |
|
|
{
|
114 |
|
|
if (out == SLIP_ESC)
|
115 |
|
|
{
|
116 |
|
|
writeByte(SLIP_ESC);
|
117 |
|
|
writeByte(SLIP_ESCAPED_ESC);
|
118 |
|
|
}
|
119 |
|
|
else if (out == SLIP_END)
|
120 |
|
|
{
|
121 |
|
|
writeByte(SLIP_ESC);
|
122 |
|
|
writeByte(SLIP_ESCAPED_END);
|
123 |
|
|
}
|
124 |
|
|
else
|
125 |
|
|
writeByte( out );
|
126 |
torben |
44 |
}
|