0

我正在使用 Linux Ubuntu 并尝试让串行通信正常工作。好吧,我用什么...

我使用 Raspberry Pi 并通过 USB/Serial-Adapter 将其与 Inserial Measurement Unit(多功能传感器)连接起来。

只是为了澄清我正在尝试做的事情:

在 Raspberry Pi 和 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,但我的串行连接没有得到答案。

所以这就是我需要你帮助的地方,也许有人给我提示。如何与我的 IMU 通信或通过串行通信与 Linux 正确通信?

4

1 回答 1

0

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

你的 bytes 数组只需要 3 个字节长,所以 len 应该是 3。(0x3e 是 IMU 应该响应的,所以它不应该在你的程序中,除非在检查响应时。)当你阅读时,你应该只读取答案的预期大小(len=4)。写完不需要睡觉,读完可能也不需要。

于 2013-10-01T03:52:13.830 回答