aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXiao Pan <xyz@flylightning.xyz>2025-05-13 01:21:01 -0700
committerXiao Pan <xyz@flylightning.xyz>2025-05-13 01:21:01 -0700
commit54e59da6df406516aea9e52d9c6391829b1aa3cd (patch)
tree6c75c7a73d96bd763b341e21298d90f459b41dec
parente4c2319fef97c722132617e6736da7c8afa8622a (diff)
Add and edit comments
-rw-r--r--mycan.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/mycan.c b/mycan.c
index 0a6c106..56add73 100644
--- a/mycan.c
+++ b/mycan.c
@@ -45,6 +45,7 @@ cansend vcan0 680#00.FF.02.00.02.00.02.00
// 10+96*7+96*4+1=1067 bytes
#define ENTRY_SIZE 1067
+// Convert can message to uint64_t
// 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)
@@ -59,6 +60,7 @@ int main(int argc, char **argv)
struct ifreq ifr;
struct can_frame frame;
char *filename;
+ // Initialize filename to $XDG_DATA_HOME/mycan.csv. If no XDG_DATA_HOME environment variable, then initialize to $HOME/.local/share/mycan.csv
{
// usually ~/.local/share
const char *dir=getenv("XDG_DATA_HOME");
@@ -81,12 +83,11 @@ int main(int argc, char **argv)
memset(&frame, 0, sizeof(struct can_frame));
+ // No need any more because I use systemd unit to autostart configure CAN network interface
//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");
- // check and fix corrupted .csv file
+ // Check and fix corrupted .csv file due to power failure.
for(FILE *fp;(fp=fopen(filename,"r"))!=NULL;)
{
char ch;
@@ -120,6 +121,9 @@ int main(int argc, char **argv)
// https://unix.stackexchange.com/questions/785686#comment1505589_785686
fflush(stdout);
+ // https://www.waveshare.com/wiki/RS485_CAN_HAT
+ // Using Linux kernel's SocketCAN
+ // https://docs.kernel.org/networking/can.html
//1.Create socket
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {
@@ -148,7 +152,8 @@ int main(int argc, char **argv)
}
//4.Define receive rules
- // 24 (voltage) + 16 (temp)
+ // Adding masks to the socket, to only read CAN ids I want.
+ // 24 (voltage) + 16 (temp) can ids
struct can_filter rfilter[CAN_VOLT_LEN+CAN_TEMP_LEN];
rfilter[0].can_id = 0x630;
rfilter[0].can_mask = CAN_SFF_MASK;
@@ -166,7 +171,7 @@ int main(int argc, char **argv)
}
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
- //5.Receive data and exit
+ //5.Receive data
for(t_before=time(NULL);;)
{
nbytes = read(s, &frame, sizeof(frame));
@@ -175,7 +180,6 @@ int main(int argc, char **argv)
uint8_t iout;
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);
@@ -187,14 +191,17 @@ int main(int argc, char **argv)
// printf("data[%d] = %d\r\n", i, frame.data[i]);
can_message_u64 = u64_from_can_msg(frame.data);
t=time(NULL);
+ // Unpack CAN message and save to obj. This function is automatically generated using dbcc.
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);
+
+ // Decode CAN message and write to file, using decode functions generated automatically using dbcc.
if(t>t_before)
{
t_before=t;
- // Maybe better if concat strings and print all at once
+ // Maybe better if concat strings and print all at once, but it would be more complicated, I tried, see git log
fprintf(fp,"%u",t);
decode_can_0x630_BMS_M1_Cell_1_Voltage(&obj,&out);
fprintf(fp,",%6.4f",out);