Boost logo

Boost Users :

Subject: [Boost-users] read floats from serial (polhemus fastrak)
From: Jubei (nkatzakis_at_[hidden])
Date: 2010-06-16 11:38:19


Hello. I have been trying to read some floats off a polhemus tracker for a
good week now. I'm just about ready to give up.
I would really really appreciate any help in solving this as it has consumed
an entire week of my life :)

An example of the floats I'm trying to read are displayed in this image.
It's the data that I see when I open a terminal to it.
http://www.rci.kutc.kansai-u.ac.jp/problem.png

Based on an example i found on the net I put together a class as such:

*class minicom_client *
*{ *
*public: *
* minicom_client(boost::asio::io_service& io_service, unsigned int
baud, const std::string& device, EventQueue* queue) *
* : serialPort(io_service, device), eventQueue(queue) *
* { *
* **if (!serialPort.is_open()) *
* { *
* MessageBox(NULL,"Serial Port","Failed to
open",MB_OK);*
* return; *
* } *
* boost::asio::serial_port_base::baud_rate
baud_option(baud); *
* serialPort.set_option(baud_option);*
* *
* **std::string msg = "fC"; **//command to get the tracker to start
sending us binary output continuously*
*
*
* **boost::asio::write(serialPort,boost::asio::buffer(msg.c_str(),msg.size()));
 *//send the command
* *
* ** *//start reading for input
* **serialPort.async_read_some(boost::asio::buffer(buffer, max_read_length),
boost::bind(&minicom_client::read_complete, *
* this, *
* boost::asio::placeholders::error, *
*
 boost::asio::placeholders::bytes_transferred)); *
* *
* } *
* *
*private:*
* **void read_complete(const boost::system::error_code& error, size_t
bytes_transferred) *
* **{** *
* **boost::array<float, 7> topush;*
* *
* **for (int i=0;i<7;i++) {*
* **topush[i] = reverse(getFloat(i)); //already tried with ntohl()*
* *
* **}** *
* *
* *
* **boost::mutex::scoped_lock lock(io_mutex);*
* *//event queue is a <dequeue> that we push the data in (the ui thread
reads the data off that queue)
* **eventQueue->push(topush); *
* **lock.unlock();*
* *
* //go back to waiting for data*
* **serialPort.async_read_some(boost::asio::buffer(buffer, max_read_length),
boost::bind(&minicom_client::read_complete, *
* **this, *
* **boost::asio::placeholders::error, *
* **boost::asio::placeholders::bytes_transferred)); *
* **}*
*
*
* **float getFloat(int index) **//extract floats from the buffer by
memcpying them to floats. Function tested and works fine*
* **{*
* **float temp = 0.0;** *
*
*
* **memcpy(&temp,buffer+3*sizeof(char)+index*sizeof(float),sizeof(float));*
* **return temp;*
*
*
* **}*
*
*
* **float reverse( float const& x ) ** **//reverse the bytes*
* **{*
* **float res;*
*
*
* **char* pres = (char*)(&res);*
* **char const* px = (char const*)(&x);*
*
*
* **std::reverse_copy(px,px+sizeof(float),pres);*
*
*
* **return res;*
* **}*
* *
* **// maximum amount of data to read in one operation 2 ascii, 1 space and
7 4-byte floats *
* **static const int max_read_length = 7*4+3;*
* *
* **boost::asio::serial_port serialPort; **// the serial port this instance
is connected to** *
* **unsigned char buffer[max_read_length]; **// data read from the socket *
* **EventQueue* eventQueue;*
*};*

then on my main() i do:

*
*
*
*
*boost::asio::io_service io_service;*
*boost::asio::io_service::work work(io_service);*
*try {*
* **minicom_client c(io_service, 115200, "COM4",queuePointer); *
* **io_service.run();*
*}*
*catch (std::exception& e)*
*{*
* **MessageBox(NULL,e.what(),"You Buffoon SERIAL error",MB_OK);*
*}*

But the floats on the output become unreadable (like -541.3414e-035). Any
help would be appreciated.

Thanks in advance,

Jubei



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net