我试图用树莓派从arduino串行端口读取串行数据,并以33字节的封装以800 Hz的频率进行读取。我试图计时,但变得非常不稳定。
我用来接收数据的代码如下,基本上,它只是计算“读取”所需的时间。它将被称为800 Hz。
std::chrono::system_clock::time_point startGet= std::chrono::system_clock::now();
int readBytes = read(serialPort, this->serialBuf+alreadyRead, this->dataNeedRead);
std::chrono::system_clock::time_point endGet= std::chrono::system_clock::now();
microsecs_t get_time(std::chrono::duration_cast<microsecs_t>(endGet-startGet));
cout<<"spend "<<get_time.count()<<endl;
由于上面的脚本将以800 Hz的频率调用,因此理想情况下,我应该看到端口在短时间内收到了33个字节,但结果却是:
read: 33
spend 3817
read: 33
spend 5
read: 33
spend 1301
read: 33
spend 40
read: 33
spend 1333
read: 33
spend 31
我可以看到该端口确实读取了33个字节,但是处理时间太长(需要在1250 us内运行800Hz,但其中一些超过3000 us。
我设置串行端口的方法如下
int Sensor::serialPortConnect(char *portName)
{
// Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device)
int serial_port = open(portName, O_RDWR);
// Create new termios struc, we call it 'tty' for convention
struct termios tty;
memset(&tty, 0, sizeof tty);
// Read in existing settings, and handle any error
if (tcgetattr(serial_port, &tty) != 0)
{
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
}
tty.c_cflag &= ~PARENB; // set parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] =33;
// Set in/out baud rate to be 1000000
cfsetispeed(&tty, B1000000);
cfsetospeed(&tty, B1000000);
// Save tty settings, also checking for error
if (tcsetattr(serial_port, TCSANOW, &tty) != 0)
{
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
}
//Allocate buffer for read buffer
//tcflush(serial_port, TCIFLUSH);
return serial_port;
}
我不确定为什么会这样。 RPi随RT-PREEMPT内核一起安装,我正在运行的应用程序具有最高优先级(RT)。我以为它不应该这么不稳定。