diff options
-rw-r--r-- | mycan.c | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -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) { |