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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (show annotations) (download)
Sun Feb 4 21:22:44 2007 UTC (17 years, 3 months ago) by torben
File size: 1957 byte(s)
Clean the MFC heavy code, so it at least will compile with G++


1 #ifdef _WINDOWS
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, int 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 unsigned char data = readByte();
46 switch (state)
47 {
48 case SlipNormal:
49 if (data == SLIP_END)
50 state = SlipStopped;
51 else if (data == SLIP_ESC)
52 state = SlipEscaped;
53 else
54 buf.push_back(data);
55 break;
56 case SlipEscaped:
57 if (data == SLIP_ESCAPED_END)
58 buf.push_back(SLIP_END);
59 else if (data == SLIP_ESCAPED_ESC)
60 buf.push_back(SLIP_ESC);
61 else
62 state = SlipError;
63 break;
64 default: //empty default case
65 break;
66 }
67 }
68
69 if (state != SlipStopped || state == SlipError) //in case we ran out of data before we recieved af SLIP_END character or there was an error
70 {
71 buf.clear();
72 }
73
74 return buf;
75 }
76
77 void CSlipSerial::writeFrame(std::vector<unsigned char> data)
78 {
79 for (int i=0; i<data.size(); i++)
80 {
81 writeSlipByte( data[i] );
82 }
83 //send the frame termination byte
84 writeByte(SLIP_END);
85
86 }
87
88 void CSlipSerial::writeFrame(unsigned char buf[], int len)
89 {
90 for (int i=0; i<len; i++)
91 {
92 writeSlipByte( buf[i] );
93 }
94 writeByte(SLIP_END);
95 }
96
97 void CSlipSerial::writeSlipByte(unsigned char out)
98 {
99 if (out == SLIP_ESC)
100 {
101 writeByte(SLIP_ESC);
102 writeByte(SLIP_ESCAPED_ESC);
103 }
104 else if (out == SLIP_END)
105 {
106 writeByte(SLIP_ESC);
107 writeByte(SLIP_ESCAPED_END);
108 }
109 else
110 writeByte( out );
111 }

  ViewVC Help
Powered by ViewVC 1.1.20