Linux下串口模块的使用与实现 (linux 串口 模块)

随着计算机技术的不断发展,串口通信技术在很多领域得到了广泛应用,特别是在工业自动化、数据采集和控制等方面。在Linux系统中,串口模块的使用与实现是一门必须掌握的技能。本文将介绍Linux下串口模块的使用方法和实现原理。

一、串口通信介绍

串口通信是一种广泛使用的数据传输方式,其特点是数据传输速度较慢、传输距离较短(一般不超过数千米),但传输稳定性高、可靠性强,适用于控制和监视设备等应用场景。

需要说明的是,在计算机中,串口通信是通过串口接口实现的,串口分为COM口(在Windows系统中)和TTY口(在Linux系统中)。

二、Linux下串口口设备文件的命名规则

在Linux系统中,串口设备被视为一个文件,在/dev/目录下以ttySx(x表示串口号,0-3表示COM1-4,4-7表示/dev/ttyUSB0-3)或ttyUSBx(x表示USB串口号,从0开始计数)的形式存在。

这里需要注意的是,使用USB串口时,需要先插入USB串口,然后使用dmesg命令查看串口号,或者使用ls /dev/ttyUSB*命令查看可用的USB串口设备。

三、串口模块的安装

在Linux系统中,串口模块是通过内核模块的形式实现的。可以通过modprobe命令加载串口模块,也可以在内核编译时将串口模块编译进内核。

1.modprobe命令加载串口模块

如果系统中没有预装串口模块,需要手动加载串口模块,可以使用以下命令:

“`bash

# 加载serial_core模块,该模块包含常见的串口驱动

sudo modprobe serial_core

“`

如果需要使用特定的串口驱动,则需要加载相应的串口驱动模块。例如,加载USB串口驱动:

“`bash

# 加载USB串口驱动,其中userial为USB串口驱动,cp210x为USB串口芯片的驱动

sudo modprobe userial

sudo modprobe cp210x

“`

2.内核编译时编译串口模块

在编译内核时,可以将串口模块编译进内核,具体方法如下:

(1)进入Linux内核源代码所在目录:

“`bash

cd /usr/src/linux

“`

(2)打开配置文件:

“`bash

sudo make menuconfig

“`

(3)在menuconfig中选择“Device Drivers”选项,然后选择“Serial drivers”, 在下面打开,也就是M或者*:

“`text

General setup -> Serial drivers ->Serial console support

“`

(4)保存配置后退出,并编译内核:

“`bash

sudo make

sudo make install

“`

(5)重启系统。

四、使用串口模块

在Linux系统中,串口模块的使用需要调用相应的系统调用,并确保设置正确的串口参数。以下是串口模块的使用流程:

1.打开串口设备文件

在打开串口前,需要先获取串口设备文件的文件描述符,使用open()函数可以打开串口文件的文件描述符。下面是打开/dev/ttyS0串口的示例:

“`c

#include

#include

#include

#include

int open_serialport(char *portname)

{

int fd;

fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);

if (fd == -1)

{

return (-1);

}

else

{

return fd;

}

}

“`

2.设置串口参数

在打开串口设备文件后,需要设置正确的串口参数,包括波特率、数据位、校验位、停止位等。下面是设置波特率为9600,数据位为8位,无校验位,停止位为1的示例:

“`c

int set_serialport(int fd, int baudrate, int databits, int parity, int stopbits)

{

struct termios options;

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

{

printf(“error %d from tcgetattr”, errno);

return -1;

}

/* Set input and output baudrate.*/

cfsetispeed(&options, baudrate);

cfsetospeed(&options, baudrate);

/* Set data bits */

options.c_cflag &= ~CSIZE;

switch (databits)

{

case 5:

options.c_cflag |= CS5;

break;

case 6:

options.c_cflag |= CS6;

break;

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr, “Unsupported data size.\n”);

return -1;

}

/* Set parity */

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); /* Enable odd parity*/

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

break;

case ‘e’:

case ‘E’:

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

options.c_cflag &= ~PARODD; /* Convert to even parity*/

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

break;

case ‘s’:

case ‘S’:

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;

break;

default:

fprintf(stderr, “Unsupported parity.\n”);

return -1;

}

/* Set stop bits */

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr, “Unsupported stop bits.\n”);

return -1;

}

/* Enable raw input and output mode */

options.c_cflag |= (CLOCAL | CREAD);

/* Disable software flow control */

options.c_iflag &= ~(IXON | IXOFF | IXANY);

/* Set input mode */

options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);

/* Set output mode */

options.c_oflag &= ~OPOST;

/* Set raw FIFO mode for input and output */

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

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

options.c_cc[VTIME] = 1; /* Set timeout value (in 1/10 sec)*/

options.c_cc[VMIN] = 60; /* Set minimum number of characters */

/* Commit new setting */

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

{

printf(“error %d from tcsetattr”, errno);

return -1;

}

return 0;

}

“`

3.读写串口数据

在完成串口设备文件的打开和参数设置后,就可以进行串口数据的读写操作。使用read()函数可以从串口读取数据,使用write()函数可以向串口写入数据。下面是读取和写入串口数据的示例:

“`c

int read_serialport(int fd, char *buf, int len)

{

int n;

n = read(fd, buf, len);

if (n

{

printf(“error %d from read”, errno);

}

return n;

}

int write_serialport(int fd, char *buf, int len)

{

int n;

n = write(fd, buf, len);

if (n

{

printf(“error %d from write”, errno);

}

return n;

}

“`

4.关闭串口设备文件

在完成串口数据读写操作后,需要关闭串口设备文件。使用close()函数可以关闭串口设备文件。下面是关闭串口设备文件的示例:

“`c

void close_serialport(int fd)

{

close(fd);

}

“`

五、

相关问题拓展阅读:

linux 下,串口读取很多数据 放到1.txt里

你的这个串口设备在打开(也就是调用open函数获取设备描述符)的时候设置的是非阻塞方式。导致串口上没数据拦耐的时候read也立即返雀衡衡回,但是你的while已经把顷做有效的数据读走了,if里面读到的一定是空的,所以什么也不打印。

建议

1. 在打开串口设备时使用阻塞方式,不会设置的话查查open系统调用的帮助,它有个flag;

2. 把while循环内的if语句去掉。

linux 串口调试工具有哪些

工具有这些:picocom, kermit, minicom

对比:

picocom:

优点:简单,文字可以有颜色,不会改变终端的背景(我喜欢半透明的)

缺点:启动简判和关闭的速度较慢

minicom:

优点:启动速度快

缺点:当设置有颜色时(minicom -c on),背景不能设置透明, 比较蛋疼,另外中文显示有问题(加 -R utf-8 也不行),拦没改察猜再另外,串口数据不断输出到终端的时候,不好复制已有的数据(会动)。

kermit:

优点:功能强大,有自己的脚本语言和命令行

缺点:我暂时不需要这些功能,

linux 串口 模块的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux 串口 模块,Linux下串口模块的使用与实现,linux 下,串口读取很多数据 放到1.txt里,linux 串口调试工具有哪些的信息别忘了在本站进行查找喔。


数据运维技术 » Linux下串口模块的使用与实现 (linux 串口 模块)