📄 alert_main.cpp~
字号:
i=0; while(1) { ret=uartread(&rbuf[1+i],rbuf[0]); if (ret<0) { return -1; } i=i+ret; if(i==rbuf[0]) { break; } } sum=0; for (i=0;i<rbuf[0]+1;i++) { sum=sum+rbuf[i]; } ch=sum; sum=ch; if (sum!=0) { printf("check sum error\n"); return -1; } return 0;}int communication_with_module(uchar *tbuf,uchar *rbuf){ int result; result=get_uart(); if(result!=0) { printf("get uart error\n"); return -1; } result=handshake(); if(result!=0) { printf("handshake error\n"); free_uart(); return -1; } printf("handshake ok\n"); result=uartsend(tbuf,tbuf[0]+1); if (result!=0) { free_uart(); return -1; } result=uartreadframe(rbuf); if (result!=0) { free_uart(); return -1; } free_uart(); return 0;};/******************************* type://类型*********************************/int query_zone_parameter(int type){ int ret; uchar order; uchar tbuf[20],rbuf[20]; alert_device_t *temp_device; tbuf[0]=0x06; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x02; //data length tbuf[4]=0xcd; //data1----command :get zone param switch(type) { case WIRELESS: tbuf[5]=0; //0: wireless zone type break; case WIRE: tbuf[5]=4; //4: wire zone type break; default: return -3; }; tbuf[6]=checksum(tbuf,6); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } for (int i=0;i<rbuf[0];i++) printf("%x",rbuf[i]); printf("\n"); if ((rbuf[4]!=0xcd)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } PROFILES.zone_stat_buf[0]=rbuf[6];//防区状态 PROFILES.zone_stat_buf[1]=rbuf[7];//常设报警 PROFILES.zone_stat_buf[2]=rbuf[8];//外出报警 PROFILES.zone_stat_buf[3]=rbuf[9];//警号 for (temp_device=PROFILES.dev_head;temp_device;temp_device=temp_device->next) { if (temp_device->device_type==type) { order=temp_device->device_cno+1; temp_device->device_enable=bit_value(order,rbuf[6]); if (bit_value(order,rbuf[7]))//常设报警 temp_device->device_module=ALERT_ALWAYS; else if (bit_value(order,rbuf[8]))//外出报警 temp_device->device_module=ALERT_INNER; else temp_device->device_module=ALERT_OUT; temp_device->device_self_alert=bit_value(order,rbuf[9]); } } printf("get zone param ok\n"); return 0;}/****************************************** 保存时,建一个临时设备, 测试设置是否成功!*********************************************/int set_zone_parameter(alert_device_t *op_dev){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x0a; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x06; //data length tbuf[4]=0xce; //data1----command :set zone param if (op_dev->device_type==WIRE) tbuf[5]=4; //data2----zone type:0--WD 4--YX else tbuf[5]=0; tbuf[6]=PROFILES.zone_stat_buf[0]; //data3----zone state tbuf[7]=PROFILES.zone_stat_buf[1]; //data4----zone zst tbuf[8]=PROFILES.zone_stat_buf[2]; //data5----zone inter tbuf[9]=PROFILES.zone_stat_buf[3]; //data6----horn state set_bit_value(op_dev->device_cno+1,&tbuf[6],op_dev->device_enable); //初始化为ALERT_OUT set_bit_value(op_dev->device_cno+1,&tbuf[7],0); set_bit_value(op_dev->device_cno+1,&tbuf[8],0); if (op_dev->device_module==ALERT_ALWAYS) set_bit_value(op_dev->device_cno+1,&tbuf[7],1); else if (op_dev->device_module== ALERT_INNER) set_bit_value(op_dev->device_cno+1,&tbuf[8],1); set_bit_value(op_dev->device_cno+1,&tbuf[9],op_dev->device_self_alert); tbuf[10]=checksum(tbuf,0x0a); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } for (int i=0;i<rbuf[0];i++) printf("%x",rbuf[i]); printf("\n"); if ((rbuf[4]!=0xce)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } //show_action_result_window(0); return 0; }/******************************************************************/int query_defence_stat(){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x05; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x01; //data length tbuf[4]=0xe6; //data1----command :get defence stat param tbuf[5]=checksum(tbuf,5); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } for (int i=0;i<rbuf[0];i++) printf("%x",rbuf[i]); printf("\n"); if ((rbuf[4]!=0xe6)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } if (rbuf[6]==0) PROFILES.alert_defence_stat=DEFENCE_OFF; else if (rbuf[6]==1) PROFILES.alert_defence_stat=DEFENCE_INNER; else//2 PROFILES.alert_defence_stat=DEFENCE_OUT; printf("get defence stat param ok\n"); return 0;}int query_device_stat(){ int result_zone_wireless=query_zone_parameter(WIRELESS); int result_zone_wire=query_zone_parameter(WIRE); int result_defence_stat=query_defence_stat(); int result=!result_zone_wireless&&!result_zone_wire&&!result_defence_stat; return !result;}int arm_dearm_operation(int flag){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x06; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x02; //data length tbuf[4]=0xe5; //data1----command :arm/dearm command if (flag==DEFENCE_OFF) tbuf[5]=0; else if (flag==DEFENCE_INNER) tbuf[5]=1; else tbuf[5]=2; tbuf[6]=checksum(tbuf,6); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xe5)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } return 0;}int query_delay_parameter(int *enter_delay,int *exit_delay){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x05; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x01; //data length tbuf[4]=0xe0; //data1----command :get system delay parameter command tbuf[5]=checksum(tbuf,5); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xe0)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } *enter_delay=rbuf[6]; *exit_delay=rbuf[7]; return 0;}int set_delay_parameter(int enter_delay,int exit_delay){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x07; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x03; //data length tbuf[4]=0xe1; //data1----command :set system delay time parameter command tbuf[5]=enter_delay; tbuf[6]=exit_delay; tbuf[7]=checksum(tbuf,7); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xe1)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } return 0;}int query_remote_parameter(WordList *wd){ int ret; char buf[64]; uchar tbuf[20],rbuf[20]; tbuf[0]=0x05; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x01; //data length tbuf[4]=0xea; //data1----command :get system delay parameter command tbuf[5]=checksum(tbuf,5); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xea)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } wd->Clear(); for (int i=1;i<=8;i++) { if (bit_value(i,rbuf[6])) { sprintf(buf,"遥控器%d",i); wd->Add(buf); } } return 0;}int query_id_parameter(WordList *wd){ int ret; char buf[64]; uchar tbuf[20],rbuf[20]; tbuf[0]=0x05; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x01; //data length tbuf[4]=0xe9; //data1----command :get system delay parameter command tbuf[5]=checksum(tbuf,5); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xe9)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } wd->Clear(); for (int i=0;i<6;i++) { for (int j=1;j<=8;j++) { if (bit_value(j,rbuf[6+i])) { sprintf(buf,"ID卡%02d",i*8+j-1); wd->Add(buf); } } } return 0;}int delete_all_IDcard(){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x06; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x02; //data length tbuf[4]=0xc8; //data1----command :delete card tbuf[5]=0x0; //data2----0--all tbuf[6]=checksum(tbuf,6); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xc8)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } return 0;}int delete_specific_IDcard(int id){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x07; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x03; //data length tbuf[4]=0xc8; //data1----command :delete card tbuf[5]=0x1; //data2----1--specific tbuf[6]=id; tbuf[7]=checksum(tbuf,7); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xc8)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } return 0;}int delete_all_RC(){ int ret; uchar tbuf[20],rbuf[20]; tbuf[0]=0x06; //length tbuf[1]=0x01; //dest tbuf[2]=0x02; //flag tbuf[3]=0x02; //data length tbuf[4]=0xcb; //data1----command :delete card tbuf[5]=0x0; //data2----0--all tbuf[6]=checksum(tbuf,6); //cksum ret=communication_with_module(tbuf,rbuf); if (ret!=0) { printf("communication error\n"); return -1; } if ((rbuf[4]!=0xcb)||(rbuf[5]!=0xd6)) { printf("refused\n"); return -2; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -