linux uart串口编程
/*
* 初始化配置rs232串口设备
* 参数 fd: 设备文件描述符
* speed: rs232串口设备波特率
* databits: rs232串口设备数据位位数
* parity: rs232串口设备数据奇偶校验设置
* stopbits: rs232串口设备数据停止位位数
* 返回值: 配置成功返回零, 否则返回非零.
*
* */
int serial_port_init(int fd,int speed,int databits,char parity,int stopbits)
{
struct termios oldtio,newtio;
int status,i;
#ifdef DEBUG
printf("%s fd = %d\n speed = %d\tdatabits = %d\tparity = %c\tstopbits = %d\n", __func__, fd, speed, databits, parity, stopbits);
#endif
#if 1
/* 保存之前的串口配置 */
bzero(&newtio,sizeof(struct termios));
if(tcgetattr(fd,&oldtio)!=0){
perror("tcgetattr ");
return -1;
}
newtio=oldtio;
#endif
newtio.c_cflag |= CLOCAL;
newtio.c_cflag |= CREAD;
newtio.c_cflag &= ~ISTRIP;
#ifdef RAW_UART
cfmakeraw(&newtio);
#endif
/* 设置数据位数 */
switch (databits){
case 7: /* 七位数据位 */
newtio.c_cflag &= ~CSIZE;