| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2021 Guo Wenxue <guowenxue@gmail.com> |
| | | * Copyright: (C) 2021 Guo Wenxue <guowenxue@gmail.com> |
| | | * All rights reserved. |
| | | * |
| | | * Filename: comport.c |
| | | * Description: This file used to do ioctl() on common device or communicate |
| | | * Description: This file used to do ioctl() on common device or communicate |
| | | * with serial port/TTY device. |
| | | * |
| | | * |
| | | * Version: 1.0.0(5/1/2021~) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "5/1/2021 10:08:05 AM" |
| | | * |
| | | * |
| | | ********************************************************************************/ |
| | | #include <getopt.h> |
| | | #include <libgen.h> |
| | |
| | | {NULL, 0, NULL, 0} |
| | | }; |
| | | |
| | | while ((opt = getopt_long(argc, argv, "d:b:s:vh", long_options, NULL)) != -1) |
| | | while ((opt = getopt_long(argc, argv, "d:b:s:xvh", long_options, NULL)) != -1) |
| | | { |
| | | switch (opt) |
| | | { |
| | | case 'd': |
| | | dev_name = optarg; |
| | | break; |
| | | |
| | | |
| | | case 'b': |
| | | baudrate = atoi(optarg); |
| | | break; |
| | |
| | | |
| | | ptr = strdup(name); |
| | | progname = basename(ptr); |
| | | printf("Usage1: comport -d <device> [-b <baudrate>][-s <settings>] [-x]\n"); |
| | | printf(" -d[device ] device name\n"); |
| | | printf("Usage: comport -d <device> [-b <baudrate>][-s <settings>] [-x]\n"); |
| | | printf(" -d[device ] device name, such as /dev/ttyUSB0\n"); |
| | | printf(" -b[baudrate] device baudrate (115200, 57600, 19200, 9600), default is 115200\n"); |
| | | printf(" -s[settings] device settings as like 8N1N(default setting)\n"); |
| | | printf(" -s[settings] device settings as like 8N1N(defalt setting)\n"); |
| | | printf(" - data bits: 8, 7\n"); |
| | | printf(" - parity: N=None, O=Odd, E=Even, S=Space\n"); |
| | | printf(" - stop bits: 1, 0\n"); |
| | |
| | | void stdin_nonblock(void) |
| | | { |
| | | struct termios ttystate; |
| | | |
| | | |
| | | //get the terminal state |
| | | tcgetattr(STDIN_FILENO, &ttystate); |
| | | |
| | |
| | | ttystate.c_lflag &= ~ICANON; |
| | | //minimum of number input read. |
| | | ttystate.c_cc[VMIN] = 1; |
| | | |
| | | |
| | | //set the terminal attributes. |
| | | tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); |
| | | } |
| | | |
| | | } |
| | | |
| | | int kbhit(void) |
| | | { |
| | | { |
| | | struct timeval tv; |
| | | fd_set fds; |
| | | tv.tv_sec = 0; |
| | | tv.tv_usec = 0; |
| | | tv.tv_sec = 0; |
| | | tv.tv_usec = 0; |
| | | FD_ZERO(&fds); |
| | | FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0 |
| | | select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv); |
| | | return FD_ISSET(STDIN_FILENO, &fds); |
| | | } |
| | | } |
| | | |
| | | |