上位机会每隔2秒左右发送一个帧, 看帧id, 其实是0x0A, 就是时间, 那么问题来了, 后面8个字节怎么表示的时间呢?
def handleTimeRequest(self, data): """ Respond to device with system time. """ t = Time() t.data = rospy.Time.now() data_buffer = StringIO.StringIO() t.serialize(data_buffer) self.send( TopicInfo.ID_TIME, data_buffer.getvalue() ) self.lastsync = rospy.Time.now()
查Time.h文件, 发现序列化的方法是:
virtual int serialize(unsigned char *outbuffer) const { int offset = 0; *(outbuffer + offset + 0) = (this->data.sec >> (8 * 0)) & 0xFF; *(outbuffer + offset + 1) = (this->data.sec >> (8 * 1)) & 0xFF; *(outbuffer + offset + 2) = (this->data.sec >> (8 * 2)) & 0xFF; *(outbuffer + offset + 3) = (this->data.sec >> (8 * 3)) & 0xFF; offset += sizeof(this->data.sec); *(outbuffer + offset + 0) = (this->data.nsec >> (8 * 0)) & 0xFF; *(outbuffer + offset + 1) = (this->data.nsec >> (8 * 1)) & 0xFF; *(outbuffer + offset + 2) = (this->data.nsec >> (8 * 2)) & 0xFF; *(outbuffer + offset + 3) = (this->data.nsec >> (8 * 3)) & 0xFF; offset += sizeof(this->data.nsec); return offset; }
这样就明了了, 8个bytes,有4个是小数点之前, 有4个是小数点之后的.
所以, "xc8x38x67x59x05xb6x2cx37", 其实是:
0x596738c8 . xxxx, 放计算器里面一算, 就是1499936968.xxxx不重要了吧...
这样看来就没有设么问题了吧, 看来学点python不是没有好处.