Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SerialCtrl.cpp
- Committer:
- yabumi823
- Date:
- 2019-09-02
- Revision:
- 1:0184f38e2e83
- Parent:
- SerialCtrl/SerialCtrl.cpp@ 0:0dcd02e8aeab
- Child:
- 4:32f7deade942
File content as of revision 1:0184f38e2e83:
#include <SerialCtrl.h>
#include "mbed.h"
SerialCtrl::SerialCtrl(Serial *pts, Timer *ptt) {
_Serial = pts;
_Timer = ptt;
TIMEOUT_MS = 8;
}
void SerialCtrl::param(unsigned char header, unsigned char footer) {
STX = header;
ETX = footer;
}
bool SerialCtrl::get() {
int fail = 0;
unsigned char sum = 0;
while(1) {
if(input() == STX) {
break;
} else {
++fail;
if(fail > FAIL_MAX) return false;
}
}
for(int i=0; i<DATA_N; ++i) data[i] = input();
for(int i=0; i<DATA_N-1; ++i) sum += data[i];
if( (sum == data[SUM]) && (input() == ETX) ) {
return true;
} else {
return false;
}
}
unsigned char SerialCtrl::input() {
_Timer->reset();
while(_Timer->read_ms() < TIMEOUT_MS) {
if( _Serial->readable() ) {
return _Serial->getc();
}
}
return NUL;
}