This 'Hello World' program uses many of the SEEED_CAN library functions to display the VIN number stored in a GMLAN ECU then display all CAN messages on the CAN-BUS.

Dependencies:   SEEED_CAN mbed

Fork of Seeed_CAN_Hello_World by Sophie Dexter

main.cpp

Committer:
Just4pLeisure
Date:
2013-11-05
Revision:
0:d3e39839c3e3
Child:
1:d31ce42d53a1

File content as of revision 0:d3e39839c3e3:

#include "mbed.h"
#include "seeed_can.h"

SEEED_CAN CAN(SEEED_CAN_CS);                                            // Seeed Studios' CAN-BUS Shield
Serial pc(USBTX, USBRX);                                                // USB serial port TTYACM0

Ticker minute;
Timer timestamp;

void resetTimestamp()
{
    timestamp.reset();
}

int main()
{
    SEEED_CANMessage canMsg;

    printf("Seeed Studios CAN-BUS Shield 'Hello World' program :-)\r\n");
    timestamp.start();
    minute.attach(&resetTimestamp, 60.0);                               // Reset the 'Timestamp' timer every minute
    CAN.frequency(500000);                                              // init can bus, baudrate: 500k
    CAN.Mask(0, 0x7FF);                                                 // init Masks
    CAN.Mask(1, 0x7FF, CANStandard);                                    // init Masks
    CAN.Filter(0, 0x7E8);                                               // init filters - 0x7E8 T8 ECU id
    while (CAN.read(canMsg));                                           // Empty any unfiltered messages in the receive buffers
// Read and Display the VIN code stored in a GMLAN ECU
    char VIN[18] = {NULL};
// Request VIN using ReadDataByIdentifier method using DID
    char reqvin[] = {0x02,0x1A,0x90,0x00,0x00,0x00,0x00,0x00};
    CAN.write(SEEED_CANMessage(0x7E0, reqvin));
    while (!(CAN.read(canMsg) && (canMsg.id == 0x7E8)));
    memcpy(VIN+0, canMsg.data+4, 4);
// Send Trionic8 a "Flow Control Message to get the rest of the VIN
    char floctl[] = {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    CAN.write(SEEED_CANMessage(0x7E0, floctl));
    while (!(CAN.read(canMsg) && (canMsg.id == 0x7E8)));
    memcpy(VIN+4, canMsg.data+1, 7);
    while (!(CAN.read(canMsg) && (canMsg.id == 0x7E8)));
    memcpy(VIN+11, canMsg.data+1, 6);
    printf("VIN code: %s\r\n",VIN);
// Display all messages on the CAN-BUS
    CAN.Mask(0, NULL);                                                  // Clear the mask to accept all meassages
    while (1) {
        if (CAN.read(canMsg)) {
            printf("*** T%05dI%03xL%dD", timestamp.read_ms(), canMsg.id, canMsg.len);
            for (uint32_t i=0; i<canMsg.len; i++)
                printf("%02x", canMsg.data[i]);
            printf(" ***\r");
        }
    }
}