Linux基础串口编程

Linux串口的编程,过程也是配置像停止位、奇偶校验位、波特率和数据位。像打开文件一样打开串口设备,然后配置上面说的几个参数。串口的写用标准的Linux系统调用write,读用read。其中还有一些配置,在代码中在详细阐述,下面直接贴代码:

首先用一个serial.cfg的配置文件来指定串口的参数,后面就直接更改配置文件来修改参数。我们把这个配置文件放置在/etc/下面

DEV=/dev/tq2440_serial2
SPEED=115200
DATABITS=8
STOPBITS=1
PARITY=N

这里的设备文件用已经有的串口驱动生成的设备节点。

定义头文件Serial.h,定义表征串口的结构体

#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H

#define TRUE    1
#define FALSE    0

#define z_U32  unsigned int
#define z_U8    unsigned char

#define SERIAL_NAME_LEN 32
extern int serial_config();
extern int set_speed(int fd);
extern int set_other_parm(int fd);

typedef struct z_Serial_port {

z_U8  serial_name[SERIAL_NAME_LEN];
 z_U32 serial_baud;
 z_U8  data_bits;
 z_U8  stop_bits;
 z_U8  parity;

}HI_Serial_port;

extern z_Serial_port get_current_serial();
#endif

接下来当然是serial.c文件咯,实现串口的各个参数的设置

/*
** author:  z
** CopyRight: z
** funtion: serial port Interface
** Date:  2014
*/

#include <stdio.h> /**/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "pthread.h"

#include "serial_port.h"

#define CFG_FILE "/etc/serial.cfg"//放置配置文件的地方 
#define DEBUG 1
#define ERROR -1
#define OK    1

/*Serial Port Struct*/
z_Serial_port serial_port ;

int speed_array[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800,
                    B2400, B1200, B300, B38400, B19200, B9600, B4800,
                    B2400, B1200, B300};
int name_array[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800,
                    2400, 1200, 300, 38400, 19200, 9600, 4800,
                    2400, 1200, 300};

//得到当前的串口结构体 

z_Serial_port get_current_serial(){
 z_Serial_port current_serial;
 memset(current_serial.serial_name, 0, sizeof(current_serial.serial_name));

strncpy(current_serial.serial_name, serial_port.serial_name, sizeof(current_serial.serial_name));
  current_serial.serial_baud = serial_port.serial_baud;
  current_serial.data_bits = serial_port.data_bits;
  current_serial.stop_bits = serial_port.stop_bits;
  current_serial.parity = serial_port.parity;
  if(DEBUG){
   printf("current_serial.serial_baud = %d\n", current_serial.serial_baud);
   printf("current_serial.data_bits = %d\n" , current_serial.data_bits);
   printf("current_serial.stop_bits = %d\n", current_serial.stop_bits);
   printf("current_serial.parity = %c\n", current_serial.parity);
  }
  return current_serial;
}

//读取串口的配置文件,并将配置填充到结构体 

int serial_config()
{
    FILE *serial_fp;
    char read_buf[20] =  {0};
    char temp[20];
    char parity;

/*read serial config from file*/
    serial_fp = fopen(CFG_FILE, "r");
    if(serial_fp == NULL){
        printf("Can't open serial config file\n");
        return ERROR;
    }
    memset(serial_port.serial_name, 0 ,sizeof(serial_port.serial_name));
    fscanf(serial_fp, "DEV=%s\n", serial_port.serial_name);
   
    fscanf(serial_fp, "SPEED=%s\n", temp);
    serial_port.serial_baud = atoi(temp);

fscanf(serial_fp, "DATABITS=%s\n", temp);
    serial_port.data_bits = atoi(temp);

fscanf(serial_fp, "STOPBITS=%s\n", temp);
    serial_port.stop_bits = atoi(temp);

fscanf(serial_fp, "PARITY=%s\n", temp);
    serial_port.parity = temp[0];

if(DEBUG){
        printf(" Serial Dev = %s\n", serial_port.serial_name);
        printf(" Serial Speed = %d\n", serial_port.serial_baud);
        printf(" Serial DataBits = %d\n", serial_port.data_bits);
        printf(" Serial StopBite = %d\n ", serial_port.stop_bits);
        printf(" Serial Parity = %c\n", serial_port.parity);
    }
    fclose(serial_fp);
    return OK;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9902c22a4323317f41939272800d3efa.html