test can console

Dependencies:   mbed

Fork of CAN by Carlos Almeida

Revision:
2:be902ab7d787
Parent:
1:c022a879e21c
diff -r c022a879e21c -r be902ab7d787 main.cpp
--- a/main.cpp	Tue Feb 06 07:22:11 2018 +0000
+++ b/main.cpp	Tue Feb 06 11:01:08 2018 +0000
@@ -22,14 +22,15 @@
 char value = 0;
 uint8_t state = 0;
 uint16_t identifier = 0x0539;
-
+uint8_t bytes;
+char message[8];
 
-void send(char counter) {
+void send() {
     //pc.printf("send()\n");
-    if(can1.write(CANMessage(identifier, &counter, 8,))) {
+    if(can1.write(CANMessage(identifier, message, bytes,CANRemote))) {
         //pc.printf("wloop()\n");
         //counter++;
-        pc.printf("Message sent: %d\r\n", counter);
+        pc.printf("Message sent: %d\r\n", message);
     } 
     led1 = !led1;
 }
@@ -39,9 +40,9 @@
     //ticker.attach(&send, 1);
     CANMessage msg;
     char c;
-    uint16_t instruction;
-    uint16_t instruction_2;
-    uint8_t bytes;
+    uint64_t instruction;
+    
+    
     int8_t state_val;
     uint8_t data_report[100];
     uint8_t data_rc;
@@ -82,18 +83,22 @@
         wait(1.0);
             
             pc.printf("Endianness Configuration \r\n");
-            pc.printf("1 - Big Endian \r\n");
-            pc.printf("2 - Little Endian \r\n");
+            pc.printf("1 - Choose Big/Little Endian \r\n");
+            pc.printf("2 - Back to menu \r\n");
             
             c = pc.getc();
             
             if(c == '1'){
                 endianness = BIG_ENDIAN;
+                instruction = message[0];
+                message[0] = message[1];
                 pc.printf("\r\n1\r\n");
                 }
                 
                 if(c == '2'){
                 endianness = LITTLE_ENDIAN;
+                instruction = message[0];
+                message[0] = message[1];
                 pc.printf("\r\n2\r\n");
                 }
             state = MENU_DISPLAY;
@@ -116,24 +121,27 @@
         if(c == '4'){//envoie valeur
         pc.printf("4 \r\n\n");
         wait(1.0);
-        
+        send();
+        /*
         switch(bytes){//calcul nombre octet présent
-            
-            case 1://si 1 octet present envoie 1 octet 
-        send(instruction);
+              case 1://si 1 octet present envoie 1 octet 
+        send();
             break;
             
             case 2://si 2 octets present envoie 2 octets 1 par 1
             if(endianness == BIG_ENDIAN){
-            send(instruction_2);
-            send(instruction);}
+           // send(instruction_2);
+            send();
+            }
             
             if(endianness == LITTLE_ENDIAN){
-            send(instruction);
-            send(instruction_2);}
+            //send(instruction);
+            //send(instruction_2);
+            }
             break;
             
             }//switch
+            */
             
         state = MENU_DISPLAY; //on revient au menu de départ apres la capture de données
         
@@ -510,19 +518,110 @@
            break;
            
            case '\r': //si au sérial nous avons le retour de ligne
-           pc.printf("\r\n0x%x \r\n", instruction);
+           pc.printf("\r\Done \r\n");
            wait(1.0);
            state_val = 0;
            state = MENU_DISPLAY;
            //calcul du nombre d'octet en fonction de la valeur finale
-           if(instruction <= 255){
+           if(instruction <= 0xFF){
                bytes = 1;
+               message[0] = instruction;
+               message[1] = 0;
+               message[2] = 0;
+               message[3] = 0;
+               message[4] = 0;
+               message[5] = 0;
+               message[6] = 0;
+               message[7] = 0;
+               
+               }
+               if(instruction > 0xFF && instruction <= 0xFFFF){
+               bytes = 2;
+               message[1] = instruction & 0x00FF;
+               instruction = instruction >> 8;
+               message[0] = instruction;
+               message[2] = 0;
+               message[3] = 0;
+               message[4] = 0;
+               message[5] = 0;
+               message[6] = 0;
+               message[7] = 0;
+               }
+               if(instruction > 0xFFFF && instruction <= 0xFFFFFF){
+               bytes = 3;
+               message[2] = instruction & 0x0000FF;
+               message[1] = (instruction >> 8) & 0x00FF;
+               instruction = instruction >> 16;
+               message[0] = instruction & 0xFF;
+               message[3] = 0;
+               message[4] = 0;
+               message[5] = 0;
+               message[6] = 0;
+               message[7] = 0;
+               }
+               
+               if(instruction > 0xFFFFFF && instruction <= 0xFFFFFFFF){
+               bytes = 4;
+               message[3] = instruction & 0x000000FF;
+               message[2] = (instruction >> 8) & 0x0000FF;
+               message[1] = (instruction >> 16) & 0x00FF;
+               instruction = instruction >> 24;
+               message[0] = instruction & 0xFF;
+               message[4] = 0;
+               message[5] = 0;
+               message[6] = 0;
+               message[7] = 0;
                }
-               if(instruction > 255){
-               bytes = 2;
-               instruction_2 = instruction;
-               instruction_2 = instruction_2 >> 8;
-               instruction = instruction & 0b0000000011111111;
+               
+               if(instruction > 0xFFFFFFFF && instruction <= 0xFFFFFFFFFF){
+               bytes = 5;
+               message[4] = instruction & 0x00000000FF;
+               message[3] = (instruction >> 8) & 0x000000FF;
+               message[2] = (instruction >> 16) & 0x0000FF;
+               message[1] = (instruction >> 24) & 0x00FF;
+               instruction = instruction >> 32;
+               message[0] = instruction & 0xFF;
+               message[5] = 0;
+               message[6] = 0;
+               message[7] = 0;
+               }
+               
+               if(instruction > 0xFFFFFFFFFF && instruction <= 0xFFFFFFFFFFFF){
+               bytes = 6;
+               message[5] = instruction & 0x0000000000FF;
+               message[4] = (instruction >> 8) & 0x00000000FF;
+               message[3] = (instruction >> 16) & 0x000000FF;
+               message[2] = (instruction >> 24) & 0x0000FF;
+               message[1] = (instruction >> 32) & 0x00FF;
+               instruction = instruction >> 40;
+               message[0] = instruction & 0xFF;
+               message[6] = 0;
+               message[7] = 0;
+               }
+               
+               if(instruction > 0xFFFFFFFFFFFF && instruction <= 0xFFFFFFFFFFFFFF){
+               bytes = 7;
+               message[6] = instruction & 0x000000000000FF;
+               message[5] = (instruction >> 8) & 0x0000000000FF;
+               message[4] = (instruction >> 16) & 0x00000000FF;
+               message[3] = (instruction >> 24) & 0x000000FF;
+               message[2] = (instruction >> 32) & 0x0000FF;
+               message[1] = (instruction >> 40) & 0x00FF;
+               instruction = instruction >> 48;
+               message[0] = instruction & 0xFF;
+               message[7] = 0;
+               }
+               if(instruction > 0xFFFFFFFFFFFFFF && instruction <= 0xFFFFFFFFFFFFFFFF){
+               bytes = 8;
+               message[7] = instruction & 0x00000000000000FF;
+               message[6] = (instruction >> 8) & 0x000000000000FF;
+               message[5] = (instruction >> 16) & 0x0000000000FF;
+               message[4] = (instruction >> 24) & 0x00000000FF;
+               message[3] = (instruction >> 32) & 0x000000FF;
+               message[2] = (instruction >> 40) & 0x0000FF;
+               message[1] = (instruction >> 48) & 0x00FF;
+               instruction = instruction >> 56;
+               message[0] = instruction & 0xFF;               
                }
            break;
            
@@ -894,9 +993,3 @@
         //wait(0.2);
     }
 }
-
- /*printf("loop()\n");
-        if(can2.read(msg)) {
-            printf("Message received: %d\n", msg.data[0]);
-            led2 = !led2;
-        } */
\ No newline at end of file