-
Notifications
You must be signed in to change notification settings - Fork 0
/
float-receiver-1.ino
96 lines (85 loc) · 2.83 KB
/
float-receiver-1.ino
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
#include <CAN.h>
#define TX_GPIO_NUM 21 // Connects to CTX
#define RX_GPIO_NUM 22 // Connects to CRX
//==================================================================================//
void setup() {
// pinMode(TX_GPIO_NUM,OUTPUT_OPEN_DRAIN);
Serial.begin (115200);
while (!Serial);
delay (1000);
Serial.println ("CAN Receiver/Receiver");
// Set the pins
CAN.setPins (RX_GPIO_NUM, TX_GPIO_NUM);
// start the CAN bus at 500 kbps
if (!CAN.begin (1000E3)) {
Serial.println ("Starting CAN failed!");
while (1);
}
else {
Serial.println ("CAN Initialized");
}
// CAN.filter(0x12);
// CAN.filter(0x13);
}
//==================================================================================//
void loop() {
canReceiver();
}
//==================================================================================//
void filter() {
unsigned long packetId = CAN.packetId();
// Define a hexadecimal value to compare with
unsigned long targetId = 0x12; // Replace with your desired packet ID
// Check if the received packet ID matches the target ID
if (packetId == targetId) {
// This block will execute if the received packet ID matches the target ID
Serial.println("Data from Master 0x");
Serial.println(packetId, HEX);
// Perform actions based on the matching packet ID
}
else {
// This block will execute if the received packet ID does not match the target ID
Serial.print("Data from CAN Node 0x");
Serial.println(packetId, HEX);
// Optionally, perform other actions for non-matching IDs
}
}
void canReceiver() {
// Try to parse a CAN packet
int packetSize = CAN.parsePacket();
if (packetSize) {
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
Serial.print("RTR ");
}
filter();
// Check if the received packet ID matches the expected ID
if (CAN.packetId() == 0x13) { // Replace with your expected ID
if (!CAN.packetRtr()) {
Serial.print(" and length ");
Serial.println(packetSize);
// Read the bytes and interpret them as float values
float receivedValues[8];
for (int i = 0; i < 8; i++) {
uint8_t byteValue = CAN.read();
receivedValues[i] = (float)byteValue / 76;
}
// Print the received float values
for (int i = 0; i < 8; i++) {
Serial.print("Received Value ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(receivedValues[i], 2); // Print with 2 decimal places
}
}
} else {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
}
Serial.println();
}
}
//==================================================================================//