linux下串口通信程序,关于Linux下串口通信的一点心得

1.

打开串口

与其他的关于设备编程的方法一样,在

Linux

下,操作、控制串口也是通过操作起设备文件进行的。在

Linux

下,串口的设备文件是

/dev/ttyS0

/dev/ttyS1

等。因此要读些串口,我们首先要打开串口:

char *dev= "/dev/ttyS0"; //

串口

1

intfd = open( dev, O_RDWR );

//| O_NOCTTY | O_NDELAY

if (-1 == fd)

{

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

return -1;

}

else

return fd;

2.

设置串口速度

打开串口成功后,我们就可以对其进行读写了。首先要设置串口的波特率:

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {38400,19200,9600,4800,2400,1200,300, 38400,

19200,9600, 4800, 2400, 1200,300, };

void set_speed(int fd, int speed){

inti;

intstatus;

struct termiosOpt;

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);

}

}

}

3.

设置串口信息

这主要包括:数据位、停止位、奇偶校验位这些主要的信息。

/**

*@brief

设置串口数据位,停止位和效验位

*@paramfd

类型

int

打开的串口文件句柄

*@paramdatabits

类型

int

数据位

取值

7

或者

8

*@paramstopbits

类型

int

停止位

取值为

1

或者

2

*@paramparity

类型

int

效验类型

取值为

N,E,O,,S

*/

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

{

struct termios options;

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

perror("SetupSerial 1");

return(FALSE);

}

options.c_cflag &= ~CSIZE;

options.c_lflag&= ~(ICANON | ECHO | ECHOE | ISIG);/*Input*/

options.c_oflag&= ~OPOST;/*Output*/

switch (databits) /*

设置数据位数

*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

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

}

switch (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); /*

设置为奇效验

*/

options.c_iflag |= INPCK;/* Disnable parity checking */

break;

case 'e':

case 'E':

options.c_cflag |= PARENB;/* Enable parity */

options.c_cflag &= ~PARODD;/*

转换为偶效验

*/

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 (FALSE);

}

/*

设置停止位

*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

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

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 0; /*

设置超时

0 seconds*/

options.c_cc[VMIN] = 13; /* define the minimum bytes data to be readed*/

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

{

perror("SetupSerial 3");

return (FALSE);

}

return (TRUE);

}

在上述代码中,有两句话特别重要:

options.c_cc[VTIME] = 0; /*

设置超时

0 seconds*/

options.c_cc[VMIN] = 13; /* define the minimum bytes data to be readed*/

这两句话决定了对串口读取的函数

read()

的一些功能。我将着重介绍一下他们对

read()

函数的影响。

对串口操作的结构体是

Struct{

tcflag_t c_iflag;/*

输入模式标记

*/

tcflag_t c_oflag;/*

输出模式标记

*/

tcflag_t c_cflag;/*

控制模式标记

*/

tcflag_t c_lflag;/*

本地模式标记

*/

cc_tc_line;/*

线路规程

*/

cc_tc_cc[NCCS];/*

控制符号

*/

}

其中

cc_tc_line

只有在一些特殊的系统程序

(

比如,设置通过

tty

设备来通信的网络协议

)

中才会用。在数组

c_cc

中有两个下标

(VTIME

VMIN)

对应的元素不是控制符,并且只是在原始模式下有效。只有在原始模式下,他们决定了

read()

函数在什么时候返回。在标准模式下,除非设置了

O_NONBLOCK

选项,否则只有当遇到文件结束符或各含的字符都已经编辑完毕后才返回。

控制符

VTIME

VMIN

之间有着复杂的关系。

VTIME

定义要求等待的零到几百号妙的是间量

(

通常是一个

8

位的

unsigned char

变量,取值不能大于

cc_t)

VMIN

定义了要求等待的最小字节数

(

不是要求读的字节数——

read()

的第三个参数才是指定要求读的最大字节数

)

,这个字节数可能是

0

l

如果

VTIME

0

VMIN

定义了要求等待读取的最小字节数。函数

read()

只有在读取了

VMIN

个字节的数据或者收到一个信号的时候才返回。

l

如果

VMIN

0

VTIME

定义了即使没有数据可以读取,

read()

函数返回前也要等待几百毫秒的时间量。这时,

read()

函数不需要像其通常情况那样要遇到一个文件结束标志才返回

0

l

如果

VTIME

VMIN

等不取

0

VTIME

定义的时当接收到底一个自己的数据后开始计算等待的时间量。如果当调用

read

函数时可以得到数据,计时器马上开始计时。如果但调用

read

函数时还没有任何数据可读,则等接收到底一个字节的数据后,计时器开始计时。函数

read

可能会在读取到

VMIN

个字节的数据后返回,也可能在计时完毕后返回,这主要取决于哪个条件首先实现。不过函数至少会读取到一个字节的数据,因为计时器是在读取到第一个数据时开始计时的。

l

如果

VTIME

VMIN

都取

0

,即使读取不到任何数据,函数

read

也会立即返回。同时,返回值

0

表示

read

函数不需要等待文件结束标志就返回了。

这就是这两个变量对

read

函数的影响。我使用的读卡器每次传送的数据是

13

个字节,一开始,我把它们设置成

options.c_cc[VTIME] = 150

