Üben

Dependencies:   mbed

Committer:
mexx
Date:
Thu Nov 15 19:06:27 2018 +0000
Revision:
1:7a59092dbfee
Parent:
0:2fa9a939aa50
2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mexx 0:2fa9a939aa50 1 #include "mbed.h"
mexx 1:7a59092dbfee 2 #define TRENNZEICHEN "/"
mexx 1:7a59092dbfee 3 #define ANZ_FELDER 5
mexx 1:7a59092dbfee 4
mexx 0:2fa9a939aa50 5
mexx 0:2fa9a939aa50 6 // ---------------- Inherited Serial RS232 Event Class --------------------------
mexx 0:2fa9a939aa50 7 const uint8_t STRMAX = 80;
mexx 0:2fa9a939aa50 8 const char EOT = ';';
mexx 0:2fa9a939aa50 9 const char CRLF = '\n';
mexx 0:2fa9a939aa50 10
mexx 0:2fa9a939aa50 11 class SerialEventInh : public Serial {
mexx 0:2fa9a939aa50 12 // Serial _pc;
mexx 0:2fa9a939aa50 13 void _risingISR();
mexx 0:2fa9a939aa50 14 char _str[STRMAX];
mexx 0:2fa9a939aa50 15 volatile bool _strOkFlag;
mexx 0:2fa9a939aa50 16 int _index;
mexx 0:2fa9a939aa50 17
mexx 0:2fa9a939aa50 18 public:
mexx 0:2fa9a939aa50 19 SerialEventInh() : Serial(USBTX, USBRX) { // create the Serial on the pin specified to SwEvent
mexx 0:2fa9a939aa50 20 attach(callback(this, &SerialEventInh::pc_recv)); // attach DataReceive-function of this SerialEvent instance
mexx 0:2fa9a939aa50 21 _strOkFlag = false;
mexx 0:2fa9a939aa50 22 _index=0;
mexx 0:2fa9a939aa50 23 }
mexx 0:2fa9a939aa50 24
mexx 0:2fa9a939aa50 25 SerialEventInh(PinName tx, PinName rx) : Serial(tx, rx) { // create the Serial on the pin specified to SwEvent
mexx 0:2fa9a939aa50 26 attach(callback(this, &SerialEventInh::pc_recv)); // attach DataReceive-function of this SerialEvent instance
mexx 0:2fa9a939aa50 27 _strOkFlag = false;
mexx 0:2fa9a939aa50 28 _index=0;
mexx 0:2fa9a939aa50 29
mexx 0:2fa9a939aa50 30 }
mexx 0:2fa9a939aa50 31 void pc_recv();
mexx 0:2fa9a939aa50 32 void getString(char st[]);
mexx 0:2fa9a939aa50 33 int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated
mexx 0:2fa9a939aa50 34 };
mexx 0:2fa9a939aa50 35
mexx 0:2fa9a939aa50 36 void SerialEventInh::getString(char st[]) {
mexx 0:2fa9a939aa50 37 for( int i=0; i <= _index; i++)
mexx 0:2fa9a939aa50 38 st[i] = _str[i];
mexx 0:2fa9a939aa50 39 _index=0;
mexx 0:2fa9a939aa50 40 }
mexx 0:2fa9a939aa50 41
mexx 0:2fa9a939aa50 42 void SerialEventInh::pc_recv() {
mexx 0:2fa9a939aa50 43 char c;
mexx 0:2fa9a939aa50 44 while(readable()){
mexx 0:2fa9a939aa50 45 c = getc();
mexx 0:2fa9a939aa50 46 if((c != CRLF) && (_index < STRMAX)) {
mexx 0:2fa9a939aa50 47 _str[_index++] = c;
mexx 0:2fa9a939aa50 48 }
mexx 0:2fa9a939aa50 49 }
mexx 0:2fa9a939aa50 50 if(( c == EOT)) { // end: . string not empty
mexx 0:2fa9a939aa50 51 if(_index >= 1) {
mexx 0:2fa9a939aa50 52 _strOkFlag = true;
mexx 0:2fa9a939aa50 53 _str[--_index] = 0;
mexx 0:2fa9a939aa50 54 }
mexx 0:2fa9a939aa50 55 }
mexx 0:2fa9a939aa50 56 }
mexx 0:2fa9a939aa50 57
mexx 0:2fa9a939aa50 58 int SerialEventInh::checkFlag() {
mexx 0:2fa9a939aa50 59 if( _strOkFlag ) {
mexx 0:2fa9a939aa50 60 _strOkFlag = false;
mexx 0:2fa9a939aa50 61 return 1;
mexx 0:2fa9a939aa50 62 }
mexx 0:2fa9a939aa50 63 return 0;
mexx 0:2fa9a939aa50 64 }
mexx 1:7a59092dbfee 65 class ParsSerial::ParsSerial
mexx 1:7a59092dbfee 66 {
mexx 1:7a59092dbfee 67 public:
mexx 1:7a59092dbfee 68 ParsSerial(PinName USBTX, PinName USBRX):public ParsSerial(PinName USBTX, PinName USBRX)
mexx 1:7a59092dbfee 69 };
mexx 1:7a59092dbfee 70
mexx 0:2fa9a939aa50 71
mexx 0:2fa9a939aa50 72 ParsSerial ps(USBTX, USBRX);
mexx 0:2fa9a939aa50 73
mexx 0:2fa9a939aa50 74 int main()
mexx 0:2fa9a939aa50 75 {
mexx 1:7a59092dbfee 76 char str[]= "Bauteil9/Raum903/Reihe2/PC4/;";
mexx 0:2fa9a939aa50 77 char str[STRMAX];
mexx 0:2fa9a939aa50 78 char* pFelder[ANZ_FELDER];
mexx 0:2fa9a939aa50 79
mexx 0:2fa9a939aa50 80 while(1) {
mexx 0:2fa9a939aa50 81 if(ps.checkFlag()) {
mexx 0:2fa9a939aa50 82 ps.getString(str);
mexx 0:2fa9a939aa50 83 printf("String: %s\n", str);
mexx 0:2fa9a939aa50 84 ps.ParseFelder(str, pFelder, ANZ_FELDER, TRENNZEICHEN);
mexx 0:2fa9a939aa50 85 printf("Str: %s %s %s\r\n", pFelder[0], pFelder[1], pFelder[2]);
mexx 0:2fa9a939aa50 86 }
mexx 0:2fa9a939aa50 87 }