壹:main sscanf sprintf

随手写一段代码,作为博客第一篇,看代码


#include 
#include 
#include 

int main(int,char **);

int main(int argc,char **argv)
{
    char buff[40];
    unsigned int address=0,num=0;
    char *p = "the weather is good\r\n";
    char *Read_Command = "OWIRA=f5N=3";
    char *Write_Command = "OWIWA=a8D=3";

    memset(buff,0,40*sizeof(char)); 
    if(memcmp(Read_Command,"OWIRA",5) == 0)
    {
        sscanf(Read_Command,"OWIRA=%x[^A-N=]",&address);
        sscanf(Read_Command,"OWIRA=%*xN=%d",&num);
        sprintf(buff,"{%x:%d}\r\n",address,num);
        printf("%s",buff);
    }

    if(memcmp(Write_Command,"OWIWA",5) == 0)
    {
        sscanf(Write_Command,"OWIWA=%x[^A-N=]",&address);
        sscanf(Write_Command,"OWIWA=%*xD=%d",&num);
        sprintf(buff,"{%x:%d}\r\n",address,num);
        printf("%s",buff);
    }

    if(argc==4)
    {
        sprintf(buff,"%s %s %s\r\n",argv[1],argv[2],argv[3]);
        printf("%s",buff);
    }
    printf("the weather is good\r\n");

    return 0;
}

代码功能

  • 1:简单的不能再粗暴的上位机和下位机通信协议;
  • 2:“OWIRA=f5N=3”读协议:"OWI"单总线、'R'表示读、'A'表示寄存器地址、N表示读取字节数;
  • 3:"OWIWA=a8D=3"写协议,'D'表示写入的数据,其它同上;
  • 4:此段代码没有实现具体的功能,代码只负责解析协议并打印出来;
  • 5:代码已在ubuntu10.04上验证。

你可能感兴趣的:(壹:main sscanf sprintf)