// 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 #include #include #include #include #include #include // (GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause #include #include // time() #include #include "all_SRE_edited.h" #define CAN_VOLT_LEN 24 #define CAN_TEMP_LEN 16 #define VOLTLEN 96 #define TEMPLEN 96 // not consider \0 #define STR_T_LEN 10 #define STR_VOLT_LEN 7 #define STR_TEMP_LEN 4 // max wire unsigned number is 65535 // max wire signed number maybe is -32768 to 32767 // voltage max format 7 bytes ,6.5535 // temp max format 4 bytes ,255 // timestamp max 10 bytes 1745742182 // 1 timestamp, 96 voltage, 48 temp, 1 \n // 10+96*7+96*4+1=1067 bytes #define ENTRY_SIZE (STR_T_LEN+VOLTLEN*STR_VOLT_LEN+TEMPLEN*STR_TEMP_LEN+1) // 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; char *filename; { // usually ~/.local/share const char *dir=getenv("XDG_DATA_HOME"); const char *str; if(dir != NULL) str="/mycan.csv"; else { str="/.local/share/mycan.csv"; dir=getenv("HOME"); } filename=malloc((strlen(dir)+strlen(str)+1)*sizeof(char)); sprintf(filename,"%s%s",dir,str); } //printf("entry_size: %d\n",ENTRY_SIZE); can_obj_all_sre_edited_h_t obj; dbcc_time_stamp_t t, t_before; 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) + 16 (temp) struct can_filter rfilter[CAN_VOLT_LEN+CAN_TEMP_LEN]; rfilter[0].can_id = 0x630; rfilter[0].can_mask = CAN_SFF_MASK; rfilter[CAN_VOLT_LEN].can_id = 0x680; rfilter[CAN_VOLT_LEN].can_mask = CAN_SFF_MASK; for(int i=1;i 0) { FILE * fp; // try FIFO, socket and other file types maybe better, not sure if((fp=fopen(filename,"a"))==NULL) { fprintf(stderr,"Can't open file \"%s\".\n",filename); 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); t=time(NULL); 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: %u\n", t); if(t>t_before) { t_before=t; char entry[ENTRY_SIZE+1]; //char t_str[STR_T_LEN+1]; double out[TEMPLEN]; uint8_t iout[VOLTLEN]; decode_can_0x630_BMS_M1_Cell_1_Voltage (&obj,&out[0]); decode_can_0x630_BMS_M1_Cell_2_Voltage (&obj,&out[1]); decode_can_0x630_BMS_M1_Cell_3_Voltage (&obj,&out[2]); decode_can_0x630_BMS_M1_Cell_4_Voltage (&obj,&out[3]); decode_can_0x631_BMS_M1_Cell_5_Voltage (&obj,&out[4]); decode_can_0x631_BMS_M1_Cell_6_Voltage (&obj,&out[5]); decode_can_0x631_BMS_M1_Cell_7_Voltage (&obj,&out[6]); decode_can_0x631_BMS_M1_Cell_8_Voltage (&obj,&out[7]); decode_can_0x632_BMS_M1_Cell_9_Voltage (&obj,&out[8]); decode_can_0x632_BMS_M1_Cell_10_Voltage(&obj,&out[9]); decode_can_0x632_BMS_M1_Cell_11_Voltage(&obj,&out[10]); decode_can_0x632_BMS_M1_Cell_12_Voltage(&obj,&out[11]); decode_can_0x633_BMS_M2_Cell_1_Voltage (&obj,&out[12]); decode_can_0x633_BMS_M2_Cell_2_Voltage (&obj,&out[13]); decode_can_0x633_BMS_M2_Cell_3_Voltage (&obj,&out[14]); decode_can_0x633_BMS_M2_Cell_4_Voltage (&obj,&out[15]); decode_can_0x634_BMS_M2_Cell_5_Voltage (&obj,&out[16]); decode_can_0x634_BMS_M2_Cell_6_Voltage (&obj,&out[17]); decode_can_0x634_BMS_M2_Cell_7_Voltage (&obj,&out[18]); decode_can_0x634_BMS_M2_Cell_8_Voltage (&obj,&out[19]); decode_can_0x635_BMS_M2_Cell_9_Voltage (&obj,&out[20]); decode_can_0x635_BMS_M2_Cell_10_Voltage(&obj,&out[21]); decode_can_0x635_BMS_M2_Cell_11_Voltage(&obj,&out[22]); decode_can_0x635_BMS_M2_Cell_12_Voltage(&obj,&out[23]); decode_can_0x636_BMS_M3_Cell_1_Voltage (&obj,&out[24]); decode_can_0x636_BMS_M3_Cell_2_Voltage (&obj,&out[25]); decode_can_0x636_BMS_M3_Cell_3_Voltage (&obj,&out[26]); decode_can_0x636_BMS_M3_Cell_4_Voltage (&obj,&out[27]); decode_can_0x637_BMS_M3_Cell_5_Voltage (&obj,&out[28]); decode_can_0x637_BMS_M3_Cell_6_Voltage (&obj,&out[29]); decode_can_0x637_BMS_M3_Cell_7_Voltage (&obj,&out[30]); decode_can_0x637_BMS_M3_Cell_8_Voltage (&obj,&out[31]); decode_can_0x638_BMS_M3_Cell_9_Voltage (&obj,&out[32]); decode_can_0x638_BMS_M3_Cell_10_Voltage(&obj,&out[33]); decode_can_0x638_BMS_M3_Cell_11_Voltage(&obj,&out[34]); decode_can_0x638_BMS_M3_Cell_12_Voltage(&obj,&out[35]); decode_can_0x639_BMS_M4_Cell_1_Voltage (&obj,&out[36]); decode_can_0x639_BMS_M4_Cell_2_Voltage (&obj,&out[37]); decode_can_0x639_BMS_M4_Cell_3_Voltage (&obj,&out[38]); decode_can_0x639_BMS_M4_Cell_4_Voltage (&obj,&out[39]); decode_can_0x63a_BMS_M4_Cell_5_Voltage (&obj,&out[40]); decode_can_0x63a_BMS_M4_Cell_6_Voltage (&obj,&out[41]); decode_can_0x63a_BMS_M4_Cell_7_Voltage (&obj,&out[42]); decode_can_0x63a_BMS_M4_Cell_8_Voltage (&obj,&out[43]); decode_can_0x63b_BMS_M4_Cell_9_Voltage (&obj,&out[44]); decode_can_0x63b_BMS_M4_Cell_10_Voltage(&obj,&out[45]); decode_can_0x63b_BMS_M4_Cell_11_Voltage(&obj,&out[46]); decode_can_0x63b_BMS_M4_Cell_12_Voltage(&obj,&out[47]); decode_can_0x63c_BMS_M5_Cell_1_Voltage (&obj,&out[48]); decode_can_0x63c_BMS_M5_Cell_2_Voltage (&obj,&out[49]); decode_can_0x63c_BMS_M5_Cell_3_Voltage (&obj,&out[50]); decode_can_0x63c_BMS_M5_Cell_4_Voltage (&obj,&out[51]); decode_can_0x63d_BMS_M5_Cell_5_Voltage (&obj,&out[52]); decode_can_0x63d_BMS_M5_Cell_6_Voltage (&obj,&out[53]); decode_can_0x63d_BMS_M5_Cell_7_Voltage (&obj,&out[54]); decode_can_0x63d_BMS_M5_Cell_8_Voltage (&obj,&out[55]); decode_can_0x63e_BMS_M5_Cell_9_Voltage (&obj,&out[56]); decode_can_0x63e_BMS_M5_Cell_10_Voltage(&obj,&out[57]); decode_can_0x63e_BMS_M5_Cell_11_Voltage(&obj,&out[58]); decode_can_0x63e_BMS_M5_Cell_12_Voltage(&obj,&out[59]); decode_can_0x63f_BMS_M6_Cell_1_Voltage (&obj,&out[60]); decode_can_0x63f_BMS_M6_Cell_2_Voltage (&obj,&out[61]); decode_can_0x63f_BMS_M6_Cell_3_Voltage (&obj,&out[62]); decode_can_0x63f_BMS_M6_Cell_4_Voltage (&obj,&out[63]); decode_can_0x640_BMS_M6_Cell_5_Voltage (&obj,&out[64]); decode_can_0x640_BMS_M6_Cell_6_Voltage (&obj,&out[65]); decode_can_0x640_BMS_M6_Cell_7_Voltage (&obj,&out[66]); decode_can_0x640_BMS_M6_Cell_8_Voltage (&obj,&out[67]); decode_can_0x641_BMS_M6_Cell_9_Voltage (&obj,&out[68]); decode_can_0x641_BMS_M6_Cell_10_Voltage(&obj,&out[69]); decode_can_0x641_BMS_M6_Cell_11_Voltage(&obj,&out[70]); decode_can_0x641_BMS_M6_Cell_12_Voltage(&obj,&out[71]); decode_can_0x642_BMS_M7_Cell_1_Voltage (&obj,&out[72]); decode_can_0x642_BMS_M7_Cell_2_Voltage (&obj,&out[73]); decode_can_0x642_BMS_M7_Cell_3_Voltage (&obj,&out[74]); decode_can_0x642_BMS_M7_Cell_4_Voltage (&obj,&out[75]); decode_can_0x643_BMS_M7_Cell_5_Voltage (&obj,&out[76]); decode_can_0x643_BMS_M7_Cell_6_Voltage (&obj,&out[77]); decode_can_0x643_BMS_M7_Cell_7_Voltage (&obj,&out[78]); decode_can_0x643_BMS_M7_Cell_8_Voltage (&obj,&out[79]); decode_can_0x644_BMS_M7_Cell_9_Voltage (&obj,&out[80]); decode_can_0x644_BMS_M7_Cell_10_Voltage(&obj,&out[81]); decode_can_0x644_BMS_M7_Cell_11_Voltage(&obj,&out[82]); decode_can_0x644_BMS_M7_Cell_12_Voltage(&obj,&out[83]); decode_can_0x645_BMS_M8_Cell_1_Voltage (&obj,&out[84]); decode_can_0x645_BMS_M8_Cell_2_Voltage (&obj,&out[85]); decode_can_0x645_BMS_M8_Cell_3_Voltage (&obj,&out[86]); decode_can_0x645_BMS_M8_Cell_4_Voltage (&obj,&out[87]); decode_can_0x646_BMS_M8_Cell_5_Voltage (&obj,&out[88]); decode_can_0x646_BMS_M8_Cell_6_Voltage (&obj,&out[89]); decode_can_0x646_BMS_M8_Cell_7_Voltage (&obj,&out[90]); decode_can_0x646_BMS_M8_Cell_8_Voltage (&obj,&out[91]); decode_can_0x647_BMS_M8_Cell_9_Voltage (&obj,&out[92]); decode_can_0x647_BMS_M8_Cell_10_Voltage(&obj,&out[93]); decode_can_0x647_BMS_M8_Cell_11_Voltage(&obj,&out[94]); decode_can_0x647_BMS_M8_Cell_12_Voltage(&obj,&out[95]); decode_can_0x680_BMS_M1_Cell_1_Thermistor (&obj,&iout[0]); decode_can_0x680_BMS_M1_Cell_2_Thermistor (&obj,&iout[1]); decode_can_0x680_BMS_M1_Cell_3_Thermistor (&obj,&iout[2]); decode_can_0x680_BMS_M1_Cell_4_Thermistor (&obj,&iout[3]); decode_can_0x680_BMS_M1_Cell_5_Thermistor (&obj,&iout[4]); decode_can_0x680_BMS_M1_Cell_6_Thermistor (&obj,&iout[5]); decode_can_0x680_BMS_M1_Cell_7_Thermistor (&obj,&iout[6]); decode_can_0x680_BMS_M1_Cell_8_Thermistor (&obj,&iout[7]); decode_can_0x681_BMS_M1_Cell_9_Thermistor (&obj,&iout[8]); decode_can_0x681_BMS_M1_Cell_10_Thermistor(&obj,&iout[9]); decode_can_0x681_BMS_M1_Cell_11_Thermistor(&obj,&iout[10]); decode_can_0x681_BMS_M1_Cell_12_Thermistor(&obj,&iout[11]); decode_can_0x682_BMS_M2_Cell_1_Thermistor (&obj,&iout[12]); decode_can_0x682_BMS_M2_Cell_2_Thermistor (&obj,&iout[13]); decode_can_0x682_BMS_M2_Cell_3_Thermistor (&obj,&iout[14]); decode_can_0x682_BMS_M2_Cell_4_Thermistor (&obj,&iout[15]); decode_can_0x682_BMS_M2_Cell_5_Thermistor (&obj,&iout[16]); decode_can_0x682_BMS_M2_Cell_6_Thermistor (&obj,&iout[17]); decode_can_0x682_BMS_M2_Cell_7_Thermistor (&obj,&iout[18]); decode_can_0x682_BMS_M2_Cell_8_Thermistor (&obj,&iout[19]); decode_can_0x683_BMS_M2_Cell_9_Thermistor (&obj,&iout[20]); decode_can_0x683_BMS_M2_Cell_10_Thermistor(&obj,&iout[21]); decode_can_0x683_BMS_M2_Cell_11_Thermistor(&obj,&iout[22]); decode_can_0x683_BMS_M2_Cell_12_Thermistor(&obj,&iout[23]); decode_can_0x684_BMS_M3_Cell_1_Thermistor (&obj,&iout[24]); decode_can_0x684_BMS_M3_Cell_2_Thermistor (&obj,&iout[25]); decode_can_0x684_BMS_M3_Cell_3_Thermistor (&obj,&iout[26]); decode_can_0x684_BMS_M3_Cell_4_Thermistor (&obj,&iout[27]); decode_can_0x684_BMS_M3_Cell_5_Thermistor (&obj,&iout[28]); decode_can_0x684_BMS_M3_Cell_6_Thermistor (&obj,&iout[29]); decode_can_0x684_BMS_M3_Cell_7_Thermistor (&obj,&iout[30]); decode_can_0x684_BMS_M3_Cell_8_Thermistor (&obj,&iout[31]); decode_can_0x685_BMS_M3_Cell_9_Thermistor (&obj,&iout[32]); decode_can_0x685_BMS_M3_Cell_10_Thermistor(&obj,&iout[33]); decode_can_0x685_BMS_M3_Cell_11_Thermistor(&obj,&iout[34]); decode_can_0x685_BMS_M3_Cell_12_Thermistor(&obj,&iout[35]); decode_can_0x686_BMS_M4_Cell_1_Thermistor (&obj,&iout[36]); decode_can_0x686_BMS_M4_Cell_2_Thermistor (&obj,&iout[37]); decode_can_0x686_BMS_M4_Cell_3_Thermistor (&obj,&iout[38]); decode_can_0x686_BMS_M4_Cell_4_Thermistor (&obj,&iout[39]); decode_can_0x686_BMS_M4_Cell_5_Thermistor (&obj,&iout[40]); decode_can_0x686_BMS_M4_Cell_6_Thermistor (&obj,&iout[41]); decode_can_0x686_BMS_M4_Cell_7_Thermistor (&obj,&iout[42]); decode_can_0x686_BMS_M4_Cell_8_Thermistor (&obj,&iout[43]); decode_can_0x687_BMS_M4_Cell_9_Thermistor (&obj,&iout[44]); decode_can_0x687_BMS_M4_Cell_10_Thermistor(&obj,&iout[45]); decode_can_0x687_BMS_M4_Cell_11_Thermistor(&obj,&iout[46]); decode_can_0x687_BMS_M4_Cell_12_Thermistor(&obj,&iout[47]); decode_can_0x688_BMS_M5_Cell_1_Thermistor (&obj,&iout[48]); decode_can_0x688_BMS_M5_Cell_2_Thermistor (&obj,&iout[49]); decode_can_0x688_BMS_M5_Cell_3_Thermistor (&obj,&iout[50]); decode_can_0x688_BMS_M5_Cell_4_Thermistor (&obj,&iout[51]); decode_can_0x688_BMS_M5_Cell_5_Thermistor (&obj,&iout[52]); decode_can_0x688_BMS_M5_Cell_6_Thermistor (&obj,&iout[53]); decode_can_0x688_BMS_M5_Cell_7_Thermistor (&obj,&iout[54]); decode_can_0x688_BMS_M5_Cell_8_Thermistor (&obj,&iout[55]); decode_can_0x689_BMS_M5_Cell_9_Thermistor (&obj,&iout[56]); decode_can_0x689_BMS_M5_Cell_10_Thermistor(&obj,&iout[57]); decode_can_0x689_BMS_M5_Cell_11_Thermistor(&obj,&iout[58]); decode_can_0x689_BMS_M5_Cell_12_Thermistor(&obj,&iout[59]); decode_can_0x68a_BMS_M6_Cell_1_Thermistor (&obj,&iout[60]); decode_can_0x68a_BMS_M6_Cell_2_Thermistor (&obj,&iout[61]); decode_can_0x68a_BMS_M6_Cell_3_Thermistor (&obj,&iout[62]); decode_can_0x68a_BMS_M6_Cell_4_Thermistor (&obj,&iout[63]); decode_can_0x68a_BMS_M6_Cell_5_Thermistor (&obj,&iout[64]); decode_can_0x68a_BMS_M6_Cell_6_Thermistor (&obj,&iout[65]); decode_can_0x68a_BMS_M6_Cell_7_Thermistor (&obj,&iout[66]); decode_can_0x68a_BMS_M6_Cell_8_Thermistor (&obj,&iout[67]); decode_can_0x68b_BMS_M6_Cell_9_Thermistor (&obj,&iout[68]); decode_can_0x68b_BMS_M6_Cell_10_Thermistor(&obj,&iout[69]); decode_can_0x68b_BMS_M6_Cell_11_Thermistor(&obj,&iout[70]); decode_can_0x68b_BMS_M6_Cell_12_Thermistor(&obj,&iout[71]); decode_can_0x68c_BMS_M7_Cell_1_Thermistor (&obj,&iout[72]); decode_can_0x68c_BMS_M7_Cell_2_Thermistor (&obj,&iout[73]); decode_can_0x68c_BMS_M7_Cell_3_Thermistor (&obj,&iout[74]); decode_can_0x68c_BMS_M7_Cell_4_Thermistor (&obj,&iout[75]); decode_can_0x68c_BMS_M7_Cell_5_Thermistor (&obj,&iout[76]); decode_can_0x68c_BMS_M7_Cell_6_Thermistor (&obj,&iout[77]); decode_can_0x68c_BMS_M7_Cell_7_Thermistor (&obj,&iout[78]); decode_can_0x68c_BMS_M7_Cell_8_Thermistor (&obj,&iout[79]); decode_can_0x68d_BMS_M7_Cell_9_Thermistor (&obj,&iout[80]); decode_can_0x68d_BMS_M7_Cell_10_Thermistor(&obj,&iout[81]); decode_can_0x68d_BMS_M7_Cell_11_Thermistor(&obj,&iout[82]); decode_can_0x68d_BMS_M7_Cell_12_Thermistor(&obj,&iout[83]); decode_can_0x68e_BMS_M8_Cell_1_Thermistor (&obj,&iout[84]); decode_can_0x68e_BMS_M8_Cell_2_Thermistor (&obj,&iout[85]); decode_can_0x68e_BMS_M8_Cell_3_Thermistor (&obj,&iout[86]); decode_can_0x68e_BMS_M8_Cell_4_Thermistor (&obj,&iout[87]); decode_can_0x68e_BMS_M8_Cell_5_Thermistor (&obj,&iout[88]); decode_can_0x68e_BMS_M8_Cell_6_Thermistor (&obj,&iout[89]); decode_can_0x68e_BMS_M8_Cell_7_Thermistor (&obj,&iout[90]); decode_can_0x68e_BMS_M8_Cell_8_Thermistor (&obj,&iout[91]); decode_can_0x68f_BMS_M8_Cell_9_Thermistor (&obj,&iout[92]); decode_can_0x68f_BMS_M8_Cell_10_Thermistor(&obj,&iout[93]); decode_can_0x68f_BMS_M8_Cell_11_Thermistor(&obj,&iout[94]); decode_can_0x68f_BMS_M8_Cell_12_Thermistor(&obj,&iout[95]); sprintf(entry,"%u",t); for(int i=0;i