0

我想在运行 Ubuntu Linux 的 ODROID C1 上阅读 USB0 上的信息。我在codereview上找到了一些东西,并在 3 个单独的文件中使用了它,如下所示:

主要.C:

#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 */
#include "serial_port.h"

int main()
{
  int          local_socket;
  int            serial_fd;
  int            max_fd;
  fd_set         input;
  fd_set         tmp_input;
  char *serial_output_buffer;
  serial_output_buffer=malloc(11 * sizeof(char));

  serial_fd=open_port();
  local_socket=open_local_socket();

  FD_ZERO(&input);
  FD_SET(serial_fd, &input);
  FD_SET(local_socket, &input);
  max_fd = (local_socket > serial_fd ? local_socket : serial_fd) + 1;

   si_processed=0;
 serial_output_buffer[10]='\0';
while(1) {
   tmp_input=input;

n = select(max_fd,&tmp_input,NULL,NULL,NULL);

/* See if there was an error */
if (n<0)
    perror("select failed");
else {
    /* We have input */
    if (FD_ISSET(serial_fd, &input)) {
        if(process_serial(serial_fd,serial_output_buffer)) {
            printf("read:%s\n",serial_output_buffer);
            fflush(stdout);
        }
    }
    if (FD_ISSET(local_socket, &input))
        process_socket(local_socket);
}
usleep(20000);
}
return 0;
}

串行端口.h:

#ifndef SERIAL_PORT
#define SERIAL_PORT

int open_port();
int process_serial(int serial_fd,char *output);

int si_processed;

#endif

串行端口.c:

#include "serial_port.h"

char serial_buffer[256];


int process_serial(int serial_fd,char *output) {
   int bytes;
   int n,i;
   char tmp_buffer[256];

   ioctl(serial_fd, FIONREAD, &bytes);
   if(!bytes && si_processed<INPUT_BYTES_NUM) //proceed if data still in buffer
       return 0;

   n=read(serial_fd, tmp_buffer, sizeof(tmp_buffer));
   for(i=0;i<n;i++) {
      serial_buffer[si_processed+i]=tmp_buffer[i];
   }
   si_processed+=n;
   if(si_processed>=INPUT_BYTES_NUM) {
      for (i = 0; i < si_processed; ++i)
          if (serial_buffer[i] == '1') // found start of packet
               break;
      if (i > 0) {
        // start of packet is not start of buffer
        // so discard bad bytes at the start of the buffer

        memmove(serial_buffer, serial_buffer+i, si_processed - i);
        si_processed -= i;
      }
      if(si_processed>=INPUT_BYTES_NUM) {
        memmove(output, serial_buffer, 10);

        //move what left to the beginning
        memmove(serial_buffer,serial_buffer+10,si_processed-10);
        si_processed -= 10;
        return 1;
      }
   }
return 0;
}

int open_port(void) {
  int fd; /* File descriptor for the port */
  struct termios options;

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

  if (fd == -1) {
  /*
  * Could not open the port.
  */
      error_exit("open_port: Unable to open /dev/ttyUSB0");
  }
  else
     fcntl(fd, F_SETFL, 0);

  /*
  * Get the current options for the port...
 */

  tcgetattr(fd, &options);

  /*
  * Set the baud rates to 19200...
   */

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

  /*
  * Enable the receiver and set local mode...
  */

  options.c_cflag |= (CLOCAL | CREAD);

  //set 8N1
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;

  /*
  * Set the new options for the port...
   */

  tcsetattr(fd, TCSANOW, &options);


  return (fd);
}

我使用命令gcc -o app main.c serial_port.c,但出现以下错误: 在 serial_port.c 中:'FIONREAD undeclared(first use in function) In serial_port.c:'INPUT_BYTES_NUM' undeclared

代码似乎比我想要的要多得多,我只想要最简单的 C 代码来读取来自 USB0 、波特 115200 8N1 的数据,并像 minicom 一样显示它。

4

1 回答 1

0

包括下面的头文件,

#include <sys/ioctl.h>
于 2016-08-05T10:59:16.890 回答