我有一个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)