/[H7]/trunk/H7 Server/SlipSerial.cpp
ViewVC logotype

Contents of /trunk/H7 Server/SlipSerial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (show annotations) (download)
Thu Feb 1 13:49:01 2007 UTC (17 years, 3 months ago) by hedin
File size: 1908 byte(s)
Found a "feature" more, about the COM port...
Made some comments in the code
1 #include "StdAfx.h"
2 #include ".\slipserial.h"
3
4 #include <iomanip>
5
6 enum SlipState{
7 SlipNormal,
8 SlipEscaped,
9 SlipError,
10 SlipStopped
11 };
12
13 #define SLIP_END 192
14 #define SLIP_ESC 219
15 #define SLIP_ESCAPED_END 220
16 #define SLIP_ESCAPED_ESC 221
17
18
19 CSlipSerial::CSlipSerial(char* port, int bitrate)
20 : CSerial(port,bitrate)
21 {
22 }
23
24 CSlipSerial::CSlipSerial()
25 : CSerial()
26 {
27 }
28
29 CSlipSerial::~CSlipSerial(void)
30 {
31 }
32
33
34 std::vector<unsigned char> CSlipSerial::readFrame()
35 {
36 std::vector<unsigned char> buf;
37
38 int state = SlipNormal;
39
40 while ( state != SlipStopped )
41 {
42 unsigned char data = readByte();
43 switch (state)
44 {
45 case SlipNormal:
46 if (data == SLIP_END)
47 state = SlipStopped;
48 else if (data == SLIP_ESC)
49 state = SlipEscaped;
50 else
51 buf.push_back(data);
52 break;
53 case SlipEscaped:
54 if (data == SLIP_ESCAPED_END)
55 buf.push_back(SLIP_END);
56 else if (data == SLIP_ESCAPED_ESC)
57 buf.push_back(SLIP_ESC);
58 else
59 state = SlipError;
60 break;
61 default: //empty default case
62 break;
63 }
64 }
65
66 if (state != SlipStopped || state == SlipError) //in case we ran out of data before we recieved af SLIP_END character or there was an error
67 {
68 buf.clear();
69 }
70
71 return buf;
72 }
73
74 void CSlipSerial::writeFrame(std::vector<unsigned char> data)
75 {
76 for (int i=0; i<data.size(); i++)
77 {
78 writeSlipByte( data[i] );
79 }
80 //send the frame termination byte
81 writeByte(SLIP_END);
82
83 }
84
85 void CSlipSerial::writeFrame(unsigned char buf[], int len)
86 {
87 for (int i=0; i<len; i++)
88 {
89 writeSlipByte( buf[i] );
90 }
91 writeByte(SLIP_END);
92 }
93
94 void CSlipSerial::writeSlipByte(unsigned char out)
95 {
96 if (out == SLIP_ESC)
97 {
98 writeByte(SLIP_ESC);
99 writeByte(SLIP_ESCAPED_ESC);
100 }
101 else if (out == SLIP_END)
102 {
103 writeByte(SLIP_ESC);
104 writeByte(SLIP_ESCAPED_END);
105 }
106 else
107 writeByte( out );
108 }

  ViewVC Help
Powered by ViewVC 1.1.20