1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| //*****************************************code d'envoie****************************
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// 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 = 53;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(9600);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}
typedef union {
byte octet[8];
int entier[4];
} trame_can1;
trame_can1 message1;
trame_can1 message2;
typedef union {
byte octet[8];
float flottant[2];
} trame_can2;
trame_can2 message3;
trame_can2 message4;
trame_can2 message5;
trame_can2 message6;
trame_can2 message7;
trame_can2 message8;
trame_can2 message9;
trame_can2 message10;
void loop()
{
message1.entier[0]=10 ;
message1.entier[1]= 20;
message1.entier[2]= 30;
message1.entier[3]= 40;
message2.entier[0]=50 ;
message2.entier[1]= 60;
message2.entier[2]= 70;
message2.entier[3]= 80;
message3.flottant[0]= 0.1;
message3.flottant[1]= 0.2;
message4.flottant[0]=0.3 ;
message4.flottant[1]= 0.4;
message5.flottant[0]=0.5 ;
message5.flottant[1]= 0.6;
message6.flottant[0]= 0.7;
message6.flottant[1]= 0.8;
message7.flottant[0]= 0.9;
message7.flottant[1]=1.1 ;
message8.flottant[0]= 1.2;
message8.flottant[1]= 1.3;
message9.flottant[0]= 1.4;
message9.flottant[1]= 1.5;
message10.flottant[0]=1.6 ;
message10.flottant[1]=1.7 ;
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x00, 0, 8,message1.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message2.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message3.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message4.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message5.octet);
// delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message6.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message7.octet);
// delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message8.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message9.octet);
//delay(100);
CAN.sendMsgBuf(0x00, 0, 8,message10.octet);
//delay(100);
}
//*****************************code de réception***********************
// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13
#include <SPI.h>
#include "mcp_can.h"
// 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 = 53;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
typedef union {
byte octet[8];
int entier[4];
} trame_can1;
trame_can1 message1;
trame_can1 message2;
typedef union {
byte octet[8];
float flottant[2];
} trame_can2;
trame_can2 message3;
trame_can2 message4;
trame_can2 message5;
trame_can2 message6;
trame_can2 message7;
trame_can2 message8;
trame_can2 message9;
trame_can2 message10;
void setup()
{
Serial.begin(9600);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}
void loop()
{
unsigned char len = 0;
unsigned char buf1[8];
unsigned char buf2[8];
unsigned char buf3[8];
unsigned char buf4[8];
unsigned char buf5[8];
unsigned char buf6[8];
unsigned char buf7[8];
unsigned char buf8[8];
unsigned char buf9[8];
unsigned char buf10[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf1); // read data, len: data length, buf: data buf
delay(100);
CAN.readMsgBuf(&len, buf2);
delay(100);
CAN.readMsgBuf(&len, buf3);
delay(100);
CAN.readMsgBuf(&len, buf4);
delay(100);
CAN.readMsgBuf(&len, buf5);
delay(100);
CAN.readMsgBuf(&len, buf6);
delay(100);
CAN.readMsgBuf(&len, buf7);
delay(100);
CAN.readMsgBuf(&len, buf8);
delay(100);
CAN.readMsgBuf(&len, buf9);
delay(100);
CAN.readMsgBuf(&len, buf10);
delay(100);
//unsigned char canId = CAN.getCanId();
//Serial.println("-----------------------------");
//Serial.print("Get data from ID: ");
//Serial.println(canId,HEX);
for(int i = 0; i<8; i++) // print the data
{
message1.octet[i]=buf1[i];
message2.octet[i]=buf2[i];
message3.octet[i]=buf3[i];
message4.octet[i]=buf4[i];
message5.octet[i]=buf5[i];
message6.octet[i]=buf6[i];
message7.octet[i]=buf7[i];
message8.octet[i]=buf8[i];
message9.octet[i]=buf9[i];
message10.octet[i]=buf10[i];
}
Serial.print(message1.entier[0]);
Serial.print(" ");
Serial.print(message1.entier[1]);
Serial.print(" ");
Serial.print(message1.entier[2]);
Serial.print(" ");
Serial.print(message1.entier[3]);
Serial.print(" ");
Serial.print(message2.entier[0]);
Serial.print(" ");
Serial.print(message2.entier[1]);
Serial.print(" ");
Serial.print(message2.entier[2]);
Serial.print(" ");
Serial.print(message2.entier[3]);
Serial.print(" ");
Serial.print(message3.flottant[0]);
Serial.print(" ");
Serial.print(message3.flottant[1]);
Serial.print(" ");
Serial.print(message4.flottant[0]);
Serial.print(" ");
Serial.print(message4.flottant[1]);
Serial.print(" ");
Serial.print(message5.flottant[0]);
Serial.print(" ");
Serial.print(message5.flottant[1]);
Serial.print(" ");
Serial.print(message6.flottant[0]);
Serial.print(" ");
Serial.print(message6.flottant[1]);
Serial.print(" ");
Serial.print(message7.flottant[0]);
Serial.print(" ");
Serial.print(message7.flottant[1]);
Serial.print(" ");
Serial.print(message8.flottant[0]);
Serial.print(" ");
Serial.print(message8.flottant[1]);
Serial.print(" ");
Serial.print(message9.flottant[0]);
Serial.print(" ");
Serial.print(message9.flottant[1]);
Serial.print(" ");
Serial.print(message10.flottant[0]);
Serial.print(" ");
Serial.print(message10.flottant[1]);
Serial.print(" ");
Serial.println();
delay(100);
}} |
Partager