Monitors the CANbus and prints read messages over USB Allows you to send messages via USB to the CANbus Use a terminal application like PuTTY for windows or MoSerial for Ubuntu.

Dependencies:   mbed

Committer:
Melchiorp
Date:
Tue May 31 10:22:49 2011 +0000
Revision:
0:0b6fe59204c3

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Melchiorp 0:0b6fe59204c3 1 #include "mbed.h"
Melchiorp 0:0b6fe59204c3 2 #include "CAN.h"
Melchiorp 0:0b6fe59204c3 3 #include "stdio.h"
Melchiorp 0:0b6fe59204c3 4 #include "stdlib.h"
Melchiorp 0:0b6fe59204c3 5 #include "string.h"
Melchiorp 0:0b6fe59204c3 6
Melchiorp 0:0b6fe59204c3 7 Ticker ticker;
Melchiorp 0:0b6fe59204c3 8 Serial pc(USBTX, USBRX);
Melchiorp 0:0b6fe59204c3 9 DigitalOut write_activity(LED1); //CAN activity
Melchiorp 0:0b6fe59204c3 10 DigitalOut read_activity(LED2);
Melchiorp 0:0b6fe59204c3 11 DigitalOut pc_activity(LED3); //USB activity
Melchiorp 0:0b6fe59204c3 12 //CAN can1(p9, p10); // rd, td Transmitter
Melchiorp 0:0b6fe59204c3 13 CAN can2(p30, p29); // rd, td Monitor (Now the only CAN port)
Melchiorp 0:0b6fe59204c3 14
Melchiorp 0:0b6fe59204c3 15 int counter = 0;
Melchiorp 0:0b6fe59204c3 16 char candata[8];
Melchiorp 0:0b6fe59204c3 17 char pc_msg[50];
Melchiorp 0:0b6fe59204c3 18 CANType pc_type;
Melchiorp 0:0b6fe59204c3 19 CANFormat pc_format;
Melchiorp 0:0b6fe59204c3 20 int pc_ID; //standard 11bit ID
Melchiorp 0:0b6fe59204c3 21 int pc_IDe; //extended 29 (not used yet)
Melchiorp 0:0b6fe59204c3 22 int pc_length;
Melchiorp 0:0b6fe59204c3 23 int pcd0; int pcd1; int pcd2; int pcd3; int pcd4; int pcd5; int pcd6; int pcd7; //8 bytes data
Melchiorp 0:0b6fe59204c3 24 CANMessage msg;
Melchiorp 0:0b6fe59204c3 25
Melchiorp 0:0b6fe59204c3 26 void setdata(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) {
Melchiorp 0:0b6fe59204c3 27 candata[0] = (char) (d0); // LSB
Melchiorp 0:0b6fe59204c3 28 candata[1] = (char) (d1);
Melchiorp 0:0b6fe59204c3 29 candata[2] = (char) (d2);
Melchiorp 0:0b6fe59204c3 30 candata[3] = (char) (d3);
Melchiorp 0:0b6fe59204c3 31 candata[4] = (char) (d4);
Melchiorp 0:0b6fe59204c3 32 candata[5] = (char) (d5);
Melchiorp 0:0b6fe59204c3 33 candata[6] = (char) (d6);
Melchiorp 0:0b6fe59204c3 34 candata[7] = (char) (d7); // MSB
Melchiorp 0:0b6fe59204c3 35 }
Melchiorp 0:0b6fe59204c3 36
Melchiorp 0:0b6fe59204c3 37 void canread() {
Melchiorp 0:0b6fe59204c3 38 //printf("CANmessage: %c\n", msg);
Melchiorp 0:0b6fe59204c3 39 can2.read(msg);
Melchiorp 0:0b6fe59204c3 40 pc.printf("Read: [ ID: %4x", msg.id);
Melchiorp 0:0b6fe59204c3 41 pc.printf(" Length: %d", msg.len);
Melchiorp 0:0b6fe59204c3 42 pc.printf(" Data: %2x", msg.data[0]);
Melchiorp 0:0b6fe59204c3 43 pc.printf(" %2x", msg.data[1]);
Melchiorp 0:0b6fe59204c3 44 pc.printf(" %2x", msg.data[2]);
Melchiorp 0:0b6fe59204c3 45 pc.printf(" %2x", msg.data[3]);
Melchiorp 0:0b6fe59204c3 46 pc.printf(" %2x", msg.data[4]);
Melchiorp 0:0b6fe59204c3 47 pc.printf(" %2x", msg.data[5]);
Melchiorp 0:0b6fe59204c3 48 pc.printf(" %2x", msg.data[6]);
Melchiorp 0:0b6fe59204c3 49 pc.printf(" %2x", msg.data[7]);
Melchiorp 0:0b6fe59204c3 50 pc.printf(" Type: %d", msg.type);
Melchiorp 0:0b6fe59204c3 51 pc.printf(" Format: %d ]\r\n", msg.format);
Melchiorp 0:0b6fe59204c3 52 read_activity = !read_activity; //Blink!
Melchiorp 0:0b6fe59204c3 53 //can2.reset();
Melchiorp 0:0b6fe59204c3 54 }
Melchiorp 0:0b6fe59204c3 55
Melchiorp 0:0b6fe59204c3 56 void pc_msg_read() {
Melchiorp 0:0b6fe59204c3 57 // Data to be sent as !<format>_<type>_<ID>_<length>_<d0>_<d1>_<d2>_<d3>_<d4>_<d5>_<d6>_<d7> ("_" = space)
Melchiorp 0:0b6fe59204c3 58
Melchiorp 0:0b6fe59204c3 59 // Read the string and copy it to the char array pc_msg
Melchiorp 0:0b6fe59204c3 60 pc.scanf("%[^\x0d]s",pc_msg);
Melchiorp 0:0b6fe59204c3 61
Melchiorp 0:0b6fe59204c3 62 // Read pc_msg and extract all data
Melchiorp 0:0b6fe59204c3 63 sscanf(pc_msg,"%d %d %d %d %x %x %x %x %x %x %x %x", &pc_format, &pc_type, &pc_ID, &pc_length,
Melchiorp 0:0b6fe59204c3 64 &pcd0, &pcd1, &pcd2, &pcd3, &pcd4, &pcd5, &pcd6, &pcd7);
Melchiorp 0:0b6fe59204c3 65
Melchiorp 0:0b6fe59204c3 66 // Printing extracted data, mostly for testing
Melchiorp 0:0b6fe59204c3 67 pc.printf("Entered: [ ID: %4d ",pc_ID);
Melchiorp 0:0b6fe59204c3 68 pc.printf("length: %d ",pc_length);
Melchiorp 0:0b6fe59204c3 69 pc.printf("data: %2x %2x %2x %2x %2x %2x %2x %2x ",pcd0, pcd1, pcd2, pcd3, pcd4, pcd5, pcd6, pcd7);
Melchiorp 0:0b6fe59204c3 70 pc.printf("type: %d ",pc_type);
Melchiorp 0:0b6fe59204c3 71 pc.printf("format: %d ]\r\n",pc_format);
Melchiorp 0:0b6fe59204c3 72
Melchiorp 0:0b6fe59204c3 73 // Setting the data to CANMessage.data format
Melchiorp 0:0b6fe59204c3 74 setdata(pcd0, pcd1, pcd2, pcd3, pcd4, pcd5, pcd6, pcd7);
Melchiorp 0:0b6fe59204c3 75
Melchiorp 0:0b6fe59204c3 76 // Transmitting CANMessage
Melchiorp 0:0b6fe59204c3 77 if(pc_type==0) {
Melchiorp 0:0b6fe59204c3 78 if(can2.write(CANMessage(pc_ID,candata,(char)pc_length,pc_type,pc_format))) {
Melchiorp 0:0b6fe59204c3 79 pc.printf("MBED: [ Message compiled and sent. ]\r\n");
Melchiorp 0:0b6fe59204c3 80 write_activity = !write_activity;
Melchiorp 0:0b6fe59204c3 81 }
Melchiorp 0:0b6fe59204c3 82 }
Melchiorp 0:0b6fe59204c3 83 else if(pc_type==1) {
Melchiorp 0:0b6fe59204c3 84 if(can2.write(CANMessage(pc_ID,pc_format))) {
Melchiorp 0:0b6fe59204c3 85 pc.printf("MBED: [ RTR Message compiled and sent. ]\r\n");
Melchiorp 0:0b6fe59204c3 86 write_activity = !write_activity;
Melchiorp 0:0b6fe59204c3 87 }
Melchiorp 0:0b6fe59204c3 88 }
Melchiorp 0:0b6fe59204c3 89 //can2.reset();
Melchiorp 0:0b6fe59204c3 90 }
Melchiorp 0:0b6fe59204c3 91
Melchiorp 0:0b6fe59204c3 92 void initcan() {
Melchiorp 0:0b6fe59204c3 93 can2.frequency(1000000); //1Mbit/s
Melchiorp 0:0b6fe59204c3 94 can2.reset();
Melchiorp 0:0b6fe59204c3 95 can2.attach(&canread);
Melchiorp 0:0b6fe59204c3 96 }
Melchiorp 0:0b6fe59204c3 97
Melchiorp 0:0b6fe59204c3 98 int main() {
Melchiorp 0:0b6fe59204c3 99 //----------------Initialization-----------------------
Melchiorp 0:0b6fe59204c3 100 initcan();
Melchiorp 0:0b6fe59204c3 101 //can1.frequency(1000000);
Melchiorp 0:0b6fe59204c3 102 //can2.monitor(1); // If you want the node to only monitor the CAN network.
Melchiorp 0:0b6fe59204c3 103 pc.baud(115200); //Tested, works. Set terminal to the same. Also works with the c file.
Melchiorp 0:0b6fe59204c3 104 //ticker.attach(&cansend, 0.5); //Send every 1 seconds.
Melchiorp 0:0b6fe59204c3 105 //-----------------------------------------------------
Melchiorp 0:0b6fe59204c3 106
Melchiorp 0:0b6fe59204c3 107 char test;
Melchiorp 0:0b6fe59204c3 108 char dump;
Melchiorp 0:0b6fe59204c3 109 pc.printf(" [ Please enter !format_type_ID_length_d0_d1_d2_d3_d4_d5_d6_d7 ('_' = space) ]\r\n");
Melchiorp 0:0b6fe59204c3 110
Melchiorp 0:0b6fe59204c3 111 while(1) {
Melchiorp 0:0b6fe59204c3 112 if (pc.readable()) { // If there's data available from pc
Melchiorp 0:0b6fe59204c3 113 pc_activity = !pc_activity; // LED
Melchiorp 0:0b6fe59204c3 114 test = pc.getc();
Melchiorp 0:0b6fe59204c3 115 if (test == '!') { // See if it's a valid message
Melchiorp 0:0b6fe59204c3 116 pc_msg_read(); // Valid => read the message and extract the data
Melchiorp 0:0b6fe59204c3 117 //pc.printf("Left pc_msg_read\n"); // "Left the function" test
Melchiorp 0:0b6fe59204c3 118 }
Melchiorp 0:0b6fe59204c3 119 else { // Invalid data or leftover characters
Melchiorp 0:0b6fe59204c3 120 pc.printf(" [ Dumped: ");
Melchiorp 0:0b6fe59204c3 121 while(pc.readable()) { // Keep dumping the leftovers
Melchiorp 0:0b6fe59204c3 122 dump = pc.getc();
Melchiorp 0:0b6fe59204c3 123 pc.printf("%c",dump);
Melchiorp 0:0b6fe59204c3 124 }
Melchiorp 0:0b6fe59204c3 125 pc.printf(" ]\r\n");
Melchiorp 0:0b6fe59204c3 126 }
Melchiorp 0:0b6fe59204c3 127 //pc.printf("Leaving pc.readable()\n"); // Left the !test
Melchiorp 0:0b6fe59204c3 128 }
Melchiorp 0:0b6fe59204c3 129 }
Melchiorp 0:0b6fe59204c3 130 }