串口输入十六进制字符串数据,接收后转换为十进制数据
int main(int argc,char * argv[])
{
int fd;
int recv_buf[64];
int addr = 0,reg = 0,rw,i2cnum;
char i2cdev[64];
if(argc!=4)
{
printf("Usage:i2c_read [ADDR] [REG] [I2CBUSNUM]\n");
return 0;
}
//addr=atoi(argv[1]);
//reg=atoi(argv[2]);
i2cnum=atoi(argv[3]);
sprintf(i2cdev,"/dev/i2c-%d",i2cnum);
sscanf(argv[1],"%x",&addr); //通过sscanf函数转换
sscanf(argv[2],"%x",®);
fd=open(i2cdev,O_RDWR);
if(fd==-1)
{
printf("Can not open the device!\n");
return -1;
}
recv_buf[0]=i2c_read(fd,addr,reg);
printf("READ I2C SLAVEADDR=0x%02X, ADDR=0x%02X DATA=0x%02X\n",addr,reg,recv_buf[0]);
close(fd);
return 0;
}