options.c_cc[VMIN] = 0;

结果,每次读取的信息只有

8

个字节,剩下的

5

个字节要等到下一次打卡时才能收到。就是由于这个原因造成的。根据上面规则的第一条,我把

VTIME

0

VMIN=13

,也就是正好等于一次需要接收的字节数。这样就实现了一次读取

13

个字节值。同时,得出这样的结论,如果读卡器送出的数据为

n

个字节,那么就把

VMIN=n

,这样一次读取的信息正好为读卡器送出的信息,并且读取的时候不需要进行循环读取。

4.

读取数据

有了上面的函数后,我们设置了串口的基本信息,根据我们自己的实际情况,设置了相应的参数,就可以读取数据了。

void getcardinfo(char *buff){

int fd;

int nread,count=0;

char tempbuff[13];

char *dev= "/dev/ttyS0"; //

串口

1

fd = OpenDev(dev);

set_speed(fd,9600);

if (set_Parity(fd,8,1,'N') == FALSE){

printf("Set Parity Error\n");

//return -1;

}

while (1) //

循环读取数据

{

count=0;

//sleep(5000);

while(1)

{

if((nread = read(fd, tempbuff, 13))>0)

{

//printf("\nLen %d\n",nread);

memcpy(&buff[count],tempbuff,nread);

count+=nread;

}

if(count==13)

{

buff[count+1] = '\0';

//printf( "\n%s", buff);

break;

}

}

//break;

}

//return buff;

close(fd);

pthread_exit(NULL);

//close(fd);

// exit (0);

}

这是我原来的程序,其实把

VMIN

设置以后,可以改成:

void getcardinfo(char *buff){

int fd;

int nread,count=0;

char tempbuff[13];

char *dev= "/dev/ttyS0"; //

串口

1

fd = OpenDev(dev);

set_speed(fd,9600);

if (set_Parity(fd,8,1,'N') == FALSE){

printf("Set Parity Error\n");

//return -1;

}

nread = read(fd, buff, 13)

close(fd);

}

5.

程序完整代码:

#include/*

标准输入输出定义

*/

#include/*

标准函数库定义

*/

#include/*Unix

标准函数定义

*/

#include

#include

#include/*

文件控制定义

*/

#include/*PPSIX

终端控制定义

*/

#include/*

错误号定义

*/

#define FALSE-1

#define TRUE0

/**

*@brief

设置串口通信速率

*@paramfd

类型

int

打开串口的文件句柄

*@paramspeed

类型

int

串口速度

*@returnvoid

*/

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {38400,19200,9600,4800,2400,1200,300, 38400,

19200,9600, 4800, 2400, 1200,300, };

void set_speed(int fd, int speed){

inti;

intstatus;

struct termiosOpt;

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);

}

}

}

/**

*@brief

设置串口数据位,停止位和效验位

*@paramfd

类型

int

打开的串口文件句柄

*@paramdatabits

类型

int

数据位

取值

7

或者

8

*@paramstopbits

类型

int

停止位

取值为

1

或者

2

*@paramparity

类型

int

效验类型

取值为

N,E,O,,S

*/

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

{

struct termios options;

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

perror("SetupSerial 1");

return(FALSE);

}

options.c_cflag &= ~CSIZE;

options.c_lflag&= ~(ICANON | ECHO | ECHOE | ISIG);/*Input*/

options.c_oflag&= ~OPOST;/*Output*/

switch (databits) /*

设置数据位数

*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

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

}

switch (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); /*

设置为奇效验

*/

options.c_iflag |= INPCK;/* Disnable parity checking */

break;

case 'e':

case 'E':

options.c_cflag |= PARENB;/* Enable parity */

options.c_cflag &= ~PARODD;/*

转换为偶效验

*/

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 (FALSE);

}

/*

设置停止位

*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

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

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 0; /*

设置超时

15 seconds*/

options.c_cc[VMIN] = 13; /* define the minimum bytes data to be readed*/

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

{

perror("SetupSerial 3");

return (FALSE);

}

return (TRUE);

}

/**********************************************************************

代码说明:使用串口一测试的,发送的数据是字符,

但是没有发送字符串结束符号,所以接收到后,后面加上了结束符号

**********************************************************************/

/*********************************************************************/

int OpenDev(char *Dev)

{

intfd = open( Dev, O_RDWR );

//| O_NOCTTY | O_NDELAY

if (-1 == fd)

{

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

return -1;

}

else

return fd;

}

void getcardinfo(char *buff){

int fd;

int nread,count=0;

char tempbuff[13];

char *dev= "/dev/ttyS0"; //

串口

1

fd = OpenDev(dev);

set_speed(fd,9600);

if (set_Parity(fd,8,1,'N') == FALSE){

printf("Set Parity Error\n");

//return -1;

}

while (1) //

循环读取数据

{

count=0;

//sleep(5000);

while(1)

{

if((nread = read(fd, tempbuff, 13))>0)

{

//printf("\nLen %d\n",nread);

memcpy(&buff[count],tempbuff,nread);

count+=nread;

}

if(count==13)

{

buff[count+1] = '\0';

//printf( "\n%s", buff);

break;

}

}

//break;

}

//return buff;

close(fd);

pthread_exit(NULL);

//close(fd);

// exit (0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值