/[H7]/branches/linux-serial/SlipSerial.cpp
ViewVC logotype

Contents of /branches/linux-serial/SlipSerial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations) (download)
Mon Feb 5 10:10:18 2007 UTC (17 years, 3 months ago) by torben
File size: 2051 byte(s)
Added a cross-platform specification of desired baud-rate.

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

  ViewVC Help
Powered by ViewVC 1.1.20