我们需要对Universal Robot-UR5e Robot的实时TCP数据进行解码。由于TCP接口(端口30003),该想法是从远程Windows 7计算机控制机械臂。
我们正确地接收到一个1108字节长的帧,具有以下分布(如official UR doc中所述):
我们使用非阻塞标准QTcpSocket。数据到达时,将执行以下插槽:
void Robot::onTcpDataReceived()
{
QByteArray buffer = m_socket->readAll();
m_state->update(buffer);
}
void RobotState::update(const QByteArray data)
{
//Decode length data
int length = 0;
memcpy(&length, data.constData(), 4);
length = qToBigEndian(length);
qDebug() << length; //Print 1108 as expected
//Decode joint 1 current position
QByteArray jointArray = data.mid(252,8).toHex();
long long currentJointPosition1=jointArray.toLongLong(0,16);
double converted = reinterpret_cast<double&>(currentJointPosition1);
qDebug() << converted << " " << data.mid(252,8).toHex().toDouble();
//Expected -3.1585... but received 3.1585
//... decode the rest of the values
}
为解码第一个值(4个字节的整数),我们访问数组并使用memcpy函数。然后我们将其转换为qToBigEndian,结果就可以了。
但是,对于tcp数据的双精度值,我们不能遵循相同的过程(通过使用memcpy,然后转换为big endian,调整var类型)。
相反,我们首先需要隔离接收到的8个字节(jointArray)并将其转换为十六进制。然后,使用.ToLongLong方法并将其强制转换为两倍,但我们丢失了该符号。
我的问题是:
感谢您的帮助!