Linux uart寄存器读写,Linux下读写UART串口的代码
Linux下讀寫UART串口的代碼,從IBM Developer network上拿來的東西,操作比較的復(fù)雜,就直接跳過了,好在代碼能用,記錄一下~
兩個(gè)實(shí)用的函數(shù)~
/**
*@brief 設(shè)置串口通信速率
*@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 設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位
*@param fd 類型 int 打開的串口文件句柄
*@param databits 類型 int 數(shù)據(jù)位 取值 為 7 或者8
*@param stopbits 類型 int 停止位 取值為 1 或者2
*@param parity 類型 int 效驗(yàn)類型 取值為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) /*設(shè)置數(shù)據(jù)位數(shù)*/
{
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); /* 設(shè)置為奇效驗(yàn)*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 轉(zhuǎn)換為偶效驗(yàn)*/
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);
}
/* 設(shè)置停止位*/
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; /* 設(shè)置超時(shí)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);
}
調(diào)用的方法比較的簡(jiǎn)單,例如以下。fd是打開的tty設(shè)備的文件句柄
set_speed(fd,115200);
if (set_Parity(fd,8,1,'N') == FALSE) {
printf("Set Parity Error
");
}
總的測(cè)試代碼例如以下。
#include
#include
#include
#include
#include
#define BAUDRATE B115200
#define UART_DEVICE "/dev/ttyS3"
#define FALSE -1
#define TRUE 0
/**
*@brief 設(shè)置串口通信速率
*@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 設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位
*@param fd 類型 int 打開的串口文件句柄
*@param databits 類型 int 數(shù)據(jù)位 取值 為 7 或者8
*@param stopbits 類型 int 停止位 取值為 1 或者2
*@param parity 類型 int 效驗(yàn)類型 取值為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) /*設(shè)置數(shù)據(jù)位數(shù)*/
{
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); /* 設(shè)置為奇效驗(yàn)*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 轉(zhuǎn)換為偶效驗(yàn)*/
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);
}
/* 設(shè)置停止位*/
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; /* 設(shè)置超時(shí)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;
}
總結(jié)
以上是生活随笔為你收集整理的Linux uart寄存器读写,Linux下读写UART串口的代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英国传教士傅兰雅给曾国藩的礼物是什么
- 下一篇: linux 信号处理实验,linux下信