aboutsummaryrefslogtreecommitdiff
path: root/mycan.c
diff options
context:
space:
mode:
authorXiao Pan <xyz@flylightning.xyz>2025-05-07 18:31:18 -0700
committerXiao Pan <xyz@flylightning.xyz>2025-05-07 18:31:18 -0700
commitaabf4bac82657e48883358efe9b0ef35c2e4afd0 (patch)
tree150693383f97e09c1034e3ee11be4d9dbecf81a0 /mycan.c
parent8a6d1280fd3aca54c5be9f426299946da44d827d (diff)
Fix corrupted csv file by removing last bad line
Diffstat (limited to 'mycan.c')
-rw-r--r--mycan.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/mycan.c b/mycan.c
index 5229c7f..7320b87 100644
--- a/mycan.c
+++ b/mycan.c
@@ -36,6 +36,14 @@ cansend vcan0 680#00.FF.02.00.02.00.02.00
#define CAN_VOLT_LEN 24
#define CAN_TEMP_LEN 16
+// 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 1067
// https://github.com/howerj/dbcc MIT
static uint64_t u64_from_can_msg(const uint8_t m[8]) {
@@ -78,6 +86,36 @@ int main(int argc, char **argv)
// vcan, for test
//printf("this is a can receive demo\r\n");
+ // check and fix corrupted .csv file
+ {
+ FILE * fp;
+ char ch;
+ uint32_t offset;
+ int remain;
+ if((fp=fopen(filename,"r"))==NULL)
+ {
+ fprintf(stderr,"Can't open file \"%s\".\n",filename);
+ exit(1);
+ }
+ fseek(fp,-1L,SEEK_END);
+ fscanf(fp,"%c",&ch);
+ fseek(fp,0L,SEEK_END);
+ offset=ftell(fp);
+ printf("csv file end offset: %d\n",offset);
+ fclose(fp);
+ remain=offset%ENTRY_SIZE;
+ if(remain)
+ {
+ printf(".csv file not a multiple of %d, remove last line\n",ENTRY_SIZE);
+ truncate(filename,offset-remain);
+ }
+ else if(ch != '\n')
+ {
+ printf(".csv file last char not newline, remove last line\n");
+ truncate(filename,offset-ENTRY_SIZE);
+ }
+ }
+
//1.Create socket
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {