linux串口协议解析代码,LINUX串口簡明解析[修正版][0919]

该博客详细介绍了如何在Linux系统下使用C语言打开并配置串口,包括设置波特率、数据位、奇偶校验和停止位。通过`serial_open`、`serial_format_set`和`serial_speed_set`等函数实现串口通信的初始化,并进行了数据写入与读取的示例,展示了如何进行串口的数据交互。
摘要由CSDN通过智能技术生成

#include#include#include#include#include#include#include#include#include#include#include#includetypedefenum{

SERIAL_8N1=0,

SERIAL_7E1=1,

SERIAL_7O1=2,

SERIAL_7S1=3}serial_format;constchar*serial_port[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3","/dev/ttyS4"};intserial_open(intport)

{intfd=open(serial_port[port], O_RDWR|O_NONBLOCK);if(-1==fd)

{

perror("Can't Open Serial Port");

exit (-1);

}else{

fcntl(fd, F_SETFL,0);

}returnfd;

}voidserial_format_set(intfd,serial_format format)

{intstatus=0;structtermios options;if( tcgetattr( fd,&options)!=0)

{

perror("serial fomat abnormal");

exit (-1);

}

options.c_cflag&=~CSIZE;switch(format)

{caseSERIAL_8N1:

options.c_cflag&=~PARENB;

options.c_cflag&=~CSTOPB;

options.c_cflag&=~CSIZE;

options.c_cflag|=CS8;

options.c_iflag&=~(INPCK|ISTRIP);break;caseSERIAL_7E1:

options.c_cflag|=PARENB;

options.c_cflag&=~PARODD;

options.c_cflag&=~CSTOPB;

options.c_cflag&=~CSIZE;

options.c_cflag|=CS7;

options.c_iflag|=(INPCK|ISTRIP);break;caseSERIAL_7O1:

options.c_cflag|=PARENB;

options.c_cflag|=PARODD;

options.c_cflag&=~CSTOPB;

options.c_cflag&=~CSIZE;

options.c_cflag|=CS7;

options.c_iflag|=(INPCK|ISTRIP);break;caseSERIAL_7S1:

options.c_cflag&=~PARENB;

options.c_cflag&=~CSTOPB;

options.c_cflag&=~CSIZE;

options.c_cflag|=CS8;

options.c_iflag&=~INPCK;

options.c_iflag|=ISTRIP;break;default:

perror("serial format abnormal");

exit (-1);

}

tcflush(fd,TCIOFLUSH);

status=tcsetattr(fd, TCSANOW,&options);if(status!=0)

{

perror("tcsetattr format abnormal");

exit(-1);

}

}voidserial_speed_set(intfd,intbaudrate)

{intstatus;structtermios options;/** Get the current options for the port

1421aedabd7ca918716a95773be91a18.gif*/tcgetattr(fd,&options);/** Set the baud rates to 19200

1421aedabd7ca918716a95773be91a18.gif*/cfsetispeed(&options, baudrate);

cfsetospeed(&options, baudrate);/** Enable the receiver and set local mode

1421aedabd7ca918716a95773be91a18.gif*/options.c_cflag|=(CLOCAL|CREAD);/** Set the new options for the port

1421aedabd7ca918716a95773be91a18.gif*/tcflush(fd,TCIOFLUSH); 

status=tcsetattr(fd, TCSANOW,&options);if(status!=0)

{

perror("tcsetattr speed abnormal");

exit(-1);

}

}voidserial_etc_config(intfd)

{intstatus=0;structtermios options;if( tcgetattr( fd,&options)!=0)

{

perror("serial etc abnormal");

exit (-1);

}//no hardware flow controloptions.c_cflag&=~CRTSCTS;//no soft controloptions.c_iflag&=~(IXON|IXOFF|IXANY);//raw set_inputoptions.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);//no output process , raw set_inputoptions.c_oflag&=~OPOST;//min characteroptions.c_cc[VMIN]=1;//time to wait for dataoptions.c_cc[VTIME]=15;

tcflush(fd,TCIOFLUSH);

status=tcsetattr(fd, TCSANOW,&options);if(status!=0)

{

perror("tcsetattr etc abnormal");

exit(-1);

}

}voidserial_config(intfd,intbaudrate,serial_format format)

{

serial_speed_set(fd,baudrate);

serial_format_set(fd,format);

serial_etc_config(fd);

}intmain(intargc,char**argv)

{

fd_set  set_input;intfd;charbuff[50]={'\0',};structtimeval timeout;intret_select=0;intbytes=0;intwrite_num=0;intread_num=0;intwait=0;

fd=serial_open(0);

serial_config(fd,B9600,SERIAL_8N1);

FD_ZERO(&set_input);

FD_SET(fd,&set_input);intloop_test=5;while(loop_test)

{

printf("Loop %d\n",loop_test);

loop_test--;//tcflush(fd,TCIOFLUSH);if(loop_test%2)

{

write_num=write(fd,"hello \nlinux world !\n",strlen("hello \nlinux world !\n"));

}else{

write_num=write(fd,"hello \nserial port !\n",strlen("hello \nserial port !\n"));

}

printf("now %d bytes has been written to the serial port\n",write_num);

timeout.tv_sec=1;

timeout.tv_usec=0;

ret_select=select(fd+1,&set_input, NULL,NULL,&timeout);/*See if there was an error*/if(ret_select<0)

perror("select failed");elseif(ret_select==0)

printf("timeout\n");else{/*We have set_input*/if(FD_ISSET(fd,&set_input))

{

bytes=0;

wait=0;do{

ioctl(fd, FIONREAD,&bytes);

}while( wait++,bytes!=write_num);

printf("now %d bytes is availble to read\n",bytes);

printf("delay %d times for read\n",wait);

read_num=read(fd, buff, bytes);

printf("now %d bytes has been read from serial\n",read_num);

buff[bytes]='\0';

printf("%s", buff);

}

}

printf("************\n");

}

close(fd);

exit (0);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值