我正在开发一个在Raspberry Pi上运行的程序,并使用Pi的串口与Arduino进行交互。
我的架构是这样的: Pi连接到PanStamp(Arduino +无线收发器),并且有一个Satellite(带有少量传感器的PanStamp)无线发送数据。 Pi可以激活和停用卫星。卫星每秒发送大约5次数据,连接到Pi的PanStamp接收此数据并使用串行端口发送。每秒Pi的PanStamp也会发送一些传感器的读数。
为此,我使用Qt框架,qextserialport(实现Qt信号/插槽的串口库)wiriginpi来控制GPIO和一些TCP和UDP套接字通过互联网发送一些数据。
当卫星发送数据时,一切看起来都很好。
然而,当我关闭卫星时,整个程序变得非常慢。它只显示大约每两秒钟的数据......正常操作应该每秒接收一次读数(PanStamp正在通过正确的调整发送数据,我用示波器检查了它。)
我有一个与数据一起发送的数据包计数,并且没有数据包丢失。所以它看起来像是一种缓冲或类似的东西压低了数据流。
最奇怪的是,当我再次打开卫星时(“高速”数据流),Pi显示“数据突发”,就像所有内容都被缓冲并缓慢处理一样。 brust的大小也看起来与卫星关闭的时间成正比。
有没有人知道发生了什么/如何测试?
一些代码:
串口初始化:
/* Initialize Serial Port to talk with PanStamp */
this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
if (port->open(QIODevice::ReadWrite) == true)
{
/* Connect the arriving data signal with the incoming data function */
connect(port, SIGNAL(readyRead()), this, SLOT(incoming_data()));
/* Connect state change signal with its function */
connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
/* Debug */
qDebug() << "listening for data on" << port->portName();
}
else
{
qDebug() << "device failed to open:" << port->errorString();
exit(1);
}
函数incoming_data()(每次有要读取的数据时调用):
/**
* @brief SerialHandler::incoming_data
*
* It is called every time that a byte is received.
*
* It is responable to syncronize with the wireless link
* and call store_incoming_data with the right data.
*
* To syncronize it waits for two 0xBA, when it happens
* get_data is set to true. The next byte is the data size.
*
* The function waits to all data arrive, read it and pass it
* to store_incoming_data that parses it and sore on memory/disk.
*
*/
void SerialHandler::incoming_data()
{
QByteArray bytes;
unsigned char b;
int i;
/* Wait for sync condition */
if(!get_data)
{
debug_msg(".", port->bytesAvailable());
fflush(stderr);
if(last_byte == 0xBA)
{
bytes = port->read(1);
b = (unsigned char) bytes.at(0);
if(b == 0xBA)
{
get_data = true;
return;
}
else
last_byte = b;
}
else
{
bytes = port->read(1);
b = bytes.at(0);
fflush(stdout);
last_byte = b;
}
}
else
{
debug_msg(",");
/* Read the data size */
if(port->bytesAvailable() && (data_size < 0))
{
bytes = port->read(1);
data_size = (int) bytes.at(0);
debug_msg("Size: %i -> %02X\n", data_size, (unsigned char) data_size);
}
fflush(stderr);
/* Read data and call store_incoming_data */
if(port->bytesAvailable() >= data_size)
{
bytes = port->read(data_size);
if(data_size == 0x34)
store_incoming_stellite_data(bytes);
else
{
if(bytes.at(0) == HUB_DATA)
store_incoming_hub_data(bytes);
else
{
printf("data_size: %i\tDebug panStamp data: ", data_size);
for(i = 0; i < bytes.size(); i++)
printf("[%02X]", (unsigned char) bytes.at(i));
printf("\n");
}
fflush(stdout);
}
get_data = false;
last_byte = 0;
data_size = -1;
fflush(stderr);
}
}
}
以下是其他功能和数据结构:
/* ========================================== Data Types ======================================= */
typedef unsigned char byte;
typedef union _float_byte
{
unsigned char asByte[4];
float asFloat;
} float_byte;
typedef union _int_byte
{
unsigned char asByte[2];
short int asInt;
} int_byte;
typedef union _uint_byte
{
unsigned char asByte[4];
unsigned int asUInt;
} uint_byte;
typedef struct _stellite_data
{
QTime piTime;
float_byte x0, y0, z0;
float_byte x1, y1, z1;
float_byte temp0, temp1;
float_byte pressure0;
float_byte pressure1;
float_byte pressure2;
uint_byte time_stamp;
int_byte packet_count;
QByteArray bytes;
byte sender;
byte status_byte;
} satellite_data;
typedef struct _hub_data
{
QTime piTime;
float_byte altitude;
byte status_byte;
QByteArray bytes;
uint_byte packet_count;
byte power_sat0;
byte power_sat1;
byte hack_hd0;
byte hack_hd1;
byte solenoid0;
byte solenoid1;
byte webcam;
} hub_data;
/* ========================================== END Data Types ======================================= */
/**
* @brief SerialHandler::store_incoming_hub_data
*
* Store Hub's data.
*
* Packet description:
* +---------------------+-----------------+
* | Altitude_Command | Altitude (float)|
* | (1 byte) | (4 byts) |
* +---------------------+-----------------+
*
* @param data Data to be stored
*/
void SerialHandler::store_incoming_hub_data(QByteArray data)
{
hub_data *tmp = new hub_data;
char *datagram;
int datagram_size;
tmp->piTime = QTime::currentTime();
tmp->altitude.asByte[0] = data.at(1);
tmp->altitude.asByte[1] = data.at(2);
tmp->altitude.asByte[2] = data.at(3);
tmp->altitude.asByte[3] = data.at(4);
tmp->packet_count.asByte[0] = data.at(5);
tmp->packet_count.asByte[1] = data.at(6);
tmp->packet_count.asByte[2] = data.at(7);
tmp->packet_count.asByte[3] = data.at(8);
tmp->power_sat0 = GS_link->power_sat0;
tmp->power_sat1 = GS_link->power_sat1;
tmp->hack_hd0 = GS_link->hack_hd0;
tmp->hack_hd1 = GS_link->hack_hd1;
tmp->solenoid0 = GS_link->solenoid0;
tmp->solenoid1 = GS_link->solenoid1;
tmp->webcam = GS_link->webcam;
printf("HUB: %s:%i\t%u\t%.4f\t%i\t%i\t%i\t%i\t%i\t%i\t%i\n",
tmp->piTime.toString().toStdString().c_str(),
tmp->piTime.msec(),
tmp->packet_count.asUInt,
tmp->altitude.asFloat,
tmp->power_sat0,
tmp->power_sat1,
tmp->hack_hd0,
tmp->hack_hd1,
tmp->solenoid0,
tmp->solenoid1,
tmp->webcam);
fflush(stdout);
/* Send to GS */
datagram_size = asprintf(&datagram, "H,ALT,%.4f,S0,%i,S1,%i,HD0,%i,HD1,%i,SOL0,%i,SOL1,%i,W,%i",
tmp->altitude.asFloat,
tmp->power_sat0,
tmp->power_sat1,
tmp->hack_hd0,
tmp->hack_hd1,
tmp->solenoid0,
tmp->solenoid1,
tmp->webcam);
GS_link->send_datagram(datagram_size, datagram);
hub_data_list.append(tmp);
if((hub_data_list.size()%60) == 0)
log_hub_data("iSEDE_Hub.log");
free(datagram);
}
void SerialHandler::log_hub_data(QString file_name)
{
int i;
QFile file(file_name);
file.open(QIODevice::Append);
QTextStream file_out(&file);
printf("Logging Hub's data...\n");
for(i = last_hub_logged; i < hub_data_list.size(); i++)
{
file_out << hub_data_list.at(i)->piTime.toString(Qt::ISODate).toStdString().c_str() << ":";
file_out << hub_data_list.at(i)->piTime.msec() << ';';
file_out << hub_data_list.at(i)->altitude.asFloat << ';';
file_out << hub_data_list.at(i)->power_sat0 << ';';
file_out << hub_data_list.at(i)->power_sat1 << ';';
file_out << hub_data_list.at(i)->hack_hd0 << ';';
file_out << hub_data_list.at(i)->hack_hd1 << ';';
file_out << hub_data_list.at(i)->solenoid0 << ';';
file_out << hub_data_list.at(i)->solenoid1 << ';';
file_out << hub_data_list.at(i)->webcam << '\n';
}
last_hub_logged = i;
file.close();
printf("Done!\n");
}
/**
* @brief SerialHandler::store_incoming_data
* @param data
*
* Parse the data.
*
* Payload description:
* +---------------+------------------+---------------------+---------------+--------------+-------------+------------------+
* | Packet Count | Temperature (x2) | Accelerometers (x2) | Pressure (x3) | Time Stamp | Status byte | Sender's Address |
* | (2 bytes) | (8 bytes) | (24 bytes) | (12 bytes) | (4 bytes) | (1 byte) | (1 byte) |
* +---------------+------------------+---------------------+---------------+--------------+-------------+------------------+
*/
void SerialHandler::store_incoming_stellite_data(QByteArray data)
{
satellite_data *tmp = new satellite_data;
tmp->bytes.append(data);
/* Pi's time */
tmp->piTime = QTime::currentTime();
/* Packet count */
tmp->packet_count.asByte[0] = data.at(0);
tmp->packet_count.asByte[1] = data.at(1);
/* Temperature 0 */
tmp->temp0.asByte[0] = data.at(2);
tmp->temp0.asByte[1] = data.at(3);
tmp->temp0.asByte[2] = data.at(4);
tmp->temp0.asByte[3] = data.at(5);
/* Temperature 1 */
tmp->temp1.asByte[0] = data.at(6);
tmp->temp1.asByte[1] = data.at(7);
tmp->temp1.asByte[2] = data.at(8);
tmp->temp1.asByte[3] = data.at(9);
/* Accelerometer 0 */
tmp->x0.asByte[0] = data.at(10);
tmp->x0.asByte[1] = data.at(11);
tmp->x0.asByte[2] = data.at(12);
tmp->x0.asByte[3] = data.at(13);
tmp->y0.asByte[0] = data.at(14);
tmp->y0.asByte[1] = data.at(15);
tmp->y0.asByte[2] = data.at(16);
tmp->y0.asByte[3] = data.at(17);
tmp->z0.asByte[0] = data.at(18);
tmp->z0.asByte[1] = data.at(19);
tmp->z0.asByte[2] = data.at(20);
tmp->z0.asByte[3] = data.at(21);
/* Accelerometer 1 */
tmp->x1.asByte[0] = data.at(22);
tmp->x1.asByte[1] = data.at(23);
tmp->x1.asByte[2] = data.at(24);
tmp->x1.asByte[3] = data.at(25);
tmp->y1.asByte[0] = data.at(26);
tmp->y1.asByte[1] = data.at(27);
tmp->y1.asByte[2] = data.at(28);
tmp->y1.asByte[3] = data.at(29);
tmp->z1.asByte[0] = data.at(30);
tmp->z1.asByte[1] = data.at(31);
tmp->z1.asByte[2] = data.at(32);
tmp->z1.asByte[3] = data.at(33);
/* Pressure 0 */
tmp->pressure0.asByte[0] = data.at(34);
tmp->pressure0.asByte[1] = data.at(35);
tmp->pressure0.asByte[2] = data.at(36);
tmp->pressure0.asByte[3] = data.at(37);
/* Pressure 1 */
tmp->pressure1.asByte[0] = data.at(38);
tmp->pressure1.asByte[1] = data.at(39);
tmp->pressure1.asByte[2] = data.at(40);
tmp->pressure1.asByte[3] = data.at(41);
/* Pressure 2 */
tmp->pressure2.asByte[0] = data.at(42);
tmp->pressure2.asByte[1] = data.at(43);
tmp->pressure2.asByte[2] = data.at(44);
tmp->pressure2.asByte[3] = data.at(45);
/* Satellite time stamp */
tmp->time_stamp.asByte[0] = data.at(46);
tmp->time_stamp.asByte[1] = data.at(47);
tmp->time_stamp.asByte[2] = data.at(48);
tmp->time_stamp.asByte[3] = data.at(49);
/* Satellite status byte */
tmp->status_byte = data.at(50);
/* Sender's address */
tmp->sender = data.at(51);
printf("%u\t%u\t%u\t%hi\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%s\t%s:%03i\n",
tmp->sender,
tmp->time_stamp.asUInt,
tmp->time_stamp.asUInt/1000,
tmp->packet_count.asInt,
tmp->temp0.asFloat,
tmp->temp1.asFloat,
tmp->x0.asFloat,
tmp->y0.asFloat,
tmp->z0.asFloat,
tmp->x1.asFloat,
tmp->y1.asFloat,
tmp->z1.asFloat,
tmp->pressure0.asFloat,
tmp->pressure1.asFloat,
tmp->pressure2.asFloat,
byte_to_binary(tmp->status_byte),
tmp->piTime.toString(Qt::ISODate).toStdString().c_str(),
tmp->piTime.msec());
fflush(stdout);
/* Create a datagram and send it */
char *datagram = NULL;
int datagram_size;
datagram_size = asprintf(&datagram, "S%u,C,%u,PC,%u,A0,%.4f,%.4f,%.4f,A1,%.4f,%.4f,%.4f,T0,%.4f,T1,%.4f,P0,%.4f,P1,%.4f,P2,%.4f,SB,%u\n",
tmp->sender - 7,
tmp->time_stamp.asUInt,
tmp->packet_count.asInt,
tmp->x0.asFloat,
tmp->y0.asFloat,
tmp->z0.asFloat,
tmp->x1.asFloat,
tmp->y1.asFloat,
tmp->z1.asFloat,
tmp->temp0.asFloat,
tmp->temp1.asFloat,
tmp->pressure0.asFloat,
tmp->pressure1.asFloat,
tmp->pressure2.asFloat,
tmp->status_byte);
GS_link->send_datagram(datagram_size, datagram);
free(datagram);
data_list.append(tmp);
debug_msg("**%i**\n", data_list.size());
if(data_list.size()%100 == 0)
{
log_data("iSEDE.log");
}
/**
* @brief SerialHandler::log_data
* @param file_name
*/
void SerialHandler::log_data(QString file_name)
{
int i;
QFile file(file_name);
file.open(QIODevice::Append);
QTextStream file_out(&file);
printf("Logging data....\n");
for(i = last_logged; i < data_list.size(); i++)
{
file_out << data_list.at(i)->piTime.toString(Qt::ISODate).toStdString().c_str() << ":";
file_out << data_list.at(i)->piTime.msec() << ";";
file_out << data_list.at(i)->sender << ",";
file_out << data_list.at(i)->time_stamp.asUInt << ",";
file_out << data_list.at(i)->packet_count.asInt << ",";
file_out << data_list.at(i)->temp0.asFloat << ",";
file_out << data_list.at(i)->temp1.asFloat << ",";
file_out << data_list.at(i)->x0.asFloat << ",";
file_out << data_list.at(i)->y0.asFloat << ",";
file_out << data_list.at(i)->z0.asFloat << ",";
file_out << data_list.at(i)->x1.asFloat << ",";
file_out << data_list.at(i)->y1.asFloat << ",";
file_out << data_list.at(i)->z1.asFloat << ",";
file_out << data_list.at(i)->pressure0.asFloat << ",";
file_out << byte_to_binary(data_list.at(i)->status_byte);
file_out << "\n";
}
last_logged = i;
file.close();
printf("Done!\n");
}
经过大量的测试和研究后,我终于找到了正在发生的事情:
每次有新数据时都会发出readyRead()
信号,但正如文档中所说“readyRead()不是递归发送的”,所以它看起来像incoming_data()
连接到readyRead()
仅在某些字节到达时调用,使我的程序“不同步”与数据包只能读取一些数据包(其他数据包保留在缓冲区)
尽管如此,当有大量数据到达时,“不同步条件”不会发生。
我创建的线程的Here is我将创建另一个解决此特定问题的方法。
非常感谢帮助我的每个人!
答案 0 :(得分:1)
如果第一次阻止所有信号发出readyRead
,则直到您读取所有数据为止。
另外你应该将serialHandler写成另一个线程,因此serial不会阻止其他操作
P.S
我记得qextserialport
是实验性的...所以最好的方法是使用系统api编写类。