BayEOS-Arduino  1.8.0_0.0.4
BaySerial.cpp
1 #include "BaySerial.h"
2 uint8_t BaySerialInterface::readByte(int timeout,bool escape){
3  _read_timeout=false;
4  uint8_t b=0;
5  while(timeout>0){
6  if(i_available()){
7  b = read();
8  if (escape && b == ESCAPE) {
9  if (i_available()) {
10  b = read();
11  b= 0x20 ^ b;
12  } else {
13  // escape byte. next byte will be
14  _escape = true;
15  continue;
16  }
17  }
18  if (_escape == true) {
19  b = 0x20 ^ b;
20  _escape = false;
21  }
22  return b;
23  }
24  timeout--;
25  delay(1);
26  }
27  _read_timeout=true;
28  return b;
29 
30 }
31 
32 
33 void BaySerialInterface::sendByte(uint8_t b, bool escape) {
34 
35  if (escape && (b == START_BYTE || b == ESCAPE || b == XON || b == XOFF || b=='\n' || b=='\r')) {
36  write(ESCAPE);
37  write(b ^ 0x20);
38  } else {
39  write(b);
40  }
41 }
42 
43 void BaySerialInterface::sendAck(uint8_t b){
44  sendByte(START_BYTE,false);
45  sendByte(0x1,true);
46  sendByte(0x2,true);
47  sendByte(b,true);
48 // sendByte(b+0x2,true); Ursprüngliche Version !!! FALSCH!!!
49  sendByte(0xff-(b+0x2),true); //RICHTIG!
50 }
51 
53  sendByte(START_BYTE,false);
54  sendByte(getPacketLength(),true);
55  sendByte(0x1,true);
56  _checksumTotal=0x1;
57  for(uint8_t i=0;i<getPacketLength();i++){
58  sendByte(_payload[i],true);
59  _checksumTotal+=_payload[i];
60  }
61  sendByte((0xff-_checksumTotal),true);
62 
63 }
64 
66  if(_break) return TX_BREAK;
67  sendFrame();
68  uint8_t res=0;
69  if(res=readPacket(API_ACK)) return res;
70  else if(_ack==TX_OK) return 0;
71  else return 1;
72 
73 }
75  _read_timeout=timeout;
76  return readPacket(API_DATA);
77 }
78 
79 uint8_t BaySerialInterface::readPacket(uint8_t type) {
80  _pos=0;
81  uint8_t b=0;
82  while(true){
83  b = readByte(_timeout,false);
84  if(_read_timeout) return 2;
85  if(b == START_BYTE) break;
86  }
87 
88  _length=readByte(_timeout,true);
89  if(_read_timeout) return 2;
90 
91  _api=readByte(_timeout,true);
92  if(_api!=type){
93  return readPacket(type);
94  }
95  _checksumTotal=_api;
96  if(_read_timeout) return 2;
97 
98 
99 
100  while(_pos<_length){
101  b=readByte(_timeout,true);
102  if(_read_timeout) return 2;
103  _checksumTotal+= b;
104  if(_api==API_DATA) _payload[_pos] = b;
105  else _ack=b;
106  _pos++;
107  }
108 
109  _next=_pos;
110  b=readByte(_timeout,true);
111  if(_read_timeout) return 2;
112  _checksumTotal+= b;
113 
114  // set break
115  if(_api==API_ACK && _ack==TX_BREAK){
116  _break=1;
117  return TX_BREAK;
118  }
119  // reset break when there is data
120  if(_api==API_DATA && _break) _break=0;
121  // verify checksum
122  if ((_checksumTotal & 0xff) == 0xff) {
123  if(_api==API_DATA) sendAck(TX_OK);
124  return 0;
125  } else {
126  if(_api==API_DATA) sendAck(TX_CHECKSUM_FAILED);
127  // checksum failed
128  return 1;
129  }
130 }
131 
132 BaySerial::BaySerial(HardwareSerial &serial,int timeout){
133  BaySerialInterface::_timeout=timeout;
134  _serial=&serial;
135 }
136 
138  return _serial->available();
139 }
140 int BaySerial::i_available(void){
141  return _serial->available();
142 }
143 void BaySerial::begin(long baud){
144  _serial->begin(baud);
145 }
146 void BaySerial::flush(void){
147  _serial->flush();
148 }
149 void BaySerial::end(void){
150  _serial->end();
151 }
152 int BaySerial::read(void){
153  return _serial->read();
154 }
155 
156 size_t BaySerial::write(uint8_t c){
157  return _serial->write(c);
158 }
159 
uint8_t readIntoPayload(int timeout=5000)
Definition: BaySerial.cpp:74
uint8_t sendPayload(void)
Definition: BaySerial.cpp:65
uint8_t getPacketLength(void) const
Definition: BayEOS.cpp:395
void sendFrame(void)
Definition: BaySerial.cpp:52
BaySerial(HardwareSerial &serial, int timeout=1000)
Definition: BaySerial.cpp:132
int available(void)
Definition: BaySerial.cpp:137