![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test can console
Dependencies: mbed
Fork of CAN by
main.cpp
- Committer:
- protongamer
- Date:
- 2018-02-06
- Revision:
- 1:c022a879e21c
- Parent:
- 0:cea062e60f85
- Child:
- 2:be902ab7d787
File content as of revision 1:c022a879e21c:
//Terminal CAN projet bts sn Enzo #include "mbed.h" #define BIG_ENDIAN 0 #define LITTLE_ENDIAN 1 #define MENU_DISPLAY 0 #define MENU_CHOOSE 1 #define MENU_SENDING_DISPLAY 2 #define MENU_SENDING 3 #define SET_ID 4 Ticker ticker; DigitalOut led1(LED1); DigitalOut led2(LED2); Serial pc(USBTX, USBRX); CAN can1(p9, p10); CAN can2(p30, p29); char value = 0; uint8_t state = 0; uint16_t identifier = 0x0539; void send(char counter) { //pc.printf("send()\n"); if(can1.write(CANMessage(identifier, &counter, 8,))) { //pc.printf("wloop()\n"); //counter++; pc.printf("Message sent: %d\r\n", counter); } led1 = !led1; } int main() { //ticker.attach(&send, 1); CANMessage msg; char c; uint16_t instruction; uint16_t instruction_2; uint8_t bytes; int8_t state_val; uint8_t data_report[100]; uint8_t data_rc; uint8_t endianness = 0; while(1) { switch(state){ case MENU_DISPLAY://menu affichage pc.printf("\r\n\n\n"); pc.printf("CAN BUS - TERMINAL \r\n"); pc.printf("1 - Set the value to be sent\r\n"); pc.printf("2 - Set Endianness \r\n"); pc.printf("3 - Set Identifier \r\n"); pc.printf("4 - Send the value \r\n"); state = 1; break; case MENU_CHOOSE://choix menu c = pc.getc(); if(c == '1'){//attribution valeur a envoyer instruction = 0; state_val = 0; pc.printf("1 \r\n\n"); wait(1.0); state = MENU_SENDING_DISPLAY; } if(c == '2'){ pc.printf("2 \r\n\n"); wait(1.0); pc.printf("Endianness Configuration \r\n"); pc.printf("1 - Big Endian \r\n"); pc.printf("2 - Little Endian \r\n"); c = pc.getc(); if(c == '1'){ endianness = BIG_ENDIAN; pc.printf("\r\n1\r\n"); } if(c == '2'){ endianness = LITTLE_ENDIAN; pc.printf("\r\n2\r\n"); } state = MENU_DISPLAY; wait(1.0); } if(c == '3'){ pc.printf("3 \r\n\n"); wait(1.0); pc.printf("Set the identifier(hexa) : 0x"); state = SET_ID; } if(c == '4'){//envoie valeur pc.printf("4 \r\n\n"); wait(1.0); switch(bytes){//calcul nombre octet présent case 1://si 1 octet present envoie 1 octet send(instruction); break; case 2://si 2 octets present envoie 2 octets 1 par 1 if(endianness == BIG_ENDIAN){ send(instruction_2); send(instruction);} if(endianness == LITTLE_ENDIAN){ send(instruction); send(instruction_2);} break; }//switch state = MENU_DISPLAY; //on revient au menu de départ apres la capture de données //sequence de capture de données de retour for(int i = 0; i <= 500; i++){ if(!can1.read(msg)) { pc.printf("awaiting data\r\n");} if(can1.read(msg)) { pc.printf("Message received: %d \r\n", msg.data[0]); data_report[data_rc] = msg.data[0]; data_rc++; led2 = !led2; wait(0.1); } }//for int i pc.printf("\r\nDone\r\n\n"); //capture de données terminées, on fait un rapport des données collectées for(int i = 0; i < data_rc; i++){ pc.printf("Message captured: %d \r\n", data_report[i]); } data_rc = 0; }//if c == '2' break;// case MENU_SENDING_DISPLAY://choix de la valeur a envoyer pc.printf("Set the value to be sent(hex value) : 0x"); state = 3; break; case MENU_SENDING://demande de valeur a envoyer c = pc.getc(); //state_val correspond a la premiere valeur, si la premiere valeur n'est pas encore entrée, elle attribué a la variable instruction //dans l'autre cas, on décale de 4 rang les 4 bits de l'ancienne valeur pour l'ajouter à la nouvelle switch(c){ case '0': pc.printf("0"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0000; } if(state_val == 0){ instruction = 0b0000; state_val++; } break; case '1': if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0001; } pc.printf("1"); if(state_val == 0){ instruction = 0b0001; state_val++; } break; case '2': if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0010; } pc.printf("2"); if(state_val == 0){ instruction = 0b0010; state_val++; } break; case '3': pc.printf("3"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0011; } if(state_val == 0){ instruction = 0b0011; state_val++; } break; case '4': if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0100; } pc.printf("4"); if(state_val == 0){ instruction = 0b0100; state_val++; } break; case '5': pc.printf("5"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0101; } if(state_val == 0){ instruction = 0b0101; state_val++; } break; case '6': pc.printf("6"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0110; } if(state_val == 0){ instruction = 0b0110; state_val++; } break; case '7': pc.printf("7"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b0111; } if(state_val == 0){ instruction = 0b0111; state_val++; } break; case '8': pc.printf("8"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1000; } if(state_val == 0){ instruction = 0b1000; state_val++; } break; case '9': pc.printf("9"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1001; } if(state_val == 0){ instruction = 0b1001; state_val++; } break; case 'a': pc.printf("a"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1010; } if(state_val == 0){ instruction = 0b1010; state_val++; } break; case 'b': pc.printf("b"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1011; } if(state_val == 0){ instruction = 0b1011; state_val++; } break; case 'c': pc.printf("c"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1100; } if(state_val == 0){ instruction = 0b1100; state_val++; } break; case 'd': pc.printf("d"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1101; } if(state_val == 0){ instruction = 0b1101; state_val++; } break; case 'e': pc.printf("e"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1110; } if(state_val == 0){ instruction = 0b1110; state_val++; } break; case 'f': pc.printf("f"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1111; } if(state_val == 0){ instruction = 0b1111; state_val++; } break; case 'A': pc.printf("a"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1010; } if(state_val == 0){ instruction = 0b1010; state_val++; } break; case 'B': pc.printf("b"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1011; } if(state_val == 0){ instruction = 0b1011; state_val++; } break; case 'C': pc.printf("c"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1100; } if(state_val == 0){ instruction = 0b1100; state_val++; } break; case 'D': pc.printf("d"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1101; } if(state_val == 0){ instruction = 0b1101; state_val++; } break; case 'E': pc.printf("e"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1110; } if(state_val == 0){ instruction = 0b1110; state_val++; } break; case 'F': pc.printf("f"); if(state_val > 0){ instruction = instruction << 4; instruction |= 0b1111; } if(state_val == 0){ instruction = 0b1111; state_val++; } break; case '\r': //si au sérial nous avons le retour de ligne pc.printf("\r\n0x%x \r\n", instruction); wait(1.0); state_val = 0; state = MENU_DISPLAY; //calcul du nombre d'octet en fonction de la valeur finale if(instruction <= 255){ bytes = 1; } if(instruction > 255){ bytes = 2; instruction_2 = instruction; instruction_2 = instruction_2 >> 8; instruction = instruction & 0b0000000011111111; } break; } break; case SET_ID: c = pc.getc(); switch(c){ case '0': pc.printf("0"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0000; } if(state_val == 0){ identifier = 0b0000; state_val++; } break; case '1': if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0001; } pc.printf("1"); if(state_val == 0){ identifier = 0b0001; state_val++; } break; case '2': if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0010; } pc.printf("2"); if(state_val == 0){ identifier = 0b0010; state_val++; } break; case '3': pc.printf("3"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0011; } if(state_val == 0){ identifier = 0b0011; state_val++; } break; case '4': if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0100; } pc.printf("4"); if(state_val == 0){ identifier = 0b0100; state_val++; } break; case '5': pc.printf("5"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0101; } if(state_val == 0){ identifier = 0b0101; state_val++; } break; case '6': pc.printf("6"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0110; } if(state_val == 0){ identifier = 0b0110; state_val++; } break; case '7': pc.printf("7"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b0111; } if(state_val == 0){ identifier = 0b0111; state_val++; } break; case '8': pc.printf("8"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1000; } if(state_val == 0){ identifier = 0b1000; state_val++; } break; case '9': pc.printf("9"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1001; } if(state_val == 0){ identifier = 0b1001; state_val++; } break; case 'a': pc.printf("a"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1010; } if(state_val == 0){ identifier = 0b1010; state_val++; } break; case 'b': pc.printf("b"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1011; } if(state_val == 0){ identifier = 0b1011; state_val++; } break; case 'c': pc.printf("c"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1100; } if(state_val == 0){ identifier = 0b1100; state_val++; } break; case 'd': pc.printf("d"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1101; } if(state_val == 0){ identifier = 0b1101; state_val++; } break; case 'e': pc.printf("e"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1110; } if(state_val == 0){ identifier = 0b1110; state_val++; } break; case 'f': pc.printf("f"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1111; } if(state_val == 0){ identifier = 0b1111; state_val++; } break; case 'A': pc.printf("a"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1010; } if(state_val == 0){ identifier = 0b1010; state_val++; } break; case 'B': pc.printf("b"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1011; } if(state_val == 0){ identifier = 0b1011; state_val++; } break; case 'C': pc.printf("c"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1100; } if(state_val == 0){ identifier = 0b1100; state_val++; } break; case 'D': pc.printf("d"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1101; } if(state_val == 0){ identifier = 0b1101; state_val++; } break; case 'E': pc.printf("e"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1110; } if(state_val == 0){ identifier = 0b1110; state_val++; } break; case 'F': pc.printf("f"); if(state_val > 0){ identifier = identifier << 4; identifier |= 0b1111; } if(state_val == 0){ identifier = 0b1111; state_val++; } break; case '\r': //si au sérial nous avons le retour de ligne pc.printf("\r\n0x%x \r\n", identifier); wait(1.0); state = MENU_DISPLAY; state_val = 0; break; } break; } //wait(0.2); } } /*printf("loop()\n"); if(can2.read(msg)) { printf("Message received: %d\n", msg.data[0]); led2 = !led2; } */