(七)Linux库的串口开发


基于官方提供的串口测试

代码部分

创龙T113-i官方资料包给的代码:

uart_rw.c,功能很高端,支持读取、写入和回环测试三种模式!

/* Copyright 2018 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <libgen.h>
#include <signal.h>
#include <getopt.h>

#define NOPASS_CONDITIONS 3
#define INADEQUATE_CONDITIONS 10

enum Mode {
    READ, WRITE, LOOPBACK };

/* Exit flag */
volatile bool g_quit = false;

/* Short option names */
static const char g_shortopts [] = ":d:s:rwvhl";

/* Option names */
static const struct option g_longopts [] = {
   
    {
    "device",      required_argument,      NULL,        'd' },
    {
    "read",        no_argument,            NULL,        'r' },
    {
    "write",       no_argument,            NULL,        'w' },
    {
    "loopback",    no_argument,            NULL,        'l' },
    {
    "size",        required_argument,      NULL,        's' },
    {
    "version",     no_argument,            NULL,        'v' },
    {
    "help",        no_argument,            NULL,        'h' },
    {
    0, 0, 0, 0 }
};

static void usage(FILE *fp, int argc, char **argv) {
   
    fprintf(fp,
            "Usage: %s [options]\n\n"
            "Options:\n"
            " -d | --device        Device such as '/dev/ttyS0'\n"
            " -r | --read          Read\n"
            " -w | --write         Write\n"
            " -l | --loopback      loopback test\n"
            " -s | --size          Read size\n"
            " -v | --version       Display version information\n"
            " -h | --help          Show help content\n"
            " e.g. %s -d /dev/ttyS1 -r -s 256\n"
            "      %s -d /dev/ttyS1 -w -s 1024\n"
            "      %s -d /dev/ttyS1 -l -s 1024\n\n"
            "", argv[0], argv[0], argv[0], argv[0]);
}

static void opt_parsing_err_handle(int argc, char **argv, int flag) {
   
    /* Exit if no input parameters are entered  */
    int state = 0;
    if (argc < 2) {
   
        printf("No input parameters are entered, please check the input.\n");
        state = -1;
    } else {
   
        /* Feedback Error parameter information then exit */
        if (optind < argc || flag) {
   
            printf("Error:  Parameter parsing failed\n");
            if (flag)
                printf("\tunrecognized option '%s'\n", argv[optind-1]);

            while (optind < argc) {
   
                printf("\tunrecognized option '%s'\n", argv[optind++]);
            }

            state = -1;
        }
    }

    if (state == -1) {
   
        printf("Tips: '-h' or '--help' to get help\n\n");
        exit(2);
    }
}

void sig_handle(int arg) {
   
    g_quit = true;
}

int init_serial(int *fd, const char *dev) {
   
    struct termios opt;

    /* open serial device */
    if ((*fd = open(dev, O_RDWR)) < 0) {
   
        perror("open()");
        return -1;
    }

    /* define termois */
    if (tcgetattr(*fd, &opt) < 0) {
   
        perror("tcgetattr()");
        return -1;
    }

    opt.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);
    opt.c_oflag  &= ~OPOST;

    /* Character length, make sure to screen out this bit before setting the data bit */
    opt.c_cflag &= ~CSIZE;

    /* No hardware flow control */
    opt.c_cflag &= ~CRTSCTS;

    /* 8-bit data length */
    opt.c_cflag |= CS8;

    /* 1-bit stop bit */
    opt.c_cflag &= ~CSTOPB;

    /* No parity bit */
    opt.c_iflag |= IGNPAR;

    /* Output mode */
    opt.c_oflag = 0;
    
    /* No active terminal mode */
    opt.c_lflag = 0;

    /* Input baud rate */
    if (cfsetispeed(&opt, B115200) < 0)
        return -1;

    /* Output baud rate */
    if (cfsetospeed(&opt, B115200) < 0)
        return -1;

    /* Overflow data can be received, but not read */
    if (tcflush(*fd, TCIFLUSH) < 0)
        return -1;

    if (tcsetattr(*fd, TCSANOW, &opt) < 0)
        return -1;

    return 0;
}

int serial_write(int *fd, const <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小爪.exe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值