// 设置为特殊波特率,比如200000
int set_speci_baud(int fd, int baud)
{
struct serial_struct ss, ss_set;
struct termios Opt;
tcgetattr(fd, &Opt);
cfsetispeed(&Opt, B38400);
cfsetospeed(&Opt, B38400);
tcflush(fd, TCIFLUSH);/*handle unrecevie char*/
tcsetattr(fd, TCSANOW, &Opt);
if((ioctl(fd, TIOCGSERIAL, &ss)) < 0)
{
printf("BAUD: error to get the serial_struct info:%s\n", strerror(errno));
return -1;
}
ss.flags = ASYNC_SPD_CUST;
ss.custom_divisor = ss.baud_base/baud;
if((ioctl(fd, TIOCSSERIAL, &ss)) < 0)
{
printf("BAUD: error to set serial_struct:%s\n", strerror(errno));
return -2;
}
ioctl(fd, TIOCGSERIAL, &ss_set);
printf("BAUD: success set baud to %d,custom_divisor=%d,baud_base=%d\n",baud, ss_set.custom_divisor, ss_set.baud_base);
return 0;
}
我用这段代码设置只能设置到最接近的250000,分辨率不够,请问还有什么别的方法吗 |