add first src
This commit is contained in:
parent
598b3ecfd4
commit
436ef59532
91
src/main.c
91
src/main.c
@ -0,0 +1,91 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <linux/can.h>
|
||||||
|
#include <linux/can/raw.h>
|
||||||
|
|
||||||
|
#define NODE_ID 0x01
|
||||||
|
#define TIMING 500 // ms
|
||||||
|
|
||||||
|
long millis() {
|
||||||
|
struct timespec ts;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int s;
|
||||||
|
struct sockaddr_can addr;
|
||||||
|
struct ifreq ifr;
|
||||||
|
struct can_frame frame;
|
||||||
|
|
||||||
|
int16_t rpm = 0;
|
||||||
|
int16_t ubat = 0;
|
||||||
|
int v = 0;
|
||||||
|
int ubatr = 0;
|
||||||
|
|
||||||
|
long previous_time = 0;
|
||||||
|
long current_time = 0;
|
||||||
|
|
||||||
|
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
|
||||||
|
if (s < 0) {
|
||||||
|
perror("socket");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(ifr.ifr_name, "can0");
|
||||||
|
ioctl(s, SIOCGIFINDEX, &ifr);
|
||||||
|
|
||||||
|
addr.can_family = AF_CAN;
|
||||||
|
addr.can_ifindex = ifr.ifr_ifindex;
|
||||||
|
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
perror("bind");
|
||||||
|
close(s);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("CAN init OK\n");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
int nbytes = read(s, &frame, sizeof(struct can_frame));
|
||||||
|
|
||||||
|
if (nbytes > 0) {
|
||||||
|
|
||||||
|
switch (frame.can_id) {
|
||||||
|
|
||||||
|
case 0x382:
|
||||||
|
if (frame.can_dlc >= 2) {
|
||||||
|
rpm = frame.data[0] | (frame.data[1] << 8);
|
||||||
|
v = rpm * 0.017;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0x209:
|
||||||
|
if (frame.can_dlc >= 2) {
|
||||||
|
ubat = frame.data[0] | (frame.data[1] << 8);
|
||||||
|
ubatr = ubat / 16;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_time = millis();
|
||||||
|
|
||||||
|
if (current_time - previous_time >= TIMING) {
|
||||||
|
previous_time = current_time;
|
||||||
|
|
||||||
|
printf("Vitesse = %d\n", v);
|
||||||
|
printf("Tension = %d\n", ubatr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(s);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user