1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> #include <arpa/inet.h> #include <unistd.h> #include <errno.h> #include <netdb.h> #include <pthread.h> #define PORT 5000 #define SERVER_IP "192.168.6.5" #define IVVER_LEN 17 #define PROVER_LEN 85
typedef struct{ float Ia; float Ib; float Ic; uint16_t Va; uint16_t Vb; uint16_t Vc; } analysis_s_part;
typedef struct{ float Ia; float Ib; float Ic; uint16_t Va; uint16_t Vb; uint16_t Vc; float Ca; float Cb; float Cc; } analysis_l_part;
struct sense_part{ uint16_t type; uint16_t number; };
struct content_part { uint32_t ticks; struct sense_part sense; float value; };
struct udp_request { uint8_t forward; uint8_t reserve; uint16_t version; uint16_t length; uint16_t verify; uint16_t command; struct content_part content; };
uint16_t crc16_modbus(uint8_t *data, uint16_t length) { uint8_t i; uint16_t crc = 0xffff; while(length--) { crc ^= *data++; for (i = 0; i < 8; ++i) { if (crc & 1) crc = (crc >> 1) ^ 0xA001; else crc = (crc >> 1); } } return crc; }
analysis_s_part analysis_s(unsigned char *rec_data){ analysis_s_part ana_s; uint8_t DataLength_Short = rec_data[2]; float ElectricityA1 =(rec_data[3] << 8 | rec_data[4]); float ElectricityA = ElectricityA1/10; float ElectricityB1 =(rec_data[5] << 8 | rec_data[6]); float ElectricityB = ElectricityB1/10; float ElectricityC1 =(rec_data[7] << 8 | rec_data[8]); float ElectricityC = ElectricityC1/10; uint16_t VoltageA = rec_data[9] << 8 | rec_data[10]; uint16_t VoltageB = rec_data[11] << 8 | rec_data[12]; uint16_t VoltageC = rec_data[13] << 8 | rec_data[14]; ana_s.Ia = ElectricityA; ana_s.Ib = ElectricityB; ana_s.Ic = ElectricityC; ana_s.Va = VoltageA; ana_s.Vb = VoltageB; ana_s.Vc = VoltageC; printf("DianliuDianyaBan:\n"); printf("Length: %d\n", DataLength_Short); printf("ElectricityA: %.1f\n", ElectricityA); printf("ElectricityB: %.1f\n", ElectricityB); printf("ElectricityC: %.1f\n", ElectricityC); printf("VoltageA: %d\n", VoltageA); printf("VoltageB: %d\n", VoltageB); printf("VoltageC: %d\n", VoltageC); printf("\n"); return ana_s; }
analysis_l_part analysis_l(unsigned char *rec_data){ analysis_l_part ana_l; uint8_t DataLength_Long = rec_data[2]; float ProElectricityA1 =(rec_data[3] << 8 | rec_data[4]); float ProElectricityA = ProElectricityA1/10; float ProElectricityB1 =(rec_data[5] << 8 | rec_data[6]); float ProElectricityB = ProElectricityB1/10; float ProElectricityC1 =(rec_data[7] << 8 | rec_data[8]); float ProElectricityC = ProElectricityC1/10; uint16_t ProVoltageA = rec_data[9] << 8 | rec_data[10]; uint16_t ProVoltageB = rec_data[11] << 8 | rec_data[12]; uint16_t ProVoltageC = rec_data[13] << 8 | rec_data[14]; uint16_t ActivePowerA_h = rec_data[15] << 8 | rec_data[16]; int ActivePowerA; if(ActivePowerA_h >> 12 >= 0x8){ ActivePowerA_h = ~ActivePowerA_h + 1; ActivePowerA = -ActivePowerA_h; }else{ ActivePowerA = ActivePowerA_h; } uint16_t ActivePowerB_h = rec_data[17] << 8 | rec_data[18]; int ActivePowerB; if(ActivePowerB_h >> 12 >= 0x8){ ActivePowerB_h = ~ActivePowerB_h + 1; ActivePowerB = -ActivePowerB_h; }else{ ActivePowerB = ActivePowerB_h; } uint16_t ActivePowerC_h = rec_data[19] << 8 | rec_data[20]; int ActivePowerC; if(ActivePowerC_h >> 12 >= 0x8){ ActivePowerC_h = ~ActivePowerC_h + 1; ActivePowerC = -ActivePowerC_h; }else{ ActivePowerC = ActivePowerC_h; } uint32_t TotalActivePower_h = rec_data[21] <<24 | rec_data[22] <<16 | rec_data[23] << 8 | rec_data[24]; int TotalActivePower; if(TotalActivePower_h >> 28 >= 0x8){ TotalActivePower_h = ~TotalActivePower_h + 1; TotalActivePower = -TotalActivePower_h; }else{ TotalActivePower = TotalActivePower_h; } uint16_t ReactivePowerA_h = rec_data[25] << 8 | rec_data[26]; int ReactivePowerA; if(ReactivePowerA_h >> 12 >= 0x8){ ReactivePowerA_h = ~ReactivePowerA_h + 1; ReactivePowerA = -ReactivePowerA_h; }else{ ReactivePowerA = ReactivePowerA_h; } uint16_t ReactivePowerB_h = rec_data[27] << 8 | rec_data[28]; int ReactivePowerB; if(ReactivePowerB_h >> 12 >= 0x8){ ReactivePowerB_h = ~ReactivePowerB_h + 1; ReactivePowerB = -ReactivePowerB_h; }else{ ReactivePowerB = ReactivePowerB_h; } uint16_t ReactivePowerC_h = rec_data[29] << 8 | rec_data[30]; int ReactivePowerC; if(ReactivePowerC_h >> 12 >= 0x8){ ReactivePowerC_h = ~ReactivePowerC_h + 1; ReactivePowerC = -ReactivePowerC_h; }else{ ReactivePowerC = ReactivePowerC_h; } uint32_t TotalReactivePower_h = rec_data[31] << 24 | rec_data[32] <<16 | rec_data[33] << 8 | rec_data[34]; int TotalReactivePower; if(TotalReactivePower_h >> 28 >= 0x8){ TotalReactivePower_h = ~TotalReactivePower_h + 1; TotalReactivePower = -TotalReactivePower_h; }else{ TotalReactivePower = TotalReactivePower_h; } uint16_t ApparentPowerA_h = rec_data[35] << 8 | rec_data[36]; int ApparentPowerA; if(ApparentPowerA_h >> 12 >= 0x8){ ApparentPowerA_h = ~ApparentPowerA_h + 1; ApparentPowerA = -ApparentPowerA_h; }else{ ApparentPowerA = ApparentPowerA_h; } uint16_t ApparentPowerB_h = rec_data[37] << 8 | rec_data[38]; int ApparentPowerB; if(ApparentPowerB_h >> 12 >= 0x8){ ApparentPowerB_h = ~ApparentPowerB_h + 1; ApparentPowerB = -ApparentPowerB_h; }else{ ApparentPowerB = ApparentPowerB_h; } uint16_t ApparentPowerC_h = rec_data[39] << 8 | rec_data[40]; int ApparentPowerC; if(ApparentPowerC_h >> 12 >= 0x8){ ApparentPowerC_h = ~ApparentPowerC_h + 1; ApparentPowerC = -ApparentPowerC_h; }else{ ApparentPowerC = ApparentPowerC_h; } uint32_t TotalApparentPower_h = rec_data[41] << 24 | rec_data[42] << 16| rec_data[43] << 8 | rec_data[44]; int TotalApparentPower; if(TotalApparentPower_h >> 28 >= 0x8){ TotalApparentPower_h = ~TotalApparentPower_h + 1; TotalApparentPower = -TotalApparentPower_h; }else{ TotalApparentPower = TotalApparentPower_h; } float PowerFactorA1_h =(rec_data[45] << 8 | rec_data[46]); float PowerFactorA = PowerFactorA1_h/100; float PowerFactorB1 =(rec_data[47] << 8 | rec_data[48]); float PowerFactorB = PowerFactorB1/100; float PowerFactorC1 =(rec_data[49] << 8 | rec_data[50]); float PowerFactorC = PowerFactorC1/100; float TotalPowerFactor; uint8_t TotalPowerFactor_h = rec_data[52]; if(TotalPowerFactor_h >> 4 >= 0x8){ TotalPowerFactor_h =~TotalPowerFactor_h + 1; TotalPowerFactor = -(float)TotalPowerFactor_h / 100; }else{ float TotalPowerFactor1 = rec_data[52]; TotalPowerFactor = (float)TotalPowerFactor_h / 100; } float APhaseElectricity1 = rec_data[54] << 24 | rec_data[54] << 16| rec_data[55] << 8 | rec_data[56]; float APhaseElectricity = APhaseElectricity1/10; float BPhaseElectricity1 = rec_data[57] << 24 | rec_data[58] << 16| rec_data[59] << 8 | rec_data[60]; float BPhaseElectricity = BPhaseElectricity1/10; float CPhaseElectricity1 = rec_data[61] << 24 | rec_data[62] << 16| rec_data[63] << 8 | rec_data[64]; float CPhaseElectricity = CPhaseElectricity1/10; uint32_t TotalElectricity1_h = rec_data[65] << 16; uint32_t TotalElectricity2_h = TotalElectricity1_h << 8; float TotalElectricity1 = rec_data[65] << 24 | rec_data[66] << 16| rec_data[67] << 8 | rec_data[68]; float TotalElectricity = TotalElectricity1/10; float Frequency1 =(rec_data[79] << 8 | rec_data[80]); float Frequency = Frequency1/10; float Temperature1 =(rec_data[81] << 8 | rec_data[82]); float Temperature = Temperature1/10;
ana_l.Ia = ProElectricityA; ana_l.Ib = ProElectricityB; ana_l.Ic = ProElectricityC; ana_l.Va = ProVoltageA; ana_l.Vb = ProVoltageB; ana_l.Vc = ProVoltageC; ana_l.Ca = APhaseElectricity; ana_l.Cb = BPhaseElectricity; ana_l.Cc = CPhaseElectricity;
printf("QuannengBan:\n"); printf("Length: %d\n", DataLength_Long); printf("ElectricityA: %.1f\n", ProElectricityA); printf("ElectricityB: %.1f\n", ProElectricityB); printf("ElectricityC: %.1f\n", ProElectricityC); printf("VoltageA: %d\n", ProVoltageA); printf("VoltageB: %d\n", ProVoltageB); printf("VoltageC: %d\n", ProVoltageC); printf("Active Power A: %d\n", ActivePowerA); printf("Active Power B: %d\n", ActivePowerB); printf("Active Power C: %d\n", ActivePowerC); printf("Total Active Power: %d\n", TotalActivePower); printf("Reactive Power A: %d\n", ReactivePowerA); printf("Reactive Power B: %d\n", ReactivePowerB); printf("Reactive Power C: %d\n", ReactivePowerC); printf("Total Reactive Power: %d\n", TotalReactivePower); printf("Apparent Power A: %d\n", ApparentPowerA); printf("Apparent Power B: %d\n", ApparentPowerB); printf("Apparent Power C: %d\n", ApparentPowerC); printf("Total Apparent Power: %d\n", TotalApparentPower); printf("Power Factor A: %.2f\n", PowerFactorA); printf("Power Factor B: %.2f\n", PowerFactorB); printf("Power Factor C: %.2f\n", PowerFactorC); printf("Total Power Factor: %.2f\n", TotalPowerFactor); printf("A Quantity of electric charge: %.1f\n", APhaseElectricity); printf("B Quantity of electric charge: %.1f\n", BPhaseElectricity); printf("C Quantity of electric charge: %.1f\n", CPhaseElectricity); printf("Total Quantity of electric charge: %.1f\n", TotalElectricity); printf("Frequency: %.1f\n", Frequency); printf("Temperature: %.1f\n", Temperature); return ana_l; }
uint32_t get_seconds(void) { time_t current_time = time(NULL); return current_time; } void SendAndRec(float IV, uint16_t type, uint16_t number, unsigned char* p, struct udp_request request){ request.content.value = IV; request.content.sense.type = htons(type); request.content.sense.number = htons(number); unsigned char* v = (char*)&request.command; uint16_t crc_verify = crc16_modbus(v, sizeof(v)); request.verify = htons(crc_verify); unsigned char tmp; p = (char*)&request; tmp = p[20]; p[20] = p[23]; p[23] = tmp;
tmp = p[21]; p[21] = p[22]; p[22] = tmp; int len = sizeof(request); for(int i =0 ;i<len; i++) { printf("%02x ", *p); p++; } printf("\n"); }
int main() { struct udp_request request; struct sockaddr_in server_addr; struct hostent *h; unsigned char* p = (char*)&request; unsigned char* v = (char*)&request.command; uint32_t seconds = get_seconds(); unsigned char buf[1024] ; char buffer[] = "hello"; char send_data[1024] = ""; char rec_data[1024] = ""; char rec_data1[17] = {0x01 ,0x03 ,0x0C ,0x00 ,0x4C ,0x00 ,0x4B ,0x00 ,0x4B ,0x00 ,0xEA ,0x00 ,0xEA ,0x00 ,0xEA ,0x93 ,0x70}; char rec_data2[85] = {0x01 ,0x03 ,0x50 ,0x00 ,0x4C ,0x00 ,0x4C ,0x00 ,0x4B ,0x00 ,0xEA ,0x00 ,0xEA ,0x00 ,0xEA ,0xFF ,0xFF , 0xFF ,0xFF ,0x06 ,0xD3 ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0x00 ,0x77 ,0x00 ,0x64 ,0x00 ,0x64 ,0x00 ,0x8F ,0x00 ,0x00 ,0x00 ,0x02 ,0x00 ,0x00 ,0x00 ,0x02 ,0x00 ,0x00 ,0x00 ,0x02 ,0x00 ,0x00 ,0x00 ,0x06 ,0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,0x01 ,0x01 ,0xF4 ,0x00 ,0xCF ,0x68 ,0xCD}; char *ptr1, *ptr2; int i, j, len1, len2; int data_len = 0; int addr_len = sizeof(struct sockaddr_in); char hostname[] = "www.baidu.com"; char ip[20]; printf("\n"); printf("%p\n\n", p); request.forward = 0xaa; request.reserve = 0x00; request.version = htons(0x0101); request.length = htons(0x000c); request.command = htons(0x0a01); request.content.ticks = htonl(seconds);
uint16_t crc_verify = crc16_modbus(v, sizeof(v)); request.verify = crc_verify;
printf("Forward: 0x%04x\n", request.forward); printf("Version: 0x%04x\n", request.version); printf("Length: 0x%04x\n", request.length); printf("Verify: 0x%04x\n", crc_verify); printf("Command: 0x%04x\n", request.command); printf("Content-Ticks: 0x%04x\n",request.content.ticks); printf("Content-Value:%.2f\n",request.content.value); printf("Content-Type: 0x%04x\n",request.content.sense.type); printf("Content-Number: 0x%04x\n",request.content.sense.number);
printf("\n");
h = gethostbyname(hostname); if(h ==NULL) { perror("GetIP"); exit(1); }
strncpy(ip, inet_ntoa(*((struct in_addr *)h->h_addr)),sizeof(ip)); printf("HostName :%s\n",h->h_name); printf("IP Address1 :%s\n",inet_ntoa(*((struct in_addr *)h->h_addr))); printf("IP Address2 :%s\n",ip); printf("\n");
int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock < 0) { perror("socket creat"); return 0; } printf("socket:%d\n",sock);
memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(ip); server_addr.sin_port = htons(PORT);
int len = sizeof(request); request.content.value = -50; for(int i =0 ;i<len; i++) { printf("%02x ", *p); p++; } printf("\n");
uint16_t num_sense = 0; while(1) {
if(sizeof(rec_data1) < 30){ analysis_s_part ana_s = analysis_s(rec_data1); if(ana_s.Ia != 0 || ana_s.Va != 0){ if(ana_s.Ia != 0){ SendAndRec(ana_s.Ia, 0x0204, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_s.Va != 0){ SendAndRec(ana_s.Va, 0x0206, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} num_sense ++; } if(ana_s.Ib != 0 || ana_s.Vb != 0){ if(ana_s.Ib != 0){ SendAndRec(ana_s.Ib, 0x0204, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_s.Vb != 0){ SendAndRec(ana_s.Vb, 0x0206, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} num_sense ++; } if(ana_s.Ic != 0 || ana_s.Vc != 0){ if(ana_s.Ic != 0){ SendAndRec(ana_s.Ic, 0x0204, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_s.Vc != 0){ SendAndRec(ana_s.Vc, 0x0206, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len);
int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data)); } num_sense ++; }
}else{ analysis_l_part ana_l = analysis_l(buf); if(ana_l.Ia != 0 || ana_l.Va != 0 || ana_l.Ca != 0){ if(ana_l.Ia != 0){ SendAndRec(ana_l.Ia, 0x0204, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_l.Va != 0){ SendAndRec(ana_l.Va, 0x0206, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_l.Ca != 0){ SendAndRec(ana_l.Ca, 0x0202, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} num_sense ++; } if(ana_l.Ib != 0 || ana_l.Vb != 0 || ana_l.Cb != 0){ if(ana_l.Ib != 0){ SendAndRec(ana_l.Ib, 0x0204, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_l.Vb != 0){ SendAndRec(ana_l.Vb, 0x0206, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_l.Cb != 0){ SendAndRec(ana_l.Cb, 0x0202, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} num_sense ++; } if(ana_l.Ic != 0 || ana_l.Vc != 0 || ana_l.Cc != 0){ if(ana_l.Ic != 0){ SendAndRec(ana_l.Ic, 0x0204, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_l.Vc != 0){ SendAndRec(ana_l.Vc, 0x0206, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} if(ana_l.Cc != 0){ SendAndRec(ana_l.Cc, 0x0202, num_sense, p, request); int sendto_len = sendto(sock, p, len, 0, (struct sockaddr *)&server_addr,sizeof(server_addr)); if(sendto_len < 0){ perror("sendto"); return 0; } printf("data:%d\n",sendto_len); int rec_len = recvfrom(sock,rec_data, strlen(rec_data),0,(struct sockaddr *)&server_addr,&addr_len); if(rec_len < 0) { printf("Receive data[%d] failure:%s \n",rec_len-1,strerror(errno)); continue; } printf("Receive data: %s\n", buf); memset(&rec_data, 0, sizeof(rec_data));
} num_sense ++; } } printf("\n");
sleep(2000);
}
close(sock); printf("\n"); return 0; }
|