linux 串口通信 编程 详解,linux 串口通信 编程详解

#include /*标准输入输出定义*/#include /*标准函数库定义*/#include /*Unix 标准函数定义*/#include#include#include /*文件控制定义*/#include /*PPSIX 终端控制定义*/#include /*错误号定义*/

打开串口是通过使用标准的文件打开函数操作:

fd_uart = open(“/dev/ttyS0”, O_RDWR|O_NOCTTY);

if(fd_uart == -1)

{

perror("can‘t open uart");  //串口打开失败

}

终端设备设置

最基本的设置包括波特率设置,效验位和停止位设置。

串口的设置主要是设置 struct termios 结构体的各成员值。

struct termio

{ unsigned short c_iflag; /* 输入模式标志 */

unsigned short c_oflag; /* 输出模式标志 */

unsigned short c_cflag; /* 控制模式标志*/

unsigned short c_lflag; /* local mode flags */

unsigned char c_line; /* line discipline */

unsigned char c_cc[NCC]; /* control characters */

};

其具体意义如下。

c_iflag:输入模式标志,控制终端输入方式,具体参数如表1所示。

表1 c_iflag参数表

键 值                  说 明

IGNBRK       忽略BREAK键输入

BRKINT         如果设置了IGNBRK,BREAK键输入将被忽略

IGNPAR       忽略奇偶校验错误

PARMRK     标识奇偶校验错误

INPCK         允许输入奇偶校验

ISTRIP         去除字符的第8个比特

INLCR             将输入的NL(换行)转换成CR(回车)

IGNCR         忽略输入的回车

ICRNL         将输入的回车转化成换行(如果IGNCR未设置的情况下)

IUCLC         将输入的大写字符转换成小写字符(非POSIX)

IXON         允许输入时对XON/XOFF流进行控制

IXANY         输入任何字符将重启停止的输出

IXOFF         允许输入时对XON/XOFF流进行控制

IMAXBEL         当输入队列满的时候开始响铃

c_oflag:输出模式标志,控制终端输出方式,具体参数如表2所示。

表2 c_oflag参数

键 值    说 明

OPOST    处理后输出

OLCUC    将输入的小写字符转换成大写字符(非POSIX)

ONLCR    将输入的NL(换行)转换成CR(回车)及NL(换行)

OCRNL    将输入的CR(回车)转换成NL(换行)

ONOCR    第一行不输出回车符

ONLRET    不输出回车符

OFILL         发送填充字符以延迟终端输出

OFDEL    以ASCII码的DEL作为填充字符,如果未设置该参数,填充字符为NUL

NLDLY    换行输出延时,可以取NL0(不延迟)或NL1(延迟0.1s)

CRDLY    回车延迟,取值范围为:CR0、CR1、CR2和 CR3

TABDLY    水平制表符输出延迟,取值范围为:TAB0、TAB1、TAB2和TAB3

BSDLY    空格输出延迟,可以取BS0或BS1

VTDLY    垂直制表符输出延迟,可以取VT0或VT1

FFDLY    换页延迟,可以取FF0或FF1

c_cflag:控制模式标志,指定终端硬件控制信息,具体参数如表3所示。

表3 c_cflag参数

键 值                    说 明

CBAUD              波特率(4+1位)(非POSIX)

CBAUDEX         附加波特率(1位)(非POSIX)

CSIZE                   字符长度,取值范围为CS5、CS6、CS7或CS8

CSTOPB         设置两个停止位

CREAD         使用接收器

PARENB         使用奇偶校验

PARODD         对输入使用奇偶校验,对输出使用偶校验

HUPCL         关闭设备时挂起

CLOCAL         忽略调制解调器线路状态

CRTSCTS         使用RTS/CTS流控制

c_lflag:本地模式标志,控制终端编辑功能,具体参数如表4所示。

表4 c_lflag参数

键 值    说 明

ISIG                   当输入INTR、QUIT、SUSP或DSUSP时,产生相应的信号

ICANON                   使用标准输入模式

XCASE              在ICANON和XCASE同时设置的情况下,终端只使用大写。

ECHO         显示输入字符

ECHOE          如果ICANON同时设置,ERASE将删除输入的字符

ECHOK          如果ICANON同时设置,KILL将删除当前行

ECHONL       如果ICANON同时设置,即使ECHO没有设置依然显示换行符

ECHOPRT    如果ECHO和ICANON同时设置,将删除打印出的字符(非POSIX)

TOSTOP    向后台输出发送SIGTTOU信号

c_cc[NCCS]:控制字符,用于保存终端驱动程序中的特殊字符,如输入结束符等。c_cc中定义了如表5所示的控制字符。

表5 c_cc支持的控制字符

宏    说 明    宏    说 明

VINTR    Interrupt字符    VEOL    附加的End-of-file字符

VQUIT    Quit字符    VTIME    非规范模式读取时的超时时间

VERASE    Erase字符    VSTOP    Stop字符

VKILL    Kill字符    VSTART    Start字符

VEOF    End-of-file字符    VSUSP    Suspend字符

VMIN    非规范模式读取时的最小字符数    ?    ?

tcsetattr函数用于设置终端的相关参数。参数fd为打开的终端文件描述符,参数optional_actions用于控制修改起作用的时间,而结构体termios_p中保存了要修改的参数。

optional_actions可以取如下的值:

TCSANOW:不等数据传输完毕就立即改变属性。

TCSADRAIN:等待所有数据传输结束才改变属性。

TCSAFLUSH:清空输入输出缓冲区才改变属性。

错误信息:

EBADF:非法的文件描述符。

EINTR:tcsetattr函数调用被信号中断。

EINVAL:参数optional_actions使用了非法值,或参数termios中使用了非法值。

ENCTTY:非终端的文件描述符。

修改波特率的代码如下:

int speed_arr[] ={ B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300};int name_arr[] = { 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300};/**

[email protected] Set Serial Port BitRate

[email protected] fd Type : int The File Description of Serial Port

[email protected] speed Type : int Serial Speed

[email protected] void*/

void set_speed(int fd, intspeed)

{inti;intstatus;structtermios 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 fd");return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

设置校验的代码

/**

[email protected] Set Serial Port Databits, Stopbits and Parity.

[email protected] fd Type: int The File Description of Serial Port

[email protected] databits Type: int Databits 7 or 8

[email protected] stopbits Type: int Stopbits 1 or 2

[email protected] parity Type: int Parity Type: n,N,e,E,o,O,s,S*/

int set_Parity(int fd, int databits, int parity, intstopbits)

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

{

perror("SetupSerial 1");return(-1);

}

options.c_cflag&= ~CSIZE;switch (databits) /*Set Datebits*/{case 7:

options.c_cflag|=CS7;break;case 8:

options.c_cflag|=CS8;break;default:

fprintf(stderr,"Unsupported data size\n");return(-1);

}switch (parity) /*Set Parity*/{case ‘n‘:case ‘N‘:

options.c_cflag&= ~PARENB; /*Clear parity enable*/options.c_iflag&= ~INPCK; /*Enable parity checking*/

break;case ‘o‘:case ‘O‘:

options.c_cflag|= (PARODD | PARENB); /*Odd Checking*/options.c_iflag|= INPCK; /*Disnable parity checking*/

break;case ‘e‘:case ‘E‘:

options.c_cflag|= PARENB; /*Enable parity*/options.c_cflag&= ~PARODD; /*Even Checking*/options.c_iflag|= INPCK; /*Disnable parity checking*/

break;case ‘S‘:case ‘s‘: /*as no parity*/options.c_cflag&= ~PARENB;

options.c_cflag&= ~CSTOPB;break;default:

fprintf(stderr,"Unsupported parity\n");return(-1);

}switch (stopbits) /*Set Stobits*/{case 1:

options.c_cflag&= ~CSTOPB;break;case 2:

options.c_cflag|=CSTOPB;break;default:

fprintf(stderr,"Unsupported stop bits\n");return(-1);

}/*Set input parity option*/

if (parity != ‘n‘)

options.c_iflag|=INPCK;/*以下两句添加后发送方可以不加回车换行,但是read读取不完整*/options.c_lflag&= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/options.c_oflag&= ~OPOST; /*Output*/

//屏蔽功能: NL(换行)->CR(回车)、CR->NL、忽略输入回车

options.c_iflag &= ~(INLCR | ICRNL |IGNCR);

options.c_oflag&= ~(ONLCR |OCRNL);

tcflush(fd,TCIFLUSH);//未设置O_NONBLOCK或O_NDELAY

options.c_cc[VTIME] = 150; /*Timeout in 15 seconds*/options.c_cc[VMIN]= 0; /*Update the options and do it NOW*/

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

{

perror("SetupSerial 3");return(-1);

}return(0);

}

main函数

int main(void)

{char buf[512] = {0};int nread = 0;char* dev = "/dev/ttyS0";int fd = open(dev, O_RDWR|O_NOCTTY);if (fd > 0)

{

printf("Open %s Success!\n", dev);

}else{

printf("Can‘t Open %s\n", dev);

exit(0);

}

set_speed(fd,115200);if(set_Parity(fd, 8, ‘N‘, 1) == -1)

{

close(fd);

exit(-1);

}while(1)

{if((nread = read(fd,buf,512))>0)

{printf("%s",buf);

}

}

close(fd);

exit(0);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值