日韩性视频-久久久蜜桃-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串口的代码的全部內容,希望文章能夠幫你解決所遇到的問題。

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