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