I'm new to Boost and I'm trying to use it with C++. I'm using boost_1_82_0-msvc-14.1-64.exe on Visual Studio 2017. I've written some code that reads COM port data and I've tried my best to fix it, but for some reason, no matter what I do, I get the same errors. The first error I received was 'PCH Warning: header stop not at file scope,' which I tried to fix with #define no_init_all, but it doesn't seem to work as I'm still getting the error. I've seen that having an older version of Boost can lead to this, but I have the latest version of Boost. So, I'm unsure what can be done here.
#if (_MSC_VER >= 1915)
#define no_init_all deprecated
#endif
#include <iostream>
#include <boost/asio.hpp>
#include <string>
#include <boost/asio/io_service.hpp>
struct SensorData {
float heading;
float magneticNorth;
float magneticSouth;
};
SensorData readSerialData() {
boost::asio::io_context io;
boost::asio::serial_port port(io, "COM3");
port.set_option(boost::asio::serial_port_base::baud_rate(115200));
SensorData sensorData;
std::string sensorDataStr;
std::size_t headingPos, magNorthPos, magSouthPos;
std::string headingStr, magNorthStr, magSouthStr;
while (true)
{
boost::asio::read_until(port, boost::asio::dynamic_buffer(sensorDataStr), '\n'); // Read until a newline character is received
headingPos = sensorDataStr.find("Heading:"); // Find the position of "Heading:" in the string
if (headingPos != std::string::npos) {
headingStr = sensorDataStr.substr(headingPos + 9); // Extract the heading value as a string
sensorData.heading = std::stof(headingStr); // Convert the heading string to a float
}
magNorthPos = sensorDataStr.find("Magnetic North:"); // Find the position of "Magnetic North:" in the string
if (magNorthPos != std::string::npos) {
magNorthStr = sensorDataStr.substr(magNorthPos + 16); // Extract the magnetic north value as a string
sensorData.magneticNorth = std::stof(magNorthStr); // Convert the magnetic north string to a float
}
magSouthPos = sensorDataStr.find("Magnetic South:"); // Find the position of "Magnetic South:" in the string
if (magSouthPos != std::string::npos) {
magSouthStr = sensorDataStr.substr(magSouthPos + 16); // Extract the magnetic south value as a string
sensorData.magneticSouth = std::stof(magSouthStr); // Convert the magnetic south string to a float
}
if (headingPos != std::string::npos && magNorthPos != std::string::npos && magSouthPos != std::string::npos) {
// All data has been received, so return the SensorData struct
return sensorData;
}
}
}
I would appreciate help in understanding what I've done wrong and why.
Thanks.
So I've been playing around, and it seems it works in Visual Studio 2019, not 2017, and only in x86. This is very strange. I'm thinking that either there might be a bug or another version I must have installed a long time ago and forgotten about.