首页 新闻 会员 周边 捐助

Nodejs怎么读取二进制的.dat结尾的文件

0
悬赏园豆:50 [已解决问题] 解决于 2023-03-10 10:02
/*
 * main.c
 */
#include <stdio.h>
#include <string.h>
#define CY_VAP_TSC      "vap.dat"
typedef struct DetectorInfo_t
{
      unsigned char       DetectorId;
      unsigned char    Distance; //线圈离停止线距离
      unsigned char       Length; //线圈长度,宽度默认为车道宽度;
      unsigned char       Type; //取值:1: 排队检测器,2:搭接检测器, // 3:清空检测器,4:锁死检测器,// 5:溢出检测器;
      unsigned char        Headway; 
      unsigned char        Occupation; 
} DetectorInfo_t;

typedef struct ChannelId_t
{
 unsigned char        ChannelId;
 unsigned char        ChannelDir; //1:北向南直行,2:东向西直行…….
 unsigned char        ChannelType;//
 DetectorInfo_t      astDetectInfo[32];//保存检测器信息
} ChannelId_t;

typedef struct PhaseListVAP_t
{
 unsigned char   PhaseId;
 unsigned char   PhaseWalkGreen;
 unsigned char   PhaseWalkClear;
 unsigned char   PhaseMinGreen1;  
    unsigned char       PhaseMinGreen2; 
    unsigned char  PhaseMinGreen3; 
 unsigned char   PhaseGreenDelay;
 unsigned char   PhaseMaxGreen1;
 unsigned char   PhaseMaxGreen2;
    unsigned char  PhaseMaxGreen3; 
 unsigned char   PhaseFixGreen;
 unsigned char   PhaseGreenFlash;
 unsigned char   PhaseType;
 unsigned char   PhaseSpecFunc;
 unsigned char   PhaseReserved;
 ChannelId_t  astChannel[32];//保存当前相位的通道属性
} PhaseListVAP_t;

typedef struct PhaseVAP_t
{
 unsigned char       AllRedTime;
 unsigned char   FactPhaseNum;
 PhaseListVAP_t   PhaseListVAP[32];
} PhaseVAP_t;

int main(void) {

   PhaseVAP_t     stphase;
   memset(&stphase,0,sizeof(stphase));
  // stphase.AllRedTime = 5;
   stphase.FactPhaseNum = 2;
   stphase.PhaseListVAP[0].PhaseId = 1;
   stphase.PhaseListVAP[0].PhaseWalkGreen = 5;
   stphase.PhaseListVAP[0].PhaseWalkClear = 3;
   stphase.PhaseListVAP[0].PhaseMinGreen1 = 13;
    stphase.PhaseListVAP[0].PhaseMinGreen2 = 23;
    stphase.PhaseListVAP[0].PhaseMinGreen3 = 33;
    stphase.PhaseListVAP[0].PhaseGreenDelay = 3;
    stphase.PhaseListVAP[0].PhaseMaxGreen1 = 15;
    stphase.PhaseListVAP[0].PhaseMaxGreen2 = 25;
    stphase.PhaseListVAP[0].PhaseMaxGreen3 = 35;
    stphase.PhaseListVAP[0].PhaseFixGreen = 30;
    stphase.PhaseListVAP[0].PhaseGreenFlash = 3;
    stphase.PhaseListVAP[0].PhaseType = 32;
    stphase.PhaseListVAP[0].PhaseSpecFunc = 96;
    stphase.PhaseListVAP[0].PhaseReserved = 0;
    
    stphase.PhaseListVAP[0].astChannel[0].ChannelId = 1;
    stphase.PhaseListVAP[0].astChannel[0].ChannelDir = 1;
    stphase.PhaseListVAP[0].astChannel[0].ChannelType = 1;
    
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[0].DetectorId = 1;
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[0].Headway = 5;
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[0].Occupation = 25;
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[0].Type = 1;
       
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[1].DetectorId = 2;
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[1].Headway = 3;
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[1].Occupation = 20;
    stphase.PhaseListVAP[0].astChannel[0].astDetectInfo[1].Type = 3;
       
    
    stphase.PhaseListVAP[1].PhaseId = 2;
   stphase.PhaseListVAP[1].PhaseWalkGreen = 5;
   stphase.PhaseListVAP[1].PhaseWalkClear = 3;
   stphase.PhaseListVAP[1].PhaseMinGreen1 = 15;
    stphase.PhaseListVAP[1].PhaseMinGreen2 = 24;
    stphase.PhaseListVAP[1].PhaseMinGreen3 = 34;
    stphase.PhaseListVAP[1].PhaseGreenDelay = 3;
    stphase.PhaseListVAP[1].PhaseMaxGreen1 = 16;
    stphase.PhaseListVAP[1].PhaseMaxGreen2 = 26;
    stphase.PhaseListVAP[1].PhaseMaxGreen3 = 36;
    stphase.PhaseListVAP[1].PhaseFixGreen = 30;
    stphase.PhaseListVAP[1].PhaseGreenFlash = 3;
    stphase.PhaseListVAP[1].PhaseType = 32;
    stphase.PhaseListVAP[1].PhaseSpecFunc = 96;
    stphase.PhaseListVAP[1].PhaseReserved = 0;
    stphase.PhaseListVAP[1].astChannel[0].ChannelId = 3;
    stphase.PhaseListVAP[1].astChannel[0].ChannelDir = 1;
    stphase.PhaseListVAP[1].astChannel[0].ChannelType = 1;
    
    
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[0].DetectorId = 3;
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[0].Headway = 5;
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[0].Occupation = 25;
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[0].Type = 1;
        
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[1].DetectorId = 4;
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[1].Headway = 3;
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[1].Occupation = 20;
    stphase.PhaseListVAP[1].astChannel[0].astDetectInfo[1].Type = 3;
    
    
 FILE     *nfp = NULL;
 nfp = fopen(CY_VAP_TSC,"wb+");
 if (NULL == nfp)
 {
  printf("open vap file error,File: %s,Line: %d\n",__FILE__,__LINE__);
  return 0;
 }

 fwrite(&stphase,sizeof(stphase),1,nfp);  
 fclose(nfp);
 printf("The End sizeof:%d\n",sizeof(stphase));
   return 0;
}
古德拉克的主页 古德拉克 | 初学一级 | 园豆:147
提问于:2023-02-09 09:17
< >
分享
最佳答案
0

你得先知道这个 .dat 里面的数据是用什么方式(protobuf, boost,mfc,等等)将typedef 定义的struct 序列化的.

然后可以选择用c写一个反序列化的实现. 然后给nodejs去调用. 如果是语言无关的序列话 大概可以在nodejs里面找到对应的组件.

收获园豆:50
czd890 | 专家六级 |园豆:14488 | 2023-02-09 11:04

大佬,我补充了一下问题,加了.c的文件输出,我不是很懂这个问题,nodejs用的不多,麻烦多看两眼

古德拉克 | 园豆:147 (初学一级) | 2023-02-09 21:14

@古德拉克:
参考代码哈:

// nodejs
let datFileBytes:Buffer = get bytes from dat file;
let stphase={};
stphase.AllRedTime = datFileBytes.tostring(undefined,0,1)  //读取第一个字节.
stphase.FactPhaseNum=datFileBytes.tostring(undefined,1,1)  //读取第第二个节.

//FYI unsigned char 类型是一个byte. 剩下的字段你就挨个读取. 可能要注意大小端(字节对齐)的问题.

更好的办法是, 用c++写个dll然后在nodejs里面直接调用.

czd890 | 园豆:14488 (专家六级) | 2023-02-14 18:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册