2015-03-25 39 views
0

我正在製作一個簡單的C程序,以從串行設備串行獲取數據。數據是十六進制格式。在編寫代碼之前,我在cutecom中檢查過它,我正在收到 25 54 00 1e,這是正確和準確的值。但是,當我寫代碼,然後我收到這是錯誤的數據這個BFE50A14。我不知道我犯了什麼錯誤,請幫忙。謝謝。!在串行通信中獲取錯誤數據

CODE:

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <termios.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <string.h> 
#include <math.h> 

#define portname "/dev/ttyUSB0" 

int set_interface_attribs (int fd, int speed, int parity) 
{ 
     struct termios tty; 
     memset (&tty, 0, sizeof tty); 
     if (tcgetattr (fd, &tty) != 0) 
     { 
       // error_message ("error %d from tcgetattr", errno); 
       printf("error opening the device"); 
       return -1; 
     } 

     cfsetospeed (&tty, speed); 
     cfsetispeed (&tty, speed); 

     tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;  // 8-bit chars 
     // disable IGNBRK for mismatched speed tests; otherwise receive break 
     // as \000 chars 
     tty.c_iflag &= ~IGNBRK;   // disable break processing 
     tty.c_lflag = 0;    // no signaling chars, no echo, 
            // no canonical processing 
     tty.c_oflag = 0;    // no remapping, no delays 
     tty.c_cc[VMIN] = 0;   // read doesn't block 
     tty.c_cc[VTIME] = 5;   // 0.5 seconds read timeout 

     tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl 

     tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, 
            // enable reading 
     tty.c_cflag &= ~(PARENB | PARODD);  // shut off parity 
     tty.c_cflag |= parity; 
     tty.c_cflag &= ~CSTOPB; 
     tty.c_cflag &= ~CRTSCTS; 

     if (tcsetattr (fd, TCSANOW, &tty) != 0) 
     { 
      // error_message ("error %d from tcsetattr", errno); 
      printf("error opening the device"); 
      return -1; 
     } 
     return 0; 
} 

int set_blocking (int fd, int should_block) 
{ 
     struct termios tty; 
     memset (&tty, 0, sizeof tty); 
     if (tcgetattr (fd, &tty) != 0) 
     { 
      //error_message ("error %d from tggetattr", errno); 
      printf("error opening the device"); 
      return; 
     } 

     tty.c_cc[VMIN] = should_block ? 1 : 0; 
     tty.c_cc[VTIME] = 5;   // 0.5 seconds read timeout 

     if (tcsetattr (fd, TCSANOW, &tty) != 0) 
      // error_message ("error %d setting term attributes", errno); 
      printf("error opening the device"); 
} 


int main() 
{ 


    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); 
    if (fd < 0) 
    { 

      printf("error opening the device"); 

    } 
/*CHANGES*/ 
if(!set_interface_attribs(fd, B9600, 0)) 
     { 
      printf("error set interface"); 
     } 
     else 
      printf("correct"); 
     if(!set_blocking(fd, 0)) 
     { 
      printf("error set blocking"); 
     } 
     else 
      printf("done"); 
*/ 
    set_interface_attribs (fd, B9600, 0); 


    set_blocking (fd, 0);    // set no blocking 

    usleep ((7 + 25) * 100);    

    int receivebuffer [10]; 

    read (fd, receivebuffer, sizeof receivebuffer); 
/***CHANGES***// 
    printf("value of buffer is %2X %2X %2X %2X \n\n", receivebuffer[0],receivebuffer[1],receivebuffer[2],receivebuffer[3]); 



return 0; 
} 

我接收receivebuffer數據,我使用printf和使用%X打印它在十六進制格式它打印。我得到的輸出是BFE50A14,但正確的輸出是25 54 00 1e。請幫助,謝謝。

+0

串行端口的原始值應保存(在例如一個全局變量),這樣他們就可以恢復程序退出 – user3629249 2015-03-25 06:26:23

+0

之前set_interface-atribs()和set_blocking()應該返回一個狀態。 Main()應該檢查每個調用的狀態以確定它是否繼續,而不是假設屬性修改都是成功的 – user3629249 2015-03-25 06:34:25

+0

應該通過int receivebuffer [10] = {'0'}清零數組receivebuffer []。然而,字符正在讀取,而不是整數,所以它應該是char receivebuffer [10] = {'\ 0'}; – user3629249 2015-03-25 06:38:30

回答

0

這裏就是我們在評論到目前爲止覆蓋:

Note: this only vaguely resembles the code most recently posted 

Note: do not use tabs in your code. because 
     different editors have the tab stops and/or tab width 
     set differently 

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <termios.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <string.h> 
#include <math.h> 

#define portname "/dev/ttyUSB0" 

static struct termios oldtty; 

int set_interface_attribs (int fd, int speed, int parity) 
{ 
     struct termios tty; 
     memset (&tty, 0, sizeof tty); 
     if (tcgetattr (fd, &oldtty) != 0) 
     { 
       // error_message ("error %d from tcgetattr", errno); 
       printf("error opening the device"); 
       return -1; 
     } 

     memcpy(tty, oldtty, sizeof struct termion); 

     cfsetospeed (&tty, speed); 
     cfsetispeed (&tty, speed); 

     tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;  // 8-bit chars 
     // disable IGNBRK for mismatched speed tests; otherwise receive break 
     // as \000 chars 
     tty.c_iflag &= ~IGNBRK;   // disable break processing 
     tty.c_lflag = 0;    // no signaling chars, no echo, 
            // no canonical processing 
     tty.c_oflag = 0;    // no remapping, no delays 
     tty.c_cc[VMIN] = 0;   // read doesn't block 
     tty.c_cc[VTIME] = 5;   // 0.5 seconds read timeout 

     tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl 

     tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, 
            // enable reading 
     tty.c_cflag &= ~(PARENB | PARODD);  // shut off parity 
     tty.c_cflag |= parity; 
     tty.c_cflag &= ~CSTOPB; 
     tty.c_cflag &= ~CRTSCTS; 

     if (tcsetattr (fd, TCSANOW, &tty) != 0) 
     { 
      // error_message ("error %d from tcsetattr", errno); 
      printf("error opening the device"); 
      return -1; 
     } 
     return 0; 
} // end function: set_interface_attribs 


int set_blocking (int fd, int should_block) 
{ 
     struct termios tty; 
     memset (&tty, 0, sizeof tty); 
     if (tcgetattr (fd, &tty) != 0) 
     { 
      //error_message ("error %d from tggetattr", errno); 
      printf("error opening the device"); 
      return -1; 
     } 

     tty.c_cc[VMIN] = should_block ? 1 : 0; 
     tty.c_cc[VTIME] = 5;   // 0.5 seconds read timeout 

     if (tcsetattr (fd, TCSANOW, &tty) != 0) 
      // error_message ("error %d setting term attributes", errno); 
      printf("error opening the device"); 
    return 0; 
} // end function: set_blocking 


int main() 
{ 
    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); 
    if (fd < 0) 
    { 
     printf("error opening the device"); 
    } 


    /*CHANGES*/ 
    if(!set_interface_attribs(fd, B9600, 0)) 
    { 
     printf("error set interface"); 
    } 

    else 
     printf("correct"); 

    if(!set_blocking(fd, 0)) 
    { 
     printf("error set blocking"); 
    } 

    else 
     printf("done"); 

    // what is this stray end of comment? 
    // suggest enabling all the compiler warings 
    // and fixing the warnings 
    // */ 
    if(set_interface_attribs (fd, B9600, 0)) 
    { // then set_interface_attribs failed 
     return -1; 
    } 

    // implied else set_interface_attribs successful 

    if(set_blocking (fd, 0))    // set no blocking 
    { // then set_blocking failed 
     return -1; // might need to also restore oldtty attributes 
    } 

    // implied else, set_blocking successful 

    usleep ((7 + 25) * 100);    

    char receivebuffer [20]; 

    if(4 > read (fd, receivebuffer, sizeof receivebuffer)) 
    { // then read failed 
     return -1; 
    } 

    // implied else, read successful 

    printf("value of buffer is %2X %2X %2X %2X \n\n", 
     receivebuffer[0], 
     receivebuffer[1], 
     receivebuffer[2], 
     receivebuffer[3]); 

    // cleanup 
    tcsetattr (fd, TCSANOW, &oldtty); 
    return 0; 
} // end function: main 
+0

感謝你們。它給錯誤:'memcpy'的參數1的不兼容類型和set_interface函數的memcpy行'struct'之前的預期表達式 – user46573544 2015-03-25 07:43:59

+0

嘗試使用'memcpy(&tty,&oldtty,sizeof struct termios);' – 2015-03-25 07:52:37