aboutsummaryrefslogtreecommitdiff
path: root/old/mycan_live.c
diff options
context:
space:
mode:
Diffstat (limited to 'old/mycan_live.c')
-rw-r--r--old/mycan_live.c403
1 files changed, 403 insertions, 0 deletions
diff --git a/old/mycan_live.c b/old/mycan_live.c
new file mode 100644
index 0000000..355dd24
--- /dev/null
+++ b/old/mycan_live.c
@@ -0,0 +1,403 @@
+// https://www.waveshare.com/wiki/RS485_CAN_HAT
+// https://github.com/howerj/dbcc MIT
+
+/* build:
+cc -Wall -c all_SRETest.c
+cc -Wall -c mycan.c
+cc -Wall mycan.o all_SRETest.o -o mycan
+*/
+
+/* test:
+sudo ip link add type vcan
+sudo ip link set dev vcan0 type vcan
+sudo ip link set dev vcan0 up
+cansend vcan0 630#02.00.02.00.02.00.02.00
+cansend vcan0 680#02.00.02.00.02.00.02.00
+# if cell temperature is signed (can be negative), 00.FF sends -256
+cansend vcan0 680#00.FF.02.00.02.00.02.00
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+// (GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause
+#include <linux/can.h>
+#include <linux/can/raw.h>
+
+#include "all_SRE_edited.h"
+
+// https://github.com/howerj/dbcc MIT
+static uint64_t u64_from_can_msg(const uint8_t m[8]) {
+ return ((uint64_t)m[7] << 56) | ((uint64_t)m[6] << 48) | ((uint64_t)m[5] << 40) | ((uint64_t)m[4] << 32)
+ | ((uint64_t)m[3] << 24) | ((uint64_t)m[2] << 16) | ((uint64_t)m[1] << 8) | ((uint64_t)m[0] << 0);
+}
+
+int main(int argc, char **argv)
+{
+ int ret;
+ int s, nbytes;
+ struct sockaddr_can addr;
+ struct ifreq ifr;
+ struct can_frame frame;
+
+ can_obj_all_sre_edited_h_t obj;
+ dbcc_time_stamp_t t = 0;
+ uint64_t can_message_u64;
+ //memset(&obj, 0, sizeof(can_obj_all_sretest_h_t));
+
+ memset(&frame, 0, sizeof(struct can_frame));
+
+ //system("sudo ip link set can0 type can bitrate 500000");
+ //system("sudo ifconfig can0 up");
+ // vcan, for test
+ printf("this is a can receive demo\r\n");
+
+ //1.Create socket
+ s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (s < 0) {
+ perror("socket PF_CAN failed");
+ return 1;
+ }
+
+ //2.Specify can0 device
+ if(argc>1)
+ strcpy(ifr.ifr_name, argv[1]);
+ else
+ strcpy(ifr.ifr_name, "can0");
+ ret = ioctl(s, SIOCGIFINDEX, &ifr);
+ if (ret < 0) {
+ perror("ioctl failed");
+ return 1;
+ }
+
+ //3.Bind the socket to can0
+ addr.can_family = PF_CAN;
+ addr.can_ifindex = ifr.ifr_ifindex;
+ ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
+ if (ret < 0) {
+ perror("bind failed");
+ return 1;
+ }
+
+ //4.Define receive rules
+ // 24 (voltage) + 8 (temp)
+ struct can_filter rfilter[32];
+ rfilter[0].can_id = 0x630;
+ rfilter[0].can_mask = CAN_SFF_MASK;
+ rfilter[24].can_id = 0x680;
+ rfilter[24].can_mask = CAN_SFF_MASK;
+ for(int i=1;i<24;i++)
+ {
+ rfilter[i].can_id = rfilter[i-1].can_id+1;
+ rfilter[i].can_mask = CAN_SFF_MASK;
+ }
+ for(int i=25;i<32;i++)
+ {
+ rfilter[i].can_id = rfilter[i-1].can_id+3;
+ rfilter[i].can_mask = CAN_SFF_MASK;
+ }
+ setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
+
+ //5.Receive data and exit
+ for(;;)
+ {
+ nbytes = read(s, &frame, sizeof(frame));
+ if(nbytes > 0) {
+ double out;
+ FILE * fp;
+
+ // try FIFO, socket and other file types maybe better, not sure
+ if((fp=fopen("/tmp/mycan","w"))==NULL)
+ {
+ fprintf(stderr,"Can't open file \"%s\".\n","/tmp/mycan");
+ exit(1);
+ }
+
+ printf("can_id = 0x%X\r\ncan_dlc = %d \r\n", frame.can_id, frame.can_dlc);
+ for(int i = 0; i < 8; i++)
+ printf("data[%d] = %d\r\n", i, frame.data[i]);
+ can_message_u64 = u64_from_can_msg(frame.data);
+ unpack_message(&obj,frame.can_id,can_message_u64,frame.can_dlc,t);
+ printf("my prints:\n");
+ print_message(&obj, frame.can_id, stdout);
+ printf("my timestamp: %d\n", t);
+ //decode_can_0x630_BMS_Cell_4_Voltage(&obj,&out);
+ //printf("my can_0x630_BMS_Cell_4_Voltage: %g\n", out);
+ //decode_can_0x680_BMS_Section_4_Temp(&obj,&out);
+ //printf("my decode_can_0x680_BMS_Section_4_Temp: %g\n", out);
+ // Better if concat strings and print all at once
+ fprintf(fp,"%d",t);
+ decode_can_0x630_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x630_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x630_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x630_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x631_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x631_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x631_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x631_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x632_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x632_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x632_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x632_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x633_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x633_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x633_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x633_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x634_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x634_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x634_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x634_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x635_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x635_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x635_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x635_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x636_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x636_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x636_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x636_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x637_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x637_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x637_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x637_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x638_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x638_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x638_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x638_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x639_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x639_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x639_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x639_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63a_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63a_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63a_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63a_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63b_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63b_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63b_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63b_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63c_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63c_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63c_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63c_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63d_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63d_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63d_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63d_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63e_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63e_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63e_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63e_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63f_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63f_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63f_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x63f_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x640_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x640_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x640_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x640_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x641_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x641_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x641_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x641_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x642_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x642_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x642_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x642_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x643_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x643_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x643_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x643_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x644_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x644_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x644_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x644_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x645_BMS_Cell_4_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x645_BMS_Cell_3_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x645_BMS_Cell_2_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x645_BMS_Cell_1_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x646_BMS_Cell_8_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x646_BMS_Cell_7_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x646_BMS_Cell_6_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x646_BMS_Cell_5_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x647_BMS_Cell_12_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x647_BMS_Cell_11_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x647_BMS_Cell_10_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x647_BMS_Cell_9_Voltage(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x680_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x680_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x680_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x680_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x683_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x683_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x683_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x683_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x686_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x686_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x686_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x686_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x689_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x689_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x689_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x689_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68c_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68c_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68c_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68c_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68f_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68f_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68f_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x68f_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x692_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x692_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x692_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x692_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x695_BMS_Section_4_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x695_BMS_Section_3_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x695_BMS_Section_2_Temp(&obj,&out);
+ fprintf(fp,",%g",out);
+ decode_can_0x695_BMS_Section_1_Temp(&obj,&out);
+ fprintf(fp,",%g\n",out);
+
+ fclose(fp);
+ t++;
+ //break;
+ }
+ }
+
+ //6.Close the socket and can0
+ close(s);
+ //system("sudo ifconfig can0 down");
+
+ return 0;
+}