2016-08-05 6 views
0

私はUbuntu Linuxを実行しているODROID C1のUSB0に入ってくる情報を読んでいます。私はcodereviewに何かを発見し、このような3つの別々のファイルにそれを使用:Cコードを使用してUbuntu LinuxのCOMポートを読む

のmain.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; 
} 

serial_port.h:

#ifndef SERIAL_PORT 
#define SERIAL_PORT 

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

int si_processed; 

#endif 

serial_port.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を使用しますが、次のエラーが発生します:In serial_po rt.c:「宣言されていないFIONREAD(最初の関数で使用) serial_port.cには: 『INPUT_BYTES_NUM』宣言されていない

コードは、私が欲しいものよりも多くなるようですが、私は最も簡単なCのコードが読みたいですデータはUSB0で受信し、ボーイ115200 8N1を表示し、ミニコミと同様に表示します。

答えて

0

は、ヘッダファイルの下

#include <sys/ioctl.h> 
+0

感謝を含め、今私は、次のエラーを取得:メインに:「open_local_socket」に未定義の参照と「process_socket」に未定義の参照を。 serial_port.cで "error_exit"への未定義参照を取得する –

+0

"open_local_socket"関数と "process_socket"関数を作成する –

関連する問題