Linux ttyUSBX下的串行通信

时间:2013-09-30 15:30:33

标签: linux serial-port sensor tty

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

我使用Raspberry Pi并通过USB /串行适配器与Inserial测量单元(多功能传感器)连接。

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

在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设备。

所以我尝试设置Baudrate,Databits,Stopbits,Parity,flowcontrol。 首先通过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正确进行串行通信?

1 个答案:

答案 0 :(得分:0)

  

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

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