Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Linux下打开串口设置

给出Linux下打开串口函数int open_tty(char tty[])
{
 int fd;
 char tty_path[32]={0};
 sprintf(tty_path,"/dev/%s",tty);
 fd=tty_open_port(tty_path);
 // PORT_SPEED是一个定义的宏,表示传输速率。数据位为8,无校验位,停止位为1
 tty_set_opt(fd,PORT_SPEED,8,"N",1);
 return fd;
}该函数接受一个参数,表示你要打开的串口名称,如“ttyS1”,返回串口操作描述符,在调用该函数后,可以根据返回值来判断是否设置成功,如果fd大于0,则返回成功。 该函数还依赖于两个函数,下面也给出(包括用到的头文件)。#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <error.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <stdlib.h>
extern int tty_open_port(char *tty_num);
extern int tty_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
int tty_open_port(char *tty_num)
{
   int fd = open (tty_num, O_RDWR | O_NOCTTY | O_NDELAY );
   //阻塞 fcntl(fd,F_SETFL,0)  ; //block
   //非阻塞 fcntl(fd,F_SETFL,FNDELAY)  ;
      if (fd == -1)
      {
 printf ("Can"t Open Serial Port %s ! ",tty_num);
 return -1;
      }
      else
      {
    //将串口设置成非阻塞的操作
         fcntl(fd,F_SETFL,FNDELAY);
         return fd;
      }}
/**************************************************************************************
* 功    能:set serial port speed
* 修改历史: 2011.6.29.
**************************************************************************************/
int tty_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
 struct termios newtio,oldtio;
 if  ( tcgetattr( fd,&oldtio)!=0) {
  perror("SetupSerial 1");
  return -1;
 }
 bzero(&newtio, sizeof( newtio ));
 newtio.c_cflag  |=  CLOCAL | CREAD;
 newtio.c_cflag &= ~CSIZE;           //mask the character size bits switch( nBits )
 {
 case 7:
  newtio.c_cflag |= CS7;              //data: 7bits
  break;
 case 8:
  newtio.c_cflag |= CS8;              //data: 8bits
  break;
 } switch( nEvent )
 {
 case "O":
  newtio.c_cflag |= PARENB;
  newtio.c_cflag |= PARODD;
  newtio.c_iflag |= (INPCK | ISTRIP);
  break;
 case "E":
  newtio.c_iflag |= (INPCK | ISTRIP);
  newtio.c_cflag |= PARENB;
  newtio.c_cflag &= ~PARODD;
  break;
 case "N":
  newtio.c_cflag &= ~PARENB;
  break;
 }
 switch( nSpeed )          //set the bps
 {
 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 19200:
  cfsetispeed(&newtio, B19200);
  cfsetospeed(&newtio, B19200);
  break;
 case 115200:
  cfsetispeed(&newtio, B115200);
  cfsetospeed(&newtio, B115200);
  break;
 case 460800:
  cfsetispeed(&newtio, B460800);
  cfsetospeed(&newtio, B460800);
  break;
 default:
  cfsetispeed(&newtio, B9600);
  cfsetospeed(&newtio, B9600);
  break;
 } if( nStop == 1 )                //set the 1bit stop
  newtio.c_cflag &=  ~CSTOPB;
 else if ( nStop == 2 )       //set the 2bit stop
  newtio.c_cflag |=  CSTOPB;
 newtio.c_cc[VTIME]  = 0;
 newtio.c_cc[VMIN] = 0;
 tcflush(fd,TCIFLUSH);
 if((tcsetattr(fd,TCSANOW,&newtio))!=0)
 {
  perror("com set error");
  return -1;
 }
 printf("Current serial speed is %d ",nSpeed);
 return 0;
}代码中给出了可以设置成阻塞和非阻塞的操作。本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-03/114719.htm