Hallo sorry i have been away, here is a short sample on using Canbus while connected to Ethernet. you have to make sure you have the correct libraries, having various canbus libraries on your system causes crashes and malfunction, you must use the MCP canbus libraries, and remove any other canbus/modbus libraries as well they really do clash and then you will think your code is wrong when its actualy the libraries.
#include <mcp_can.h> //Use only this libriary for canbus and modbus
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Change these for your network!
byte mac[] = {0xD8, 0xB0, 0x4C, 0xB5, 0x18, 0xA0}; // use your own mac id for the ethernet module
IPAddress ip(192, 168, 1, 11); // make sure your ip settings are correct
IPAddress gateway(255, 255, 255, 0);
IPAddress dest(192, 168, 17, 10);
unsigned int localPort = 8888;
unsigned int remPort = 54321;
unsigned long rxId;
byte len = 0;
byte rxBuf[8]; // this buffer is for how many pars there is to your information string with your id
char buffer[728]; // how big the entire string is
MCP_CAN CAN0(10); // Make sure your CS pin is correct, for uno it is 10 and for mega it will be 53
EthernetUDP UDP;
void setup()
{
Serial.begin(38400);
// CAN0.begin(CAN_250KBPS); // init CAN Bus with 250kb/s baudrate
CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_16MHZ); // init CAN Bus with 250kb/s baudrate at 16MHz with Mask & Filters Disabled
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(2, INPUT); // Setting pin 2, MCP2515 /INT, to input mode NB!!!!
Ethernet.begin(mac,ip); // Initialize Ethernet
UDP.begin(localPort); // Initialize the UDP listen port that is currently unused!
Serial.println("CAN to Ethernet...");
}
void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read Data: rxID = Message ID, len = Data Length, buf = Data Byte(s)
// CAN0.readMsgBuf(&len, rxBuf); // Read Data: len = Data Length, buf = Data Byte(s)
// rxId = CAN0.getCanId(); // Function will be depreciated soon due to readMsgBuf now returning ID
sprintf(buffer, "ID: %.8lX Data: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n\r",
rxId, rxBuf[0], rxBuf[1], rxBuf[2], rxBuf[3], rxBuf[4], rxBuf[5], rxBuf[6], rxBuf[7]);
UDP.beginPacket(dest, remPort);
UDP.write(buffer);
Serial.print(buffer);
UDP.endPacket();
}
}
sprintf(buffer, "ID: %.8lX Data: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n\r",
rxId, rxBuf[0], rxBuf[1], rxBuf[2], rxBuf[3], rxBuf[4], rxBuf[5], rxBuf[6], rxBuf[7]);
this sprintf buffer is the important part along with this part
CAN0.readMsgBuf(&rxId, &len, rxBuf);
you have to receive your can messages with your canbus module and then place all the data into the buffr in order to send it over Ethernet.,
Canbus to Ethernet is still easy, if you want to convert it to Modbus RTU or TCP then you will have to categorize every part of canbus input along with canbus ID individual so that you can post it onto a Http web server, i can send you an example on how to do this as well if you want it.
I hope this helps, it worked very well for me when i needed to write my software for my BMS controller