1

我有一个 IMU 传感器,我正在尝试通过 USB 端口读取它的数据。我正在使用 ubuntu 11.10 和 Eclipse IDE。使用的语言是 C。为此,我必须向 IMU 发送特定的命令代码并读取结果。我已经这样做了,但我的问题是,当我想在无限循环中从 IMU 读取数据(读取数据任意多次)时,写入函数(我用来向 IMU 发送命令)冻结。即程序执行在写函数中停止。

这是代码:

int comport = -1; // Handle COM port
struct termios options;

comport = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);

fcntl(comport, F_SETFL, 0);

//Get the current options for the port...
tcgetattr(comport, &options);

cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);

options.c_cflag |= CS8;
options.c_oflag &= ~OPOST; //raw output

//Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

//Set the new options for the port...
tcsetattr(comport, TCSANOW, &options);

//read compass 0x23
char command[] = {0xF7, 0x23, 0x23};

while(1)
{
bytes_written = write(comport, command, 3);

if(bytes_written == -1)
{
        printf("\nsending command to IMU failed!");
        //...
}
else if(bytes_written == 3)
{
  //sending command OK

  bytes_read = read(comport,  // Handle
                    INBUFFER, // Incoming data
                12        // Number of bytes to read
                );
}
} //end while(1)
4

0 回答 0