linux 串口问题

binbinxiaogui 2010-10-13 11:20:05
接受到的数据有异常,
buff[2]=11
buff[3]=22
buff[4]=33
buff[5]=44
buff[6]=55
buff[7]=66
buff[8]=77
buff[9]=ffffff88
buff[10]=ffffff99
buff[11]=0
buff[12]=ffffffaa
buff[13]=ffffffbb
buff[14]=ffffffcc
buff[15]=ffffffdd
buff[16]=ffffffee
buff[17]=ffffffff
应该是,11 ,22,33,44,55,66,77,88,99,00,aa,bb,cc,dd,ee,ff,
怎么让buff 里的数据变成这个样的,求大侠没帮忙,有需要协助的尽管提出来


...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
binbinxiaogui 2010-10-13
  • 打赏
  • 举报
回复
int port_init(int fd,int speed,int databits,int stopbits,int parity)
{
int i;
int status;
struct termios opt;
tcgetattr(fd,&opt);
for(i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if(speed==name_arr[i])
{
tcflush(fd,TCIOFLUSH);
cfsetispeed(&opt,speed_arr[i]);
cfsetospeed(&opt,speed_arr[i]);
status=tcsetattr(fd,TCSANOW,&opt);
if(status!=0)
{
perror("tcsetattr fail\n");
return -1;
}
tcflush(fd,TCIOFLUSH);
}
}
if(tcgetattr(fd,&opt)!=0)
{
perror("tcgetattr is Fail\n");
return -1;
}
opt.c_cflag&=~CSIZE;
switch(databits)
{
case 7:
opt.c_cflag|=CS7;
break;
case 8:
opt.c_cflag&=~CSIZE;
opt.c_cflag|=CS8;
break;
default:
printf("UNsupprted data\n");
return -1;
}
switch(parity)
{
case 'N':
opt.c_cflag&=~PARENB;
opt.c_iflag&=~INPCK;
break;
case 'O':
opt.c_cflag|=(PARODD|PARENB);
opt.c_iflag|=INPCK;
break;
case 'E':
opt.c_cflag|=PARENB;
opt.c_cflag&=~PARODD;
opt.c_iflag|=INPCK;
break;
default:
printf("Unsupptred parity\n");
return -1;
}
switch(stopbits)
{
case 1:
opt.c_cflag&=~CSTOPB;
break;
case 2:
opt.c_cflag|=CSTOPB;
break;
default:
printf("Unsupported stopbits\n");
return -1;
}
/* opt.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
opt.c_oflag&=~OPOST;
opt.c_iflag&=~(INLCR|IGNCR|ICRNL);
opt.c_oflag&=~(ONLCR|OCRNL);
opt.c_cc[VTIME]=150;
opt.c_cc[VMIN]=0;*/

tcflush(fd,TCIFLUSH);
opt.c_cc[VTIME]=150;
opt.c_cc[VMIN]=0;

opt.c_cflag|=(CLOCAL|CREAD);
opt.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
// opt.c_oflag&=~OPOST;
// opt.c_oflag&=~(INLCR|IGNCR|ICRNL);
opt.c_iflag&=~(IXON|IXOFF|IXANY);
if(tcsetattr(fd,TCSANOW,&opt)!=0)
{
perror("setupsertal fail");
return -1;
}
tcflush(fd,TCIOFLUSH);
return 0;
}
binbinxiaogui 2010-10-13
  • 打赏
  • 举报
回复
if((nwrite=write(fd,Checkup_Password,sizeof(Checkup_Password)))>0)
printf("1write Checkup_Password YES\n");
else
printf("2write Checkup_Password NO\n");

memset(buff,'\0',sizeof(buff));
if((nread=read(fd,buff,sizeof(buff)))>0)
{printf("3YSE read\n");
printf("bufffff=%x\n",buff[0]);
printf("bufffff=%x\n",buff[1]);}
else
printf("4O read\n");
if(buff[0]==0x01&&buff[1]==0x00)
{
printf("5 Password YES\n");
//mi ma zheng que
}
else if(buff[0]==0x01&&buff[1]==0x01)
{
//mi ma cuo wu
goto star;
}

memset(buff,'\0',sizeof(buff));

if((nwrite=write(fd,read_block,sizeof(read_block)))>0)
printf("6write Checkup_Password YES\n");
else
printf("7write Checkup_Password NO\n");

memset(buff,'\0',sizeof(buff));
sleep(1);
if((nread=read(fd,buff,1024))>0)
{
printf("8YSE read\n");
nwrite=write(fd,Horn,sizeof(Horn));
}
else
printf("NO read\n");




printf("buff[0]=%x\n",buff[0]);
printf("buff[1]=%x\n",buff[1]);
printf("buff[2]=%x\n",buff[2]);
printf("buff[3]=%x\n",buff[3]);
printf("buff[4]=%x\n",buff[4]);
printf("buff[5]=%x\n",buff[5]);
printf("buff[6]=%x\n",buff[6]);
printf("buff[7]=%x\n",buff[7]);
printf("buff[8]=%x\n",buff[8]);
printf("buff[9]=%x\n",buff[9]);
printf("buff[10]=%x\n",buff[10]);
printf("buff[11]=%x\n",buff[11]);
printf("buff[12]=%x\n",buff[12]);
printf("buff[13]=%x\n",buff[13]);
printf("buff[14]=%x\n",buff[14]);
printf("buff[15]=%x\n",buff[15]);
printf("buff[16]=%x\n",buff[16]);
printf("buff[17]=%x\n",buff[17]);
}
九个太阳2023 2010-10-13
  • 打赏
  • 举报
回复
buff是如何定义的啊?
贴点上下文代码
justkk 2010-10-13
  • 打赏
  • 举报
回复
定义为unsigned char buff[100]; 试试看
或者输出的时候,强制转换一下(unsigned char)buff[9]
手机写程序 2010-10-13
  • 打赏
  • 举报
回复
定义:
unsigned char buff[256];
打印:
printf("buff[0]=%02X\n",buff[0]);

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