嵌入式Linux下串口编程

嵌入式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 <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <error.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <termios.h>

 

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 <stdio.h>

#include <stdlib.h>

#include <error.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <termios.h>

#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”数据。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值