我需要从通过TCP / IP发送的驾驶模拟器中读取速度值。
模拟器的数据包定义如下:
<PacketDefinition> = ((VariableNames, Datatype, Defaultvalue));
所以我的SendDefinition看起来像这样:
SendDefinition = ((speed, double, 0.0));
驾驶模拟器的文档中告知信息,double的数据长度= 8。
我已经试图读取这样的值:
Socket s = new Socket(server, port);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readDouble());
但这给了我胡说八道的价值。
有人可以帮助我阅读正确的值吗? 附加信息:驾驶模拟器称为“ SILAB”
答案 0 :(得分:0)
也许此C ++代码可以帮助您
Counter
这是我为以下SendDefinition设计的一个例子
void ClusterClientSocket::on_readyRead(){
typedef enum _typeRead{typeRev, typeSpeed, typeSpeedLimit} TypeRead;
static TypeRead nextType = typeRev;
// read recurringly the data from the simulator in the order rev, speed, speed limit
while(m_socket->bytesAvailable() >= sizeof(double)) {
char buffer[sizeof(double)];
qint64 size = m_socket->read(buffer, sizeof(double));
if(size != sizeof(double)) {
break;
}
double value = *(reinterpret_cast<double *>(buffer));
switch(nextType) {
case typeRev:
// qDebug() << "rev: " << value;
emit revReceived(value);
nextType = typeSpeed;
break;
case typeSpeed:
//qDebug() << "speed: " << value;
emit speedReceived(value);
nextType = typeSpeedLimit;
break;
case typeSpeedLimit:
//qDebug() << "speedLimit: " << value;
emit speedLimitReceived(value);
nextType = typeRev;
break;
default:
qDebug() << "unknown value for nextType";
break;
}
}}