#include <P1AM.h> // Main PLC header file
#include “Longan_I2C_CAN_Arduino.h”
#include <Wire.h>
I2C_CAN CAN(0x25);// I2C Address of module
unsigned char flagRecv = 0;
unsigned char len = 128;// Was 0
unsigned char buf[8];
char str[20];
// put your main code here, to run repeatedly:
//unsigned char len = 4; // Was 0 I need 8 to make sometgin work
//unsigned char buf[8]; // was 8
unsigned char what[3]={0x07, 0X01, 0X05};
//unsigned char req[1]={0x502}
//unsigned long batSOC = 0X105;
unsigned long batID = 0X70;
unsigned long hostBatReq = 0x502; // HOST_batteryRequest
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial){
}
delay(1000);
Serial.println(“Serial Set up Complete”);
delay(250);
Serial.println(“Can Init”);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS FAIL!");
delay(100);
}
Serial.println(“CAN BUS OK!”);
pinMode(13, OUTPUT); // Where did this come from?
//TOD0 Set mask swe only get BATSOC and error code
}
void loop() {
//CAN.sendMsgBuf(batID,0,1,req);
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
if(len)
{
unsigned long canId = CAN.getCanId();
Serial.print("Get ID: ");
Serial.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i], HEX);
Serial.print("\t");
}
Serial.println("..");
}
}
delay(250);
blink();
}
void blink()
{
static unsigned long timer_s = millis();
if(millis()-timer_s < 100)return;
timer_s = millis();
digitalWrite(13, 1-digitalRead(13));
}