Linux基础串口编程(2)

/*
 * Set Baudrate
 *  设置波特率,fd是打开设备节点的句柄 
 * */
int set_speed(int fd)
{
    struct termios option;
    struct termios old_option;
    int i = 0;

//get the old option
    tcgetattr(fd, &old_option);// termios是所要操作的结构体

for (i = 0; i < sizeof(speed_array)/sizeof(int); i++){
        if(serial_port.serial_baud == name_array[i]){
    tcflush(fd, TCIOFLUSH);
        cfsetispeed(&option, speed_array[i]);
        cfsetospeed(&option, speed_array[i]);

if(tcsetattr(fd, TCSANOW, &option) != 0){//配置立即生效
  printf("set serial baudrate failed\n");
  return ERROR;
    }else{
  tcflush(fd, TCIOFLUSH);//清空输入输出队列
        if(DEBUG) printf("set serial baudrate sucessed\n");
  return OK;
    }
 }
    }
}

/*
 * Set Other Paramters
 *设置其他的参数
 * */
int set_other_parm(int fd)
{
    struct termios options;
    struct termios old_options;

if(tcgetattr(fd, &old_options) != 0){
 printf("Failed to getattr\n");
 return ERROR;
    }
    options.c_cflag |= (CLOCAL|CREAD);
    options.c_cflag &= ~CSIZE;

/*set Date Bits*/
    switch (serial_port.data_bits){
        case 7:
    options.c_cflag |= CS7;
    break;
 case 8:
    options.c_cflag |= CS8;
    break;
 default:
    options.c_cflag |= CS8;
        break;
    }

/*Set Parity Bites*/
    switch (serial_port.parity){
 case 'n':
 case 'N':
    options.c_cflag &= ~PARENB;
    options.c_iflag &= ~INPCK;
    break;
 case 'o':
 case 'O':
    options.c_cflag |= (PARODD|PARENB);
    options.c_iflag |= INPCK;
    break;
 case 'e':
 case 'E':
    options.c_cflag |= PARENB;
    options.c_cflag &= ~PARODD;
    options.c_iflag |= INPCK;
    break;
 default:
    options.c_cflag &= ~PARENB;
    options.c_iflag &= ~INPCK;
        break;
    printf("default parity none parity\n");
    }

/*Set Stop Bites*/
    switch (serial_port.stop_bits){
 case 1:
    options.c_cflag &= ~CSTOPB;
    break;
 case 2:
    options.c_cflag |= CSTOPB;
    break;
 default:
    options.c_cflag &= ~CSTOPB;
    }
    if(serial_port.parity != 'n' || serial_port.parity != 'N'){
 options.c_iflag |= INPCK;
    }
    options.c_cc[VTIME] = 0;//Timeout parma  超时时间的设置
    options.c_cc[VMIN] = 0;//read x bite return 读到多少个字节才进行操作,这里设置为0

options.c_iflag |= IGNPAR|ICRNL;
    options.c_oflag |= OPOST;//原始模式,有两种模式,若不是原始模式的话,则会在\n后,才会输出
    options.c_iflag &= ~(IXON|IXOFF|IXANY); 


    tcflush(fd, TCIFLUSH);
    if(tcsetattr(fd, TCSANOW, &options) != 0){
 printf("set serial other parma failed\n");
 return ERROR;
    }
    return OK;
}

到这里,基本的参数就配置完毕了,这里需要注意VMIN和VTIME的配置和原始模式的配置。

接口写好了,写个测试的程序来进行测试就好,测试程序写的比较丑,见谅,哈哈:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

#include "serial_port.h"

int main(int argc, char *argv[])
{
 int ret;
 int fd;
 unsigned char data[50];

z_Serial_port serial_p; 

printf("Configing serial from local file...\n");
 ret = serial_config();
 if(ret != 1){
  printf("Failed to config serial\n");
  return -1;
 }
 serial_p = get_current_serial();
 fd = open(serial_p.serial_name,O_RDWR, 0);//以读写的方式打开
 printf(".opening. %s\n", serial_p.serial_name);
 if(fd == -1){
  printf("Can't open tty\n");
  return -1;
 }
 // set speed
    if(set_speed(fd) < 0){
  printf("set speed Failed\n");
  return -1;
 }
 if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0){
  printf("fcntl failed\n");
  return -1;
 }
    if(isatty(STDIN_FILENO)==0)
    {
        printf("not a terminal device\n");
    }else
        printf("isatty success!\n");
 //set other parma like stopbites etc.
    if(set_other_parm(fd) < 0 ){
  printf("set other parm failed\n");
  return -1;
 }
 //write data
 int i;
    char buff[512];
    char buff21[] = "Hi Babby!";

int nread,nwrite = 0;
    int nx;
    printf("fd = %d\n",fd );
    nx = write(fd,buff21,sizeof(buff21));

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

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