In Arduino data can be read as follows:
void setup() {
Serial.begin(9600);
}
String Comand = "";
void loop() {
if (Serial.available() > 0) //If there is data read
{
char c = Serial.read(); //get byte
if(c!='\n')
{
Comand+=c;
}
else
{
if(Comand == "MyComand")
{
Serial.println("Comand 1");
}
//else
//...
Comand = "";
}
}
}
С++ boost asio Here is an example of c ++ playback of data from the port, but what function to check if there is anything to read?
#include <boost/asio.hpp>
#include <windows.h>
//....
int main(int, char**)
{
{
boost::asio::io_service io;
boost::asio::serial_port serial(io,"COM5");
serial.set_option(boost::asio::serial_port_base::baud_rate(9600));
std::string s = "MyComand\n";
boost::asio::write(serial,boost::asio::buffer(s.c_str(),s.size()));
std::string result;
//while serial data > 0 ??? How to check
{
using namespace boost;
char c;
asio::read(serial,asio::buffer(&c,1));
result+=c;
}
std::cout<<result.c_str();
}
return 0;
}
If I read in a cycle, and there is no data, the program will stop and wait for the data, it does not suit me. How to check if there is data before trying to read.