Chinaunix首页 | 论坛 | 博客
  • 博客访问: 386224
  • 博文数量: 115
  • 博客积分: 2501
  • 博客等级: 少校
  • 技术积分: 1009
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-23 17:05
文章分类

全部博文(115)

文章存档

2011年(2)

2010年(86)

2009年(27)

我的朋友

分类: LINUX

2009-10-23 18:36:05

嵌入式Linux下串口编程

一、配置内核

在嵌入式Linux下进行串口编程之前,先在内核中配置串口部分,如下:

Device Drivers--->

       character devices--->

              Serial drivers--->

                     <*>Samssung S3C2440/S3C2442 Serial port support

二、应用程序

C文件:

uart_init.c:打开设备、初始化串口(设置参数)

main.c:测试串口读写

头文件:

uart_init.h

Makfile

 

 

uart_init.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

int open_termios(char *dev)

{

       int fd = -1;

       if(-1 == (fd = open(dev,O_RDWR|O_NOCTTY|O_NONBLOCK)))

       {

              printf("open termios error!\n");

       }

       return fd;

}

 

int set_termios(int fd,long speed,char databit,char stopbit,char oebit)

{

       struct termios newtio;

       int err = -1;

      

       //清零结构体

       bzero(&newtio,sizeof(newtio));

       //设置接收使能

       newtio.c_cflag |= CLOCAL | CREAD;

       // 设置数据位

       switch(databit)

       {

              case 7:

                     newtio.c_cflag |= CS7;

                     break;

              case 8:

                     newtio.c_cflag |= CS8;

                     break;

              default:

                     newtio.c_cflag |= CS8;

                     break;

       }

       //设置停止位

       switch(stopbit)

       {

              case 1:

                     newtio.c_cflag &= ~CSTOPB;

                     break;

              case 2:

                     newtio.c_cflag |= CSTOPB;

                     break;

              default:

                     newtio.c_cflag &= ~CSTOPB;

                     break;

       }

       //设置校验位

       switch(oebit)

       {

              case 'O':    //奇校验

                     newtio.c_cflag |= PARENB;

                     newtio.c_cflag |= (INPCK | ISTRIP);

                     newtio.c_cflag |= PARODD;

                     break;

              case 'E':    //偶数

                     newtio.c_cflag |= PARENB;

                     newtio.c_cflag |= (INPCK | ISTRIP);

                     newtio.c_cflag &= ~PARODD;

                     break;

              case 'N':    //不校验

                     newtio.c_cflag &= ~PARODD;

                     break;

              default:

                      //不校验

                     newtio.c_cflag &= ~PARODD;

                     break;

       }

       //设置波特率

       switch(speed)

       {

              case 2400:

                     cfsetispeed(&newtio,B2400);

                     cfsetospeed(&newtio,B2400);

                     break;

              case 4800:

                     cfsetispeed(&newtio,B4800);

                     cfsetospeed(&newtio,B4800);

                     break;

              case 9600:

                     cfsetispeed(&newtio,B9600);

                     cfsetospeed(&newtio,B9600);

                     break;    

              case 57600:

                     cfsetispeed(&newtio,B57600);

                     cfsetospeed(&newtio,B57600);

                     break;    

              case 115200:

                     cfsetispeed(&newtio,B115200);

                     cfsetospeed(&newtio,B115200);

                     break;    

              default:

                     cfsetispeed(&newtio,B9600);

                     cfsetospeed(&newtio,B9600);

                     break;    

       }

       //设置等待时间和最小接收字符数

       newtio.c_cc[VTIME] = 0;

       newtio.c_cc[VMIN] = 0;

       //处理未接收字符

       tcflush(fd,TCIFLUSH);

       //激活配置

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

              perror("com set error!");

       else

              err = 0;

       perror("com set done!");

       return err;

}

 

 

uart_init.h

#ifndef __UART_INIT__

#define __UART_INIT__

extern int open_termios(char *dev);

int set_termios(int fd,long speed,char databit,char stopbit,char oebit);

#endif

 

 

main.c

#include

#include

#include

#include

#include

#include

#include

#include "uart_init.h"

 

char *uart0_dev="/dev/ttySAC0";

 

int main(void)

{

       int uart0_fd;

       int err = 0;

       char uart0_byte = 0;     

       char *uart0_buff[8]={0};

 

       uart0_fd = open_termios(uart0_dev);

       if(uart0_fd < 0)

       {

              exit(1);

       }

       do

       {

              err = set_termios(uart0_fd,115200,8,1,'N');

       }

       while(-1 == err);

       while(1)

       {

              read(uart0_fd,&uart0_byte,1);

              if(0xfd == uart0_byte)         

              {

                     read(uart0_fd,uart0_buff,8);

                     write(uart0_fd,uart0_buff,8);

                     memset(uart0_buff,0,sizeof(uart0_buff));

                     uart0_byte = 0;

              }           

             

       }

       exit(0);

}

 

 

Makefile

#You can add source code file like the following

SOURCE += main.c

SOURCE += uart_init.c

 

##You can add header code file like the following

HEADER += uart_init.h

 

 

#You can name the executable file like the following

TARGET = UART_ARM

 

#You can add include path like the following

INCLUDEPATH =

#You can library path like the following

LIBPATH =

#You can add library like the following

LIBS = -lpthread

 

 

#You cant add compile options like the following

OPTION += -Wall 

OPTION += -g

 

#You can specify the compiler

CC=arm-linux-gcc

 

#Don't modify the following code unless you know howto exactly

OBJECTS =$(SOURCE:%.c=%.o)

 

$(TARGET):$(OBJECTS)

       $(CC) $(OPTION) $(INCLUDEPATH) $(LIBPATH) $(LIBS) -o $(TARGET) $(OBJECTS)

clean:

       -rm -f $(TARGET) *.o *~

在串口调试助手选择波特率,选择十六进制显示并打开串口后,发送“fd 31 32 33 34 35 36 37 38”这串十六进制数据后,接收端就出现“31 32 33 34 35 36 37 38”数据。

阅读(1438) | 评论(0) | 转发(1) |
0

上一篇:ARM大小端

下一篇:ARM 8位图

给主人留下些什么吧!~~