日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux uart寄存器读写,Linux下读写UART串口的代码

發布時間:2025/4/5 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux uart寄存器读写,Linux下读写UART串口的代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux下讀寫UART串口的代碼,從IBM Developer network上拿來的東西,操作比較的復雜,就直接跳過了,好在代碼能用,記錄一下~

兩個實用的函數~

/**

*@brief 設置串口通信速率

*@param fd 類型 int 打開串口的文件句柄

*@param speed 類型 int 串口速度

*@return void

*/

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

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

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

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

void set_speed(int fd, int speed){

int i;

int status;

struct termios 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 fd1");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

/**

*@brief 設置串口數據位,停止位和效驗位

*@param fd 類型 int 打開的串口文件句柄

*@param databits 類型 int 數據位 取值 為 7 或者8

*@param stopbits 類型 int 停止位 取值為 1 或者2

*@param parity 類型 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;

switch (databits) /*設置數據位數*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size

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

");

return (FALSE);

}

/* 設置停止位*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits

");

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 150; /* 設置超時15 seconds*/

options.c_cc[VMIN] = 0; /* Update the options and do it NOW */

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

{

perror("SetupSerial 3");

return (FALSE);

}

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

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

return (TRUE);

}

調用的方法比較的簡單,例如以下。fd是打開的tty設備的文件句柄

set_speed(fd,115200);

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

printf("Set Parity Error

");

}

總的測試代碼例如以下。

#include

#include

#include

#include

#include

#define BAUDRATE B115200

#define UART_DEVICE "/dev/ttyS3"

#define FALSE -1

#define TRUE 0

/**

*@brief 設置串口通信速率

*@param fd 類型 int 打開串口的文件句柄

*@param speed 類型 int 串口速度

*@return void

*/

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

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

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

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

void set_speed(int fd, int speed){

int i;

int status;

struct termios 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 fd1");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

/**

*@brief 設置串口數據位,停止位和效驗位

*@param fd 類型 int 打開的串口文件句柄

*@param databits 類型 int 數據位 取值 為 7 或者8

*@param stopbits 類型 int 停止位 取值為 1 或者2

*@param parity 類型 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;

switch (databits) /*設置數據位數*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size

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

");

return (FALSE);

}

/* 設置停止位*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits

");

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 150; /* 設置超時15 seconds*/

options.c_cc[VMIN] = 0; /* Update the options and do it NOW */

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

{

perror("SetupSerial 3");

return (FALSE);

}

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

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

return (TRUE);

}

int main(int argc, char *argv[])

{

int fd, c=0, res;

char buf[256];

printf("Start...

");

fd = open(UART_DEVICE, O_RDWR);

if (fd < 0) {

perror(UART_DEVICE);

exit(1);

}

printf("Open...

");

set_speed(fd,115200);

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

printf("Set Parity Error

");

exit (0);

}

printf("Reading...

");

while(1) {

res = read(fd, buf, 255);

if(res==0)

continue;

buf[res]=0;

printf("%s", buf);

if (buf[0] == 0x0d)

printf("

");

if (buf[0] == '@') break;

}

printf("Close...

");

close(fd);

return 0;

}

總結

以上是生活随笔為你收集整理的Linux uart寄存器读写,Linux下读写UART串口的代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲中文一区二区三区 | 欧美视频在线观看一区 | 欧美日韩精品二区 | 国产中文字幕三区 | 国产网红主播精品av | 精品99久久久久成人网站免费 | 国产精品久久无码一三区 | 国产中文 | 一本久草 | 久久av中文字幕 | av资源站 | 免费黄色国产 | 色偷偷网站| 一个人看的视频www 色就是色网站 | 姑娘第5集在线观看免费好剧 | 欧美日韩精品一二三区 | 无码成人一区二区 | 久草av在线播放 | 男人日女人在线观看 | 95久久 | 中文字幕国产视频 | 日韩经典午夜福利发布 | 国产一二三区在线视频 | 美女搡bbb又爽又猛又黄www | 国产精品无码久久久久高潮 | 日日草天天干 | 亚洲精品尤物 | 极品91尤物被啪到呻吟喷水 | 久草网站 | 国产黄网站 | 伊人影院综合在线 | 国内自拍视频网站 | 久久新网址 | av资源网在线 | 欧美成人午夜影院 | 亚洲一区二区在线观看视频 | 波多野结衣办公室双飞 | 一本大道视频 | 国产白嫩美女无套久久 | 狼人久久 | 三级在线国产 | 亚洲免费国产 | 探花av在线 | 免费观看一区二区三区视频 | 中文字字幕码一二三区 | 国产精品va在线观看无码 | 特级黄色片 | 韩国三级做爰视频 | 亚洲高清视频网站 | 日本欧美三级 | 人人射人人爽 | 国产噜噜噜 | 成人免费视频网站 | 一区二区在线观看免费 | 中文字幕第一 | 亚洲午夜网站 | 日本在线视频不卡 | 北条麻妃一区二区三区 | 超碰666 | 五月婷婷激情四射 | 日韩国产一区二区三区 | 国产精品免费视频一区 | 欧美高清日韩 | 私人毛片 | 老牛影视一区二区三区 | 欧美日韩亚洲国产一区 | 日本人妻不卡一区二区三区中文字幕 | 欧美不卡在线视频 | 亚洲精品久久久久中文字幕二区 | 伊人av影院| 日本免费在线视频 | 激情五月综合网 | 人妻少妇偷人精品无码 | 美女隐私无遮挡网站 | 日本大乳奶做爰 | av一区二区在线观看 | 亚洲7777 | 国产日韩精品一区二区三区在线 | a级片在线免费观看 | 新呦u视频一区二区 | 李丽珍裸体午夜理伦片 | 亚日韩欧美 | 波多野结衣在线一区 | 亚洲 欧美 日韩 国产综合 在线 | 久久久www | 国产影音先锋 | 日本妈妈3 | 毛片毛多水多 | 日本伦理一区 | 久久免费少妇高潮99精品 | 91av看片 | 欧美无砖专区免费 | 日韩一二三四区 | 亚洲欧美精品 | 秋霞福利| 久久99精品久久久久久水蜜桃 | 非洲黑寡妇性猛交视频 | 一级视频在线播放 | 成人在线观看18 |