2 uint8_t BaySerialInterface::readByte(
int timeout,
bool escape){
8 if (escape && b == ESCAPE) {
18 if (_escape ==
true) {
33 void BaySerialInterface::sendByte(uint8_t b,
bool escape) {
35 if (escape && (b == START_BYTE || b == ESCAPE || b == XON || b == XOFF || b==
'\n' || b==
'\r')) {
43 void BaySerialInterface::sendAck(uint8_t b){
44 sendByte(START_BYTE,
false);
49 sendByte(0xff-(b+0x2),
true);
53 sendByte(START_BYTE,
false);
58 sendByte(_payload[i],
true);
59 _checksumTotal+=_payload[i];
61 sendByte((0xff-_checksumTotal),
true);
66 if(_break)
return TX_BREAK;
69 if(res=readPacket(API_ACK))
return res;
70 else if(_ack==TX_OK)
return 0;
75 _read_timeout=timeout;
76 return readPacket(API_DATA);
79 uint8_t BaySerialInterface::readPacket(uint8_t type) {
83 b = readByte(_timeout,
false);
84 if(_read_timeout)
return 2;
85 if(b == START_BYTE)
break;
88 _length=readByte(_timeout,
true);
89 if(_read_timeout)
return 2;
91 _api=readByte(_timeout,
true);
93 return readPacket(type);
96 if(_read_timeout)
return 2;
101 b=readByte(_timeout,
true);
102 if(_read_timeout)
return 2;
104 if(_api==API_DATA) _payload[_pos] = b;
110 b=readByte(_timeout,
true);
111 if(_read_timeout)
return 2;
115 if(_api==API_ACK && _ack==TX_BREAK){
120 if(_api==API_DATA && _break) _break=0;
122 if ((_checksumTotal & 0xff) == 0xff) {
123 if(_api==API_DATA) sendAck(TX_OK);
126 if(_api==API_DATA) sendAck(TX_CHECKSUM_FAILED);
133 BaySerialInterface::_timeout=timeout;
138 return _serial->available();
140 int BaySerial::i_available(
void){
141 return _serial->available();
143 void BaySerial::begin(
long baud){
144 _serial->begin(baud);
146 void BaySerial::flush(
void){
149 void BaySerial::end(
void){
152 int BaySerial::read(
void){
153 return _serial->read();
156 size_t BaySerial::write(uint8_t c){
157 return _serial->write(c);
uint8_t readIntoPayload(int timeout=5000)
uint8_t sendPayload(void)
uint8_t getPacketLength(void) const
BaySerial(HardwareSerial &serial, int timeout=1000)