新聞中心
隨著計(jì)算機(jī)技術(shù)的不斷發(fā)展,串口通信技術(shù)在很多領(lǐng)域得到了廣泛應(yīng)用,特別是在工業(yè)自動(dòng)化、數(shù)據(jù)采集和控制等方面。在Linux系統(tǒng)中,串口模塊的使用與實(shí)現(xiàn)是一門必須掌握的技能。本文將介紹Linux下串口模塊的使用方法和實(shí)現(xiàn)原理。

創(chuàng)新互聯(lián)專注于府谷網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供府谷營(yíng)銷型網(wǎng)站建設(shè),府谷網(wǎng)站制作、府谷網(wǎng)頁(yè)設(shè)計(jì)、府谷網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開發(fā)服務(wù),打造府谷網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供府谷網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
一、串口通信介紹
串口通信是一種廣泛使用的數(shù)據(jù)傳輸方式,其特點(diǎn)是數(shù)據(jù)傳輸速度較慢、傳輸距離較短(一般不超過(guò)數(shù)千米),但傳輸穩(wěn)定性高、可靠性強(qiáng),適用于控制和監(jiān)視設(shè)備等應(yīng)用場(chǎng)景。
需要說(shuō)明的是,在計(jì)算機(jī)中,串口通信是通過(guò)串口接口實(shí)現(xiàn)的,串口分為COM口(在Windows系統(tǒng)中)和TTY口(在Linux系統(tǒng)中)。
二、Linux下串口口設(shè)備文件的命名規(guī)則
在Linux系統(tǒng)中,串口設(shè)備被視為一個(gè)文件,在/dev/目錄下以ttySx(x表示串口號(hào),0-3表示COM1-4,4-7表示/dev/ttyUSB0-3)或ttyUSBx(x表示USB串口號(hào),從0開始計(jì)數(shù))的形式存在。
這里需要注意的是,使用USB串口時(shí),需要先插入U(xiǎn)SB串口,然后使用dmesg命令查看串口號(hào),或者使用ls /dev/ttyUSB*命令查看可用的USB串口設(shè)備。
三、串口模塊的安裝
在Linux系統(tǒng)中,串口模塊是通過(guò)內(nèi)核模塊的形式實(shí)現(xiàn)的??梢酝ㄟ^(guò)modprobe命令加載串口模塊,也可以在內(nèi)核編譯時(shí)將串口模塊編譯進(jìn)內(nèi)核。
1.modprobe命令加載串口模塊
如果系統(tǒng)中沒有預(yù)裝串口模塊,需要手動(dòng)加載串口模塊,可以使用以下命令:
“`bash
# 加載serial_core模塊,該模塊包含常見的串口驅(qū)動(dòng)
sudo modprobe serial_core
“`
如果需要使用特定的串口驅(qū)動(dòng),則需要加載相應(yīng)的串口驅(qū)動(dòng)模塊。例如,加載USB串口驅(qū)動(dòng):
“`bash
# 加載USB串口驅(qū)動(dòng),其中userial為USB串口驅(qū)動(dòng),cp210x為USB串口芯片的驅(qū)動(dòng)
sudo modprobe userial
sudo modprobe cp210x
“`
2.內(nèi)核編譯時(shí)編譯串口模塊
在編譯內(nèi)核時(shí),可以將串口模塊編譯進(jìn)內(nèi)核,具體方法如下:
(1)進(jìn)入Linux內(nèi)核源代碼所在目錄:
“`bash
cd /usr/src/linux
“`
(2)打開配置文件:
“`bash
sudo make menuconfig
“`
(3)在menuconfig中選擇“Device Drivers”選項(xiàng),然后選擇“Serial drivers”, 在下面打開,也就是M或者*:
“`text
General setup -> Serial drivers ->Serial console support
“`
(4)保存配置后退出,并編譯內(nèi)核:
“`bash
sudo make
sudo make install
“`
(5)重啟系統(tǒng)。
四、使用串口模塊
在Linux系統(tǒng)中,串口模塊的使用需要調(diào)用相應(yīng)的系統(tǒng)調(diào)用,并確保設(shè)置正確的串口參數(shù)。以下是串口模塊的使用流程:
1.打開串口設(shè)備文件
在打開串口前,需要先獲取串口設(shè)備文件的文件描述符,使用open()函數(shù)可以打開串口文件的文件描述符。下面是打開/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.設(shè)置串口參數(shù)
在打開串口設(shè)備文件后,需要設(shè)置正確的串口參數(shù),包括波特率、數(shù)據(jù)位、校驗(yàn)位、停止位等。下面是設(shè)置波特率為9600,數(shù)據(jù)位為8位,無(wú)校驗(yàn)位,停止位為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.讀寫串口數(shù)據(jù)
在完成串口設(shè)備文件的打開和參數(shù)設(shè)置后,就可以進(jìn)行串口數(shù)據(jù)的讀寫操作。使用read()函數(shù)可以從串口讀取數(shù)據(jù),使用write()函數(shù)可以向串口寫入數(shù)據(jù)。下面是讀取和寫入串口數(shù)據(jù)的示例:
“`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.關(guān)閉串口設(shè)備文件
在完成串口數(shù)據(jù)讀寫操作后,需要關(guān)閉串口設(shè)備文件。使用close()函數(shù)可以關(guān)閉串口設(shè)備文件。下面是關(guān)閉串口設(shè)備文件的示例:
“`c
void close_serialport(int fd)
{
close(fd);
}
“`
五、
相關(guān)問題拓展閱讀:
- linux 下,串口讀取很多數(shù)據(jù) 放到1.txt里
- linux 串口調(diào)試工具有哪些
linux 下,串口讀取很多數(shù)據(jù) 放到1.txt里
你的這個(gè)串口設(shè)備在打開(也就是調(diào)用open函數(shù)獲取設(shè)備描述符)的時(shí)候設(shè)置的是非阻塞方式。導(dǎo)致串口上沒數(shù)據(jù)攔耐的時(shí)候read也立即返雀衡衡回,但是你的while已經(jīng)把頃做有效的數(shù)據(jù)讀走了,if里面讀到的一定是空的,所以什么也不打印。
建議
1. 在打開串口設(shè)備時(shí)使用阻塞方式,不會(huì)設(shè)置的話查查open系統(tǒng)調(diào)用的幫助,它有個(gè)flag;
2. 把while循環(huán)內(nèi)的if語(yǔ)句去掉。
linux 串口調(diào)試工具有哪些
工具有這些:picocom, kermit, minicom
對(duì)比:
picocom:
優(yōu)點(diǎn):簡(jiǎn)單,文字可以有顏色,不會(huì)改變終端的背景(我喜歡半透明的)
缺點(diǎn):?jiǎn)?dòng)簡(jiǎn)判和關(guān)閉的速度較慢
minicom:
優(yōu)點(diǎn):?jiǎn)?dòng)速度快
缺點(diǎn):當(dāng)設(shè)置有顏色時(shí)(minicom -c on),背景不能設(shè)置透明, 比較蛋疼,另外中文顯示有問題(加 -R utf-8 也不行),攔沒改察猜再另外,串口數(shù)據(jù)不斷輸出到終端的時(shí)候,不好復(fù)制已有的數(shù)據(jù)(會(huì)動(dòng))。
kermit:
優(yōu)點(diǎn):功能強(qiáng)大,有自己的腳本語(yǔ)言和命令行
缺點(diǎn):我暫時(shí)不需要這些功能,
linux 串口 模塊的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于linux 串口 模塊,Linux下串口模塊的使用與實(shí)現(xiàn),linux 下,串口讀取很多數(shù)據(jù) 放到1.txt里,linux 串口調(diào)試工具有哪些的信息別忘了在本站進(jìn)行查找喔。
成都服務(wù)器租用選創(chuàng)新互聯(lián),先試用再開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)提供簡(jiǎn)單好用,價(jià)格厚道的香港/美國(guó)云服務(wù)器和獨(dú)立服務(wù)器。物理服務(wù)器托管租用:四川成都、綿陽(yáng)、重慶、貴陽(yáng)機(jī)房服務(wù)器托管租用。
當(dāng)前名稱:Linux下串口模塊的使用與實(shí)現(xiàn) (linux 串口 模塊)
瀏覽路徑:http://fisionsoft.com.cn/article/dpjjhgd.html


咨詢
建站咨詢
