Volvo CAN bus mittaristotesteri

Arduino + MCP2515 CAN bus adapteri

Ohjelma CAN-viestin lähettämiseksi

#include <SPI.h>
#include "mcp_can.h"

const int spiCSPin = 10;
MCP_CAN CAN(spiCSPin);

unsigned char volvoClusterActivate[8] = {0xCC, 0x51, 0xB2, 0x2, 0x80, 0x0, 0x0, 0x0};

void setup()
{
    Serial.begin(115200);

    while (CAN_OK != CAN.begin(CAN_125KBPS, MCP_8MHz))
    {
        Serial.println("CAN BUS Init Failed");
        delay(100);
    }
    CAN.init_Mask(0,1,0);
    CAN.init_Filt(0,1,0);
    CAN.setMode(MODE_LISTENONLY);
    Serial.println("CAN BUS  Init OK!");
}

void loop()
{
    unsigned char len = 0;
    unsigned char buf[8];
    int incomingSerial = 0;

    if(CAN_MSGAVAIL == CAN.checkReceive())
    {
        CAN.readMsgBuf(&len, buf);

        unsigned long canId = CAN.getCanId();

        Serial.print("ID: 0x");
        Serial.print(canId, HEX);
        Serial.print(": ");

        for(int i = 0; i<len; i++)
        {
            Serial.print("0x");
            Serial.print(buf[i], HEX);
            Serial.print(", ");
        }
 
        Serial.println();
    }

	if (Serial.available() > 0) 
	{
		incomingByte = Serial.read();

		if(incomingSerial =='s')
		{
			CAN.setMode(MODE_NORMAL);
			Serial.print("Sending CAN request\n");

			CAN.sendMsgBuf(0xFFFFE, CAN_EXTID, 8, volvoClusterActivate);

			CAN.setMode(MODE_LISTENONLY);
		}
	}
}