PSSS
/
ReadFromSerial446
nulla
main.cpp@2:67223774f668, 2018-07-30 (annotated)
- Committer:
- giuseppe_guida
- Date:
- Mon Jul 30 12:44:42 2018 +0000
- Revision:
- 2:67223774f668
- Parent:
- 1:fc463dd04a7f
aggiornato
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giuseppe_guida | 0:b40deb141f99 | 1 | #include "mbed.h" |
giuseppe_guida | 2:67223774f668 | 2 | #include "CAN3.h" |
giuseppe_guida | 2:67223774f668 | 3 | #include "mcp2515.h" |
giuseppe_guida | 0:b40deb141f99 | 4 | #include <string> |
giuseppe_guida | 2:67223774f668 | 5 | #include <sstream> |
giuseppe_guida | 0:b40deb141f99 | 6 | #include <algorithm> |
giuseppe_guida | 2:67223774f668 | 7 | #define DIM_STORE 6000 |
giuseppe_guida | 2:67223774f668 | 8 | #define DIM_PARAMS 10 |
giuseppe_guida | 0:b40deb141f99 | 9 | using namespace std; |
giuseppe_guida | 0:b40deb141f99 | 10 | |
giuseppe_guida | 0:b40deb141f99 | 11 | RawSerial pc(USBTX,USBRX); |
ironidio | 1:fc463dd04a7f | 12 | DigitalOut myled(LED2); |
giuseppe_guida | 2:67223774f668 | 13 | SPI spi(SPI_MOSI,SPI_MISO,SPI_SCK); |
giuseppe_guida | 2:67223774f668 | 14 | CAN3 can(spi,SPI_CS,PA_1); |
giuseppe_guida | 2:67223774f668 | 15 | |
giuseppe_guida | 2:67223774f668 | 16 | |
giuseppe_guida | 2:67223774f668 | 17 | CANMessage msg1; |
ironidio | 1:fc463dd04a7f | 18 | |
ironidio | 1:fc463dd04a7f | 19 | char data='A'; |
ironidio | 1:fc463dd04a7f | 20 | char vrx[1000]; //memorizzo i caratteri ricevuti dopo il comando di start '+' |
ironidio | 1:fc463dd04a7f | 21 | |
ironidio | 1:fc463dd04a7f | 22 | bool start=false; // true quando ho ricevuto il comando di start '+' |
ironidio | 1:fc463dd04a7f | 23 | bool stop=false; // true quando ho ricevuto il comando di stop '-' |
giuseppe_guida | 0:b40deb141f99 | 24 | |
ironidio | 1:fc463dd04a7f | 25 | bool canread=false; //true per abilitare la lettura da can |
ironidio | 1:fc463dd04a7f | 26 | |
ironidio | 1:fc463dd04a7f | 27 | int i = 0; |
ironidio | 1:fc463dd04a7f | 28 | int j = 0; |
ironidio | 1:fc463dd04a7f | 29 | int k = 0; |
giuseppe_guida | 2:67223774f668 | 30 | int index_data; |
ironidio | 1:fc463dd04a7f | 31 | int i_row = 0; |
ironidio | 1:fc463dd04a7f | 32 | const char* FromStrToChar; |
ironidio | 1:fc463dd04a7f | 33 | size_t n,pos; |
ironidio | 1:fc463dd04a7f | 34 | string delimiter = ","; |
giuseppe_guida | 2:67223774f668 | 35 | string paramlist[DIM_PARAMS]; |
giuseppe_guida | 2:67223774f668 | 36 | string StoreData[DIM_STORE]; |
giuseppe_guida | 2:67223774f668 | 37 | |
giuseppe_guida | 2:67223774f668 | 38 | void clear_StoreData(){ |
giuseppe_guida | 2:67223774f668 | 39 | int it; |
giuseppe_guida | 2:67223774f668 | 40 | for(it = 0; it < DIM_STORE; it++) |
giuseppe_guida | 2:67223774f668 | 41 | StoreData[it].clear(); |
giuseppe_guida | 2:67223774f668 | 42 | } |
giuseppe_guida | 2:67223774f668 | 43 | |
giuseppe_guida | 2:67223774f668 | 44 | void clear_paramlist(){ |
giuseppe_guida | 2:67223774f668 | 45 | int it; |
giuseppe_guida | 2:67223774f668 | 46 | for(it = 0; it < DIM_PARAMS; it++) |
giuseppe_guida | 2:67223774f668 | 47 | paramlist[it].clear(); |
giuseppe_guida | 2:67223774f668 | 48 | } |
giuseppe_guida | 0:b40deb141f99 | 49 | |
giuseppe_guida | 0:b40deb141f99 | 50 | void rxCallback(){ |
ironidio | 1:fc463dd04a7f | 51 | data = char(pc.getc()); |
ironidio | 1:fc463dd04a7f | 52 | if (data == '+'){ |
ironidio | 1:fc463dd04a7f | 53 | myled = 1; |
ironidio | 1:fc463dd04a7f | 54 | i=0; |
ironidio | 1:fc463dd04a7f | 55 | j=0; |
ironidio | 1:fc463dd04a7f | 56 | start=true; |
giuseppe_guida | 2:67223774f668 | 57 | clear_paramlist(); |
giuseppe_guida | 2:67223774f668 | 58 | clear_StoreData(); |
ironidio | 1:fc463dd04a7f | 59 | }else if(data == '-'){ |
ironidio | 1:fc463dd04a7f | 60 | myled=0; |
ironidio | 1:fc463dd04a7f | 61 | stop=true; |
ironidio | 1:fc463dd04a7f | 62 | start=false; |
ironidio | 1:fc463dd04a7f | 63 | canread=false; //disabilito la lettura da CAN |
ironidio | 1:fc463dd04a7f | 64 | //pc.puts("123?"); |
ironidio | 1:fc463dd04a7f | 65 | }else{ |
ironidio | 1:fc463dd04a7f | 66 | if(start){ |
ironidio | 1:fc463dd04a7f | 67 | //quando ricevo il terminatore del messaggio '?' |
ironidio | 1:fc463dd04a7f | 68 | if(data == '?'){//quando ricevo il terminatore del messaggio '?' |
ironidio | 1:fc463dd04a7f | 69 | |
ironidio | 1:fc463dd04a7f | 70 | //converto il vettore di char in string |
ironidio | 1:fc463dd04a7f | 71 | string strrx(vrx); |
ironidio | 1:fc463dd04a7f | 72 | //contro il numero di id che sono stati ricevuti contando le ',' |
ironidio | 1:fc463dd04a7f | 73 | n = count(strrx.begin(),strrx.end(),','); |
ironidio | 1:fc463dd04a7f | 74 | |
ironidio | 1:fc463dd04a7f | 75 | pos = 0; |
ironidio | 1:fc463dd04a7f | 76 | |
ironidio | 1:fc463dd04a7f | 77 | //faccio il parsing della stringa, salvando i diversi Id ricevuti in un vettore di stringhe |
ironidio | 1:fc463dd04a7f | 78 | while((pos = strrx.find(',')) != string::npos){ |
ironidio | 1:fc463dd04a7f | 79 | paramlist[i_row] = strrx.substr(0,pos); |
ironidio | 1:fc463dd04a7f | 80 | strrx.erase(0,pos+1); |
ironidio | 1:fc463dd04a7f | 81 | i_row++; |
ironidio | 1:fc463dd04a7f | 82 | } |
ironidio | 1:fc463dd04a7f | 83 | canread=true; //abilito la lettura da CAN |
giuseppe_guida | 2:67223774f668 | 84 | index_data = 0; |
ironidio | 1:fc463dd04a7f | 85 | }else { //altrimenti memorizzo il carattere ricevuto |
ironidio | 1:fc463dd04a7f | 86 | vrx[i]=data; |
ironidio | 1:fc463dd04a7f | 87 | i++; |
ironidio | 1:fc463dd04a7f | 88 | } |
ironidio | 1:fc463dd04a7f | 89 | |
ironidio | 1:fc463dd04a7f | 90 | |
ironidio | 1:fc463dd04a7f | 91 | } |
ironidio | 1:fc463dd04a7f | 92 | |
ironidio | 1:fc463dd04a7f | 93 | |
ironidio | 1:fc463dd04a7f | 94 | |
ironidio | 1:fc463dd04a7f | 95 | |
ironidio | 1:fc463dd04a7f | 96 | } |
giuseppe_guida | 0:b40deb141f99 | 97 | } |
giuseppe_guida | 0:b40deb141f99 | 98 | |
giuseppe_guida | 0:b40deb141f99 | 99 | int main() { |
giuseppe_guida | 2:67223774f668 | 100 | int index; |
giuseppe_guida | 2:67223774f668 | 101 | can.frequency(125000); |
giuseppe_guida | 0:b40deb141f99 | 102 | pc.attach(&rxCallback, RawSerial::RxIrq); |
giuseppe_guida | 0:b40deb141f99 | 103 | while(true){ |
ironidio | 1:fc463dd04a7f | 104 | if(canread){ |
ironidio | 1:fc463dd04a7f | 105 | //ToDo: devo leggere da CAN |
giuseppe_guida | 2:67223774f668 | 106 | if(can.read(&msg1)){ |
giuseppe_guida | 2:67223774f668 | 107 | for(index = 0; index < i_row-1; index++){ |
giuseppe_guida | 2:67223774f668 | 108 | if(msg1.id == atoi(paramlist[index].c_str())){ |
giuseppe_guida | 2:67223774f668 | 109 | string tmpData = string(msg1.data, find(msg1.data, msg1.data + 8, '\0')); |
giuseppe_guida | 2:67223774f668 | 110 | stringstream ss; |
giuseppe_guida | 2:67223774f668 | 111 | ss << msg1.id; |
giuseppe_guida | 2:67223774f668 | 112 | StoreData[index_data] = ss.str()+","+tmpData; |
giuseppe_guida | 2:67223774f668 | 113 | index_data++; |
giuseppe_guida | 2:67223774f668 | 114 | } |
giuseppe_guida | 2:67223774f668 | 115 | } |
ironidio | 1:fc463dd04a7f | 116 | }else if (stop){ |
ironidio | 1:fc463dd04a7f | 117 | //ToDo: devo mandare tutto quello che ho letto sulla seriale |
giuseppe_guida | 2:67223774f668 | 118 | //pc.puts("101,20|101,20|101,20|101,20|101,20|259,1|101,20|101,20|101,20|101,20|101,20|259,3.0?"); |
giuseppe_guida | 2:67223774f668 | 119 | for(int z = 0; z < index_data-2; z++){ |
giuseppe_guida | 2:67223774f668 | 120 | pc.puts(StoreData[z].c_str()+'|'); |
giuseppe_guida | 2:67223774f668 | 121 | //pc.putc('|'); |
giuseppe_guida | 2:67223774f668 | 122 | } |
giuseppe_guida | 2:67223774f668 | 123 | pc.puts(StoreData[index_data-1].c_str()+'?'); |
giuseppe_guida | 2:67223774f668 | 124 | |
ironidio | 1:fc463dd04a7f | 125 | stop=false; |
ironidio | 1:fc463dd04a7f | 126 | start=false; |
ironidio | 1:fc463dd04a7f | 127 | canread=false; |
ironidio | 1:fc463dd04a7f | 128 | } |
ironidio | 1:fc463dd04a7f | 129 | |
giuseppe_guida | 0:b40deb141f99 | 130 | } |
giuseppe_guida | 2:67223774f668 | 131 | } |
giuseppe_guida | 0:b40deb141f99 | 132 | } |