基于官方提供的串口测试
代码部分
创龙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 <