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 コマンドを使用し、後で単純な 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 リード コマンドを起動します。「Not Ready」ビットが 0 であることを確認する必要がありますが、シリアル接続で応答がありません。

ここであなたの助けが必要です。誰かが私にヒントをくれたのかもしれません。IMU と、またはシリアル通信を介して Linux と適切に通信するにはどうすればよいですか?

4

1 に答える 1

0

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

バイト配列の長さは 3 バイトだけでよいため、len は 3 にする必要があります (0x3e は IMU が応答する必要があるため、応答を確認する場合を除いて、プログラムに含めるべきではありません)。予想される回答のサイズ (len=4) のみを読み取ります。書いた後に眠る必要はありません。

于 2013-10-01T03:52:13.830 に答える