C#:
// ECUHARD CANBUS SHIELD V2 ILE YAPILDI ARDUINO UNO KULLANILDI
// https://wiki.seeedstudio.com/CAN-BUS_Shield_V2.0/
#include <SPI.h>
#define CAN_2515
// #define CAN_2518FD
// Set SPI CS Pin according to your hardware
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else
// For Arduino MCP2515 Hat:
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
#endif
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif
#include <SoftwareSerial.h>
SoftwareSerial stabil(A4,A5); // RX | TX
unsigned long n = 100;
int y=0;
int x=0;
byte R[4];
int rpm=7000;
int ws=0;
int oilPress=0;
int oilTemp=0;
int clt=0;
int fuelPress=0;
float battery=0;
int tps=0;
int mapp=0;
int iat=0;
int egt=0;
float lambda=0;
int fuelTemp=0;
int say=0;
bool blink=false;
int gear=0;
int errors=0;
char xStr[5];
void setup()
{
Serial.begin(115200);
stabil.begin(19200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) {
if(Serial){
Serial.println("CAN bağlantı hatası");
delay(100);
}}
if(Serial){
Serial.println("CAN bağlantı tamam");
}
delay(10);
}
unsigned char RPM_BYTE[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char CLT_BYTE[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char GEAR_BYTE[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char OTHER_BYTE[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char WS_BYTE[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char TRACTION_BYTE[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop()
{
if(Serial){
Serial.print(" RPM:"); Serial.print(rpm);
Serial.print(" CLT:"); Serial.print(clt);
Serial.print(" BTT:"); Serial.print(battery);
Serial.print(" TPS:"); Serial.print(tps);
Serial.print(" İAT:"); Serial.print(iat);
Serial.print(" MAP:"); Serial.print(mapp);
Serial.print(" GEAR:"); Serial.print(gear);
Serial.print(" BLINK:"); Serial.print(blink);
Serial.print(" ERR:"); Serial.println(errors);
}
//////////////////////////// BLİNK
if(say<10){blink=true;} else {blink=false;}
if(say>20){say=0;}
say++;
//////////////////////////// BLİNK
//////////////////////////// RPM
int rMSB = rpm / 40;
int rLSB = (rpm - (rMSB * 40)) * 6.4;
RPM_BYTE[0] = 0xB0;
RPM_BYTE[1] = 0x0;
RPM_BYTE[2] = rLSB;
RPM_BYTE[3] = rMSB;
RPM_BYTE[4] = 0x0;
RPM_BYTE[5] = 0x0;
RPM_BYTE[6] = 0x0;
RPM_BYTE[7] = 0x0;
CAN.sendMsgBuf(0x316, 0, 8, RPM_BYTE);
//////////////////////////// RPM
//////////////////////////// CLT
int CLT_HAM=clt*2 + 20;
if(CLT_HAM>254){CLT_HAM=254;}
CLT_BYTE[0] = 0xD9;
CLT_BYTE[1] = CLT_HAM;
CLT_BYTE[2] = 0xB2;
CLT_BYTE[3] = 0xC;
CLT_BYTE[4] = 0x0;
CLT_BYTE[5] = (tps*0.390); // TPS
CLT_BYTE[6] = 0x0; // KICKDOWN
CLT_BYTE[7] = 0x0;
CAN.sendMsgBuf(0x329, 0, 8, CLT_BYTE);
//////////////////////////// CLT
//////////////////////////// GEAR
if(gear == 5){ gear = 9; }
GEAR_BYTE[0] = 0x81;
GEAR_BYTE[1] = gear;
GEAR_BYTE[2] = 0xFF;
GEAR_BYTE[3] = 0xFF;
GEAR_BYTE[4] = 0x00;
GEAR_BYTE[5] = 0x80;
GEAR_BYTE[6] = 0xFF;
GEAR_BYTE[7] = 0x00;
CAN.sendMsgBuf(0x43F, 0, 8, GEAR_BYTE);
//////////////////////////// GEAR
//////////////////////////// OTHER
int mMSB = tps;
int mLSB = tps;
if(rpm>6000 && blink){OTHER_BYTE[0] = 0x02;}else{OTHER_BYTE[0] = 0x81;} // ARIZA LAMBASI
OTHER_BYTE[1] = mLSB;
OTHER_BYTE[2] = mMSB;
if(clt > 100 && blink){OTHER_BYTE[3] = 0xFF;} else {OTHER_BYTE[3] = 0x00;} // HARARET LAMBASI
OTHER_BYTE[4] = 0x00;
OTHER_BYTE[5] = 0x90;
OTHER_BYTE[6] = 0xFF;
OTHER_BYTE[7] = 0x00;
CAN.sendMsgBuf(0x545, 0, 8, OTHER_BYTE);
//////////////////////////// OTHER
//////////////////////////// TRACTION
TRACTION_BYTE[0] = 0x00;
TRACTION_BYTE[1] = 0x00;
TRACTION_BYTE[2] = 0x00;
TRACTION_BYTE[3] = 0x00;
TRACTION_BYTE[4] = 0x00;
TRACTION_BYTE[5] = 0x00;
TRACTION_BYTE[6] = 0x00;
TRACTION_BYTE[7] = 0x00;
CAN.sendMsgBuf(0x153, 0, 8, TRACTION_BYTE);
//////////////////////////// TRACTION
comm();
delay(10);
}
void comm()
{
//The datastream is standard RS232 at 19200,n,8,1. It consists of a number of short packets. Packets are
//sent on 10 ms ticks. Note that this does not mean that there is a packet sent every 10 ms tick – there is
//a pattern which repeats once a second to achieve the channel frequencies listed below, and there are
//some unused ticks where nothing is trasmitted. Each packet consists of 5 bytes. The first byte is the
//channel number, the second is always A3H, the third and fourth are the channel value, high byte first.
//The fifth is the sum of the preceeding four bytes.
while(stabil.available() >= 4)
{
stabil.readBytes(R,5); // Read 4 bytes and write it to R[4]
if(R[1] == 163)
{
switch (R[0])
{
case 1:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
rpm = word(R[2], R[3]);
}
break;
case 5:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
ws = word(R[2], R[3]);
ws=ws/10;
}
break;
case 9:
//y =(R[0]+R[1]+R[2]+R[3]);
//if(y>=256){y=y-255;} // 106
//if(R[4] == HEX,y)
//{
oilPress = word(R[2], R[3]);
oilPress=oilPress/1000;
//}
break;
case 13:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
oilTemp = word(R[2], R[3]);
oilTemp=(oilTemp/10)-100;
}
break;
case 17:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
clt = word(R[2], R[3]);
clt=(clt/10)-100;
}
break;
case 21:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
fuelPress = word(R[2], R[3]);
fuelPress=fuelPress/1000;
}
break;
case 33:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
battery = word(R[2], R[3]);
battery=battery/100;
}
break;
case 45:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
tps = word(R[2], R[3]);
tps=tps/10;
}
break;
case 69:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
mapp = word(R[2], R[3]);
mapp=mapp/10;
}
break;
case 97:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
iat = word(R[2], R[3]);
iat=(iat/10)-100;
}
break;
case 101:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
egt = word(R[2], R[3]);
egt=(egt/10)-100;
}
break;
case 105:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
lambda = word(R[2], R[3]);
lambda=lambda/1000;
}
break;
case 109:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
fuelTemp = word(R[2], R[3]);
fuelTemp=(fuelTemp/10)-100;
}
break;
case 113:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
gear = word(R[2], R[3]);
}
break;
case 125:
y =(R[0]+R[1]+R[2]+R[3]);
if(y>=256){y=y-255;} // 106
if(R[4] == HEX,y)
{
errors = word(R[2], R[3]);
}
break;
default:
break;
}
} else {
stabil.end();
delay(100);
stabil.begin(19200);
}
}
}