Howdy! This is my first post so bear with me if its bad. I'm working on a canbus gauge for my 1988 rx7. Right now I'm testing this on my 2009 328i. I'm using an arduino uno and the seeedstuido Serial CAN-BUS Module. I have the unit hooked up to my car and started using a couple different test codes. Right now the furtest I could get was the program kicking back "Starting Can Failed".
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <CAN.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Receiver");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = CAN.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr()) {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
} else {
Serial.print(" and length ");
Serial.println(packetSize);
// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print((char)CAN.read());
}
Serial.println();
}
Serial.println();
}
}
I'm using this walk through https://www.instructables.com/id/Yes-We-CAN-BUS-With-Arduino-in-30-Seconds/
I tried using the code above but I'm unsure how to connect the can controller using this code.
I tried this code too which I like more because it shows what pins need to be connected. Though this program just spits trash into the serial monitor.
// RECV EXAMPLE OF SERIAL CAN MODULE
// unsigned char recv(unsigned long *id, uchar *buf);
// SUPPORT: joney.sui@longan-labs.cc
#include <Serial_CAN_Module.h>
#include <SoftwareSerial.h>
Serial_CAN can;
#define can_tx 2 // tx of serial can module connect to D2
#define can_rx 3 // rx of serial can module connect to D3
void setup()
{
Serial.begin(10400);
can.begin(can_tx, can_rx, 10400); // tx, rx
Serial.println("begin");
}
unsigned long id = 0;
unsigned char dta[8];
// send(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf);
void loop()
{
if(can.recv(&id, dta))
{
Serial.print("GET DATA FROM ID: ");
Serial.println(id);
for(int i=0; i<8; i++)
{
Serial.print("0x");
Serial.print(dta[i], HEX);
Serial.print('\t');
}
Serial.println();
}
}
// END FILE