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.
Diff: SerialCtrl.cpp
- Revision:
- 1:0184f38e2e83
- Parent:
- 0:0dcd02e8aeab
- Child:
- 4:32f7deade942
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialCtrl.cpp Mon Sep 02 02:15:34 2019 +0000
@@ -0,0 +1,46 @@
+#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;
+}