Boost Serial Port Flush Buffer

- Il 22:02, Dario Ramos ha scritto: I'm seriously considering using asyncread and asyncwrite. Since I want read and write calls to be blocking, I'll need to use a Windows event.
- Aug 11, 2012 Clone via HTTPS Clone with Git or checkout with SVN using the repository’s web address.
Serial Port Arduino
Cost to install water heater expansion tank. Flushing a widicom bluetooth serial port; why does select syscall returns evenif no data in serial port buffer; Serial Port Send or Receive data not available for display; reading binary array from Serial Port; ir on serial port vb.net 2005; Serial Port Communuciation in VB with Compact Framework 2.0; getting data through serial port.
Boost Serial Port Flush Buffer For Sale
SerialPort class sample using boost::asio::serial_port
sample_main.cpp
| #include'stdafx.h' |
| #include'SerialPort.h' |
| intmain(int argc, char* argv[]) |
| { |
| bool rv; |
| SerialPort::print_devices(); |
| std::string name = SerialPort::get_port_name(0); |
| SerialPort serial; |
| rv = serial.start(name.c_str(), 115200); |
| if (rv false) { |
| return -1; |
| } |
| // initialize |
| serial.end_of_line_char(0x0d); |
| serial.write_some('BI010rn'); |
| serial.write_some('PE011rn'); |
| serial.write_some('RA01000rn'); |
| // wait |
| Sleep(5 * 1000); |
| serial.stop(); |
| return0; |
| } |
SerialPort.cpp

Serial Port Buffer
| #include'StdAfx.h' |
| #include<Setupapi.h> |
| #pragma comment(lib, 'Setupapi.lib') |
| #include'SerialPort.h' |
| SerialPort::SerialPort(void) : end_of_line_char_('n') |
| { |
| } |
| SerialPort::~SerialPort(void) |
| { |
| stop(); |
| } |
| charSerialPort::end_of_line_char() const |
| { |
| returnthis->end_of_line_char_; |
| } |
| voidSerialPort::end_of_line_char(constchar &c) |
| { |
| this->end_of_line_char_ = c; |
| } |
| std::vector<std::string> SerialPort::get_port_names() |
| { |
| std::vector<std::string> names; |
| BOOL rv; |
| DWORD size; |
| GUID guid[1]; |
| HDEVINFO hdevinfo; |
| DWORD idx = 0; |
| SP_DEVINFO_DATA devinfo_data; |
| devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA); |
| int count = 0; |
| rv = SetupDiClassGuidsFromName('Ports', (LPGUID)&guid, 1, &size) ; |
| if (!rv) { |
| std::cout << 'error : SetupDiClassGuidsFromName() failed..' << std::endl; |
| return names; |
| } |
| hdevinfo = SetupDiGetClassDevs(&guid[0], NULL, NULL, DIGCF_PRESENT DIGCF_PROFILE); |
| if (hdevinfo INVALID_HANDLE_VALUE) { |
| std::cout << 'error : SetupDiGetClassDevs() failed..' << std::endl; |
| return names; |
| } |
| while(SetupDiEnumDeviceInfo(hdevinfo, idx++, &devinfo_data)) { |
| char friendly_name[MAX_PATH]; |
| char port_name[MAX_PATH]; |
| DWORD prop_type; |
| DWORD type = REG_SZ; |
| HKEY hKey = NULL; |
| rv = ::SetupDiGetDeviceRegistryProperty(hdevinfo, &devinfo_data, SPDRP_FRIENDLYNAME, &prop_type, |
| (LPBYTE)friendly_name, sizeof(friendly_name), &size); |
| if (!rv) { |
| std::cout << 'error : SetupDiGetDeviceRegistryProperty() failed..' << std::endl; |
| continue; |
| } |
| hKey = ::SetupDiOpenDevRegKey(hdevinfo, &devinfo_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); |
| if (!hKey) continue; |
| size = sizeof(port_name); |
| rv = ::RegQueryValueEx(hKey, 'PortName', 0, &type, (LPBYTE)&port_name, &size); |
| ::RegCloseKey(hKey); |
| names.push_back(port_name); |
| } |
| SetupDiDestroyDeviceInfoList(hdevinfo); |
| return names; |
| } |
| intSerialPort::get_port_number() |
| { |
| std::vector<std::string> names = get_port_names(); |
| return names.size(); |
| } |
| std::string SerialPort::get_port_name(constunsignedint &idx) |
| { |
| std::vector<std::string> names = get_port_names(); |
| if (idx >= names.size()) returnstd::string(); |
| return names[idx]; |
| } |
| voidSerialPort::print_devices() |
| { |
| std::cout << 'SerialPort::print_devices()' << std::endl; |
| int n = SerialPort::get_port_number(); |
| for (int i = 0; i < n; ++i) { |
| std::string name = SerialPort::get_port_name(i); |
| std::cout << 't' << name.c_str() << std::endl; |
| } |
| } |
| boolSerialPort::start(constchar *com_port_name, int baud_rate) |
| { |
| boost::system::error_code ec; |
| if (port_) { |
| std::cout << 'error : port is already opened..' << std::endl; |
| returnfalse; |
| } |
| port_ = serial_port_ptr(newboost::asio::serial_port(io_service_)); |
| port_->open(com_port_name, ec); |
| if (ec) { |
| std::cout << 'error : port_->open() failed..com_port_name=' |
| << com_port_name << ', e=' << ec.message().c_str() << std::endl; |
| returnfalse; |
| } |
| // option settings.. |
| port_->set_option(boost::asio::serial_port_base::baud_rate(baud_rate)); |
| port_->set_option(boost::asio::serial_port_base::character_size(8)); |
| port_->set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one)); |
| port_->set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none)); |
| port_->set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none)); |
| boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service_)); |
| async_read_some_(); |
| returntrue; |
| } |
| voidSerialPort::stop() |
| { |
| boost::mutex::scoped_lock look(mutex_); |
| if (port_) { |
| port_->cancel(); |
| port_->close(); |
| port_.reset(); |
| } |
| io_service_.stop(); |
| io_service_.reset(); |
| } |
| intSerialPort::write_some(const std::string &buf) |
| { |
| returnwrite_some(buf.c_str(), buf.size()); |
| } |
| intSerialPort::write_some(constchar *buf, constint &size) |
| { |
| boost::system::error_code ec; |
| if (!port_) return -1; |
| if (size 0) return0; |
| return port_->write_some(boost::asio::buffer(buf, size), ec); |
| } |
| voidSerialPort::async_read_some_() |
| { |
| if (port_.get() NULL !port_->is_open()) return; |
| port_->async_read_some( |
| boost::asio::buffer(read_buf_raw_, SERIAL_PORT_READ_BUF_SIZE), |
| boost::bind( |
| &SerialPort::on_receive_, |
| this, boost::asio::placeholders::error, |
| boost::asio::placeholders::bytes_transferred)); |
| } |
| voidSerialPort::on_receive_(const boost::system::error_code& ec, size_t bytes_transferred) |
| { |
| boost::mutex::scoped_lock look(mutex_); |
| if (port_.get() NULL !port_->is_open()) return; |
| if (ec) { |
| async_read_some_(); |
| return; |
| } |
| for (unsignedint i = 0; i < bytes_transferred; ++i) { |
| char c = read_buf_raw_[i]; |
| if (c end_of_line_char_) { |
| this->on_receive_(read_buf_str_); |
| read_buf_str_.clear(); |
| } |
| else { |
| read_buf_str_ += c; |
| } |
| } |
| async_read_some_(); |
| } |
| voidSerialPort::on_receive_(const std::string &data) |
| { |
| std::cout << 'SerialPort::on_receive_() : ' << data << std::endl; |
| } |
SerialPort.h
| #pragma once |
| #include<boost/asio.hpp> |
| #include<boost/asio/serial_port.hpp> |
| #include<boost/system/error_code.hpp> |
| #include<boost/system/system_error.hpp> |
| #include<boost/bind.hpp> |
| #include<boost/thread.hpp> |
| #include<string> |
| #include<vector> |
| typedef boost::shared_ptr<boost::asio::serial_port> serial_port_ptr; |
| #defineSERIAL_PORT_READ_BUF_SIZE256 |
| classSerialPort |
| { |
| protected: |
| boost::asio::io_service io_service_; |
| serial_port_ptr port_; |
| boost::mutex mutex_; |
| char read_buf_raw_[SERIAL_PORT_READ_BUF_SIZE]; |
| std::string read_buf_str_; |
| char end_of_line_char_; |
| private: |
| SerialPort(const SerialPort &p); |
| SerialPort &operator=(const SerialPort &p); |
| public: |
| SerialPort(void); |
| virtual~SerialPort(void); |
| charend_of_line_char() const; |
| voidend_of_line_char(constchar &c); |
| virtualboolstart(constchar *com_port_name, int baud_rate=9600); |
| virtualvoidstop(); |
| intwrite_some(const std::string &buf); |
| intwrite_some(constchar *buf, constint &size); |
| staticintget_port_number(); |
| static std::string get_port_name(constunsignedint &idx); |
| static std::vector<std::string> get_port_names(); |
| staticvoidprint_devices(); |
| protected: |
| virtualvoidasync_read_some_(); |
| virtualvoidon_receive_(const boost::system::error_code& ec, size_t bytes_transferred); |
| virtualvoidon_receive_(const std::string &data); |
| }; |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment