Linux system getopt Use example

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <stdint.h>5 6 voidusage ()7 {8 fprintf (stderr,9             "\nusage:mcu_dload [OPTIONS] *.bin\n"Ten             "- A specify device i²c slave address (7Bit), eg:-a 5E, default is 0x58\n" One             "- D Download binary code for MCU only\n" A             "- h This page.\n\n" -     ); -Exit0); the } -  -uint8_t Asc2hex (CharAsccode) - { + uint8_t ret; -     if('0'<= Asccode && Asccode <='9') +ret = Asccode-'0'; A     Else if('a'<= Asccode && Asccode <='F') atret = Asccode-'a'+Ten; -     Else if('A'<= Asccode && Asccode <='F') -ret = Asccode-'A'+Ten; -     ElseRET =0; -     returnret; - } in  - intMainintargcChar**argv) to { +     intopt; -uint32_t i =0; theuint8_t I2c_adr =0x58, I2c_reg; *FILE *FP; $     Panax Notoginseng     //Parse Command Line Options -      while(opt = getopt (argc, argv,"a:h?")) !=EOF) { the         Switch(opt) { +              Case 'a': AI2c_adr = (Asc2hex (optarg[0]) <<4) | Asc2hex (optarg[1]); theprintf"i²c Device Address (7Bit) is 0x%2x.\n", I2c_adr); +                  Break; -              Case 'h': $              Case '?': $             default: - usage (); -                  Break; the         } -     }Wuyi  the     //Last one is the input file -     if(Optind +1!=argc) { Wuprintf"Input File not found!\n"); - usage (); AboutExit0); $     } -  -     //Open *.bin file and download it to MCU -fp = fopen (Argv[optind],"R"); A     if(fp = =NULL) { +printf"Input file can not open!\n"); the usage (); -Exit0); $     } the      the     //Get File Size theFseek (FP,0L, seek_set); theFseek (FP,0L, seek_end); -uint32_t binsize =Ftell (FP); inprintf"inpuf file size is%d bytes.\n", binsize); the  theuint8_t *bincode = (uint8_t *)malloc(binsize); AboutFseek (FP,0L, seek_set); theFread (Bincode,1L, Binsize, FP); the fclose (FP); the      +     //Download -printf"Reset MCU ... \ n"); theprintf"dowloading bin ... \ n");Bayi  theuint32_t dpos = binsize >>6; the      for(i =0; i < binsize; ++i) { -         if(i% Dpos = =0) printf ("#"); -Usleep the); the         //ch341writei2c (0, I2c_adr, I2c_reg, Bincode[i]); the fflush (stdout); the     } theprintf"\nfile Download succeed,%d Bytes transfered!\n", binsize); -  the      Free(Bincode); the     return 0; the}

Linux system getopt Use example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.