简体   繁体   English

从串口linux读取

[英]Read from serial port linux

I'm trying to read from serial port, but always get 0 (zero) characters back. 我正在尝试从串口读取,但总是得到0(零)字符。 Already read the "Serial Programming Guide for POSIX Operating Systems", but can't find out why the program not waiting (blocking). 已经阅读了“POSIX操作系统的串行编程指南”,但无法找出为什么程序没有等待(阻塞)。 The code: 编码:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

void main()
{
    printf("Hello world\n");

    int fd; /* File descriptor for the port */
    int n;
    int bytes;

    char c;

    char buffer[10];
    char *bufptr;

    struct termios options;

    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (fd == -1) {
        perror("open_port: Unable to open /dev/ttyUSB0 - ");
    }
    else {
        fcntl(fd, F_SETFL, FNDELAY);
    }

  tcgetattr( fd, &options );

  /* SEt Baud Rate */

  cfsetispeed( &options, B9600 );
  cfsetospeed( &options, B9600 );

  //I don't know what this is exactly

  options.c_cflag |= ( CLOCAL | CREAD );

  // Set the Charactor size

  options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  options.c_cflag |= CS8;    /* Select 8 data bits */

  // Set parity - No Parity (8N1)

  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;

  // Disable Hardware flowcontrol

  //  options.c_cflag &= ~CNEW_RTSCTS;  -- not supported

  // Enable Raw Input

  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

  // Disable Software Flow control

  options.c_iflag &= ~(IXON | IXOFF | IXANY);

  // Chose raw (not processed) output

  options.c_oflag &= ~OPOST;

  if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
    printf ("Error with tcsetattr = %s\n", strerror ( errno ) );
  else
    printf ( "%s\n", "tcsetattr succeed" );

    fcntl(fd, F_SETFL, 0);

    // Write to the port
    n = write(fd, "1", 1);

    if (n < 0) {
        fputs("write() of 1 bytes failed!\n", stderr);
    }

    // Read from the port

    //fcntl(fd, F_SETFL, FNDELAY);

    bytes = read(fd, &buffer, sizeof(buffer));
    printf("number of bytes read is %d\n", bytes);
    printf("%s\n", buffer);
    //perror ("read error:");

    close(fd);
}

This information was originally from the Serial Programming Guide 1 . 此信息最初来自串行编程指南1

The reason you are getting a 0 return value is because of this line: 你得到0返回值的原因是因为这一行:

fcntl(fd, F_SETFL, FNDELAY);

If you want a normal blocking read, unset that flag. 如果您想要正常的阻塞读取,请取消设置该标志。

1. http://www.easysw.com/~mike/serial/serial.html#2_5_4 (now defunct) 1. http://www.easysw.com/~mike/serial/serial.html#2_5_4 (现已解散)

You are using O_NDELAY 您正在使用O_NDELAY

O_NONBLOCK or O_NDELAY O_NONBLOCK或O_NDELAY

When possible, the file is opened in non-blocking mode. 如果可能,文件将以非阻塞模式打开。 Neither the open() nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait. 对返回的文件描述符的open()或任何后续操作都不会导致调用进程等待。 For the handling of FIFOs (named pipes), see also fifo(7). 有关FIFO(命名管道)的处理,另请参见fifo(7)。 For a discussion of the effect of O_NONBLOCK in conjunction with mandatory file locks and with file leases, see fcntl(2). 有关O_NONBLOCK结合强制文件锁和文件租约的影响的讨论,请参阅fcntl(2)。

EDIT: You're doing the same thing in your fcntl() call, as well. 编辑:你也在你的fcntl()调用中做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM