2013-09-30 66 views
0

我使用Linux Ubuntu並嘗試使串行通信正常工作。 好吧,我在用什麼...Linux下的串行通信ttyUSBX

我使用Raspberry Pi並通過USB /串行適配器連接一個Inserial Measurement Unit(多功能傳感器)。

只是爲了澄清我想要做的事:

建立betwenn樹莓派和IMU的連接。

要運行IMU,我必須遵循以下步驟。

開機順序:

(a) power-on. 
(b) Wait 800ms. 
(c) Wait until NOT_READY bit goes to 0. NOT_READY is GLOB_CMD[3Eh]'s bit[10]. 
TXdata={0x3E,0x00,0x0d}. /* GLOB_CMD read command */ 
TXdata={0x3E,MSByte,LSByte,0x0d}. /* get response */ 
Confirm NOT_READY bit. 
When NOT_READY becomes 0, it ends. Otherwise , please repeat (c). 
(d) Confirm HARD_ERR bits. HARD_ERR is DIAG_STAT[3Ch]'s bit[6:5]. 
TXdata={0x3C,0x00,0x0d}. /* DIAG_STAT read command */ 
TXdata={0x3C,MSByte,LSByte,0x0d}. /* get response */ 
Confirm HARD_ERR is 00. 
If HARD_ERR is 00, the IMU is OK. Otherwise, the IMU is faulty. 

讀寫寄存器:

[Read Example] 
To read a 16bit-data from a register(addr=0x38). 
TXdata={0x38,0x00,0x0d}. /* command */ 
RXdata={0x38,0x04,0x04,0x0d} /* response */ 
0x04 in 2nd byte of RXdata is Configuration mode. 
0x04 in 3rd byte of RXdata is TAP=16. 
Please note that read data unit is 16bit, and Most Significant Byte first. 
------------------------------------------------------------- 
[Write Example] 
To write a 8bit-data into a register(addr=0x39). 
TXdata={0xB9,0x01,0x0d}. /* command */ 
RXdata= w/o response 
By sending this command, the IMU moves to Sampling mode. 
Please note that write data unit is 8bit. 

在我的Linux操作系統Ubuntu有連接IMU後給予ttyUSB0設備。

所以我試圖設置波特率,數據位,停止位,奇偶校驗,流量控制。 首先通過stty-command,稍後用一個簡單的C++代碼。

我使用這個C++ - 代碼:

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

void SleepMs(int); 

int main(int argc, char** argv) 
{ 
    int fd; // port file descriptor 
    char port[20] = "/dev/ttyUSB0"; // port to connect to 
    fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY); // connect to port 
    if(fd == -1) 
    { 
     printf("Error while opening the port.\n"); 
     return 1; 
    } 
    printf("Port opened successfully.\n"); 

    fcntl(fd, F_SETOWN, getpid()); 
    struct termios settings; 
    tcgetattr(fd, &settings); 
    settings.c_cflag &= ~(CBAUD | CSIZE | CREAD); 
    settings.c_cflag |= B230400; 
    settings.c_cflag |= CS8; 


    tcflush(fd, TCIFLUSH); 
    tcsetattr(fd, TCSANOW, &settings); // apply the settings 

    int len = 7; 
    unsigned char bytes[len]; 
    bytes[0] = 0x3E; 
    bytes[1] = 0x00; 
    bytes[2] = 0x0D; 
    bytes[3] = 0x3E; 
    bytes[4] = 0x00; 
    bytes[5] = 0x00; 
    bytes[6] = 0x0D; 

    int wr = write(fd, bytes, len); 
    unsigned char answer[32]; 
    SleepMs(350); 
    int rd = -1; 
    int i; 

    while (rd==-1) 
    { 

    if(wr != 7) 
    { 
     printf("Error while sending!\n"); 
    } 


    for(i=0; i<len; i++) 
    { 
     printf("%X sent\n", (unsigned int)bytes[i]); 
      SleepMs(350); 
    } 
    printf("\n"); 
    printf("%d bytes sent.\n", wr); 
    printf("\n"); 
    printf("Trying to read...\n"); 
    printf("\n"); 
    rd = read(fd, answer, 32); 
    SleepMs(350); 

     printf("%d\n", rd); 

    for(i=0; i<rd; i++) 
    { 
     printf("%X ", (unsigned int)answer[i]); 
    } 
    printf("\n\n"); 

    } 

    close(fd); 
    return 0; 
} 

void SleepMs(int ms) { 
usleep(ms*1000); //convert to microseconds 
return; 
} 

如果我啓動程序,它告訴我「端口打開成功」和寫入給出的字節數。 但它沒有收到任何數據。

我發送0x3E 0x00 0x0D來激活GLOB_CMD讀命令。 我必須確認「未就緒」 - 位是0,但我沒有得到與我的串行連接的答案。

所以這是我需要你的幫助,也許有人得到了我的提示。 如何使用Linux與我的IMU或串行通信進行通信?

回答

0

int wr = write(fd,bytes,len);

你的字節數組僅需要3個字節,所以LEN應爲3(該0x3E的是什麼IMU應該做出反應,所以它不應該是在你的程序檢查響應時除外)。當你閱讀時,你只應該閱讀答案的預期大小(len = 4)。在寫作後,你不需要睡,而且在閱讀後可能不會。