您好,登錄后才能下訂單哦!
備忘使用。
- #include <boost/bind.hpp>
- #include <boost/asio.hpp>
- #include <boost/thread.hpp>
- using boost::asio::io_service;
- using boost::system::error_code;
- using boost::asio::serial_port;
- using boost::asio::deadline_timer;
- using boost::asio::buffer;
- class MyCom
- {
- public:
- MyCom(void)
- {
- _pSerialPort= new serial_port(_ios);
- _pTimer = new deadline_timer(_ios);
- }
- ~MyCom(void)
- {
- if (_pTimer != NULL)
- {
- delete _pTimer;
- _pTimer = NULL;
- }
- if (_pSerialPort != NULL)
- {
- delete _pSerialPort;
- _pSerialPort = NULL;
- }
- }
- void Open(const string& comName);
- {
- _pSerialPort->open(comName);
- _pSerialPort->set_option(serial_port::flow_control(serial_port::flow_control::none)); //流量控制為none
- _pSerialPort->set_option(serial_port::parity(serial_port::parity::none)); //奇偶檢驗為none
- _pSerialPort->set_option(serial_port::stop_bits(serial_port::stop_bits::one)); //停止位為1
- _pSerialPort->set_option(serial_port::character_size(8)); //字符大小(數據位)為8
- _pSerialPort->set_option(serial_port::baud_rate(115200));//波特率
- }
- void Send(const string& data)
- {//同步發數據
- _mutex.lock();
- _pSerialPort->write_some(buffer(data, data.size()));
- _mutex.unlock();
- }
- string Recv()
- {//異步收數據
- _mutex.lock();
- memset(_buf, 0, sizeof(_buf));
- _pSerialPort->async_read_some(buffer(_buf, 256),
- boost::bind(&MyCom::RecvHandle, this,
- boost::asio::placeholders::error,//傳送錯誤碼
- boost::asio::placeholders::bytes_transferred//傳送字節數
- ));
- _mutex.unlock();
- _pTimer->expires_from_now(boost::posix_time::millisec(SLEEP_TIME));
- _pTimer->async_wait(boost::bind(&serial_port::cancel, _pSerialPort));
- _ios.run();//異步情況下使用詞句才開始執行
- _ios.reset();//還原狀態
- return string(_buf, _ret);
- }
- void Close()
- {
- _mutex.lock();
- if (_pSerialPort->is_open())
- _pSerialPort->close();
- _mutex.unlock();
- }
- protected:
- void RecvHandle(const error_code& error, size_t bytes_transferred)
- {
- if (!error)
- _pTimer->cancel();//沒有錯誤就結束定時器
- _ret = bytes_transferred;
- }
- private:
- boost::asio::io_service _ios;
- serial_port* _pSerialPort;
- deadline_timer* _pTimer;
- char _buf[256];
- size_t _ret;
- boost::mutex _mutex;
- };
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。