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

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


咨詢
建站咨詢
