我想使用标准C ++ fstream类从串口(/ dev / ttyGS0)读取和写入数据。
两小时前我认为这是一项微不足道的任务(它只是一个文件(嗯,这是一个特殊的...)),直到写入串口引发错误并且perror()显示“非法搜寻” 。进一步调查显示/ dev / ttyGS0(与每个串行设备一样)是一个不可搜索的文件,可能导致错误。
如何使用标准c ++库写入串口?
它可能完全或者我必须回退到像open()这样的C函数,还是使用像LibSerial这样的附加库?
答案 0 :(得分:0)
与此同时,我自己想出了一个解决方案。
简短回答:使用标准c ++库读取或写入不可搜索的设备并非易事。
答案很长:可以从std :: filebuf派生并更改实现。从理论上讲,它实际上非常简单,因为只需要在函数 std :: filebuf :: _ M_seek 中注释掉一行代码。
实际上并不是那么简单,因为 std :: filebuf :: _ M_seek 本身不是虚拟的,这意味着它不能在派生类中被覆盖。幸运的是(在这种情况下)仅由另一个函数 std :: filebuf :: overflow 使用,它是虚拟的。
所以这是我的班级 FilebufNoSeek ,可用于访问不可搜索的设备:
/*
* filebufNoSeek.hpp
*/
#ifndef INCLUDE_SYSTEMIO_FILEBUFNOSEEK_HPP_
#define INCLUDE_SYSTEMIO_FILEBUFNOSEEK_HPP_
#include <fstream>
using namespace std;
class FilebufNoSeek : public filebuf
{
public:
FilebufNoSeek() {};
protected:
filebuf::pos_type _M_seek(filebuf::off_type __off, ios_base::seekdir __way, filebuf::__state_type __state);
virtual filebuf::int_type overflow(filebuf::int_type __c);
};
#endif /* INCLUDE_SYSTEMIO_FILEBUFNOSEEK_HPP_ */
我在gcc源代码中找到的libstdc ++实现100%复制了实现。出于版权原因,我还会发布原始文件的标题。
您可以轻松找到注释掉的一行 - 这是“非法搜寻”错误的来源。
// File based streams -*- C++ -*-
// Copyright (C) 1997-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file bits/fstream.tcc
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{fstream}
*/
/*
* FilebufNoSeek.cpp
*
* Created / copied / modified on: 21.12.2015 by ChristophK
*/
#include "FilebufNoSeek.hpp"
filebuf::pos_type FilebufNoSeek::_M_seek(filebuf::off_type __off, ios_base::seekdir __way, filebuf::__state_type __state)
{
pos_type __ret = pos_type(off_type(-1));
if (_M_terminate_output())
{
// off_type __file_off = _M_file.seekoff(__off, __way); // Causes "illegal seek"
off_type __file_off = off_type(0); // New - no seek
if (__file_off != off_type(-1))
{
_M_reading = false;
_M_writing = false;
_M_ext_next = _M_ext_end = _M_ext_buf;
_M_set_buffer(-1);
_M_state_cur = __state;
__ret = __file_off;
__ret.state(_M_state_cur);
}
}
return __ret;
}
filebuf::int_type FilebufNoSeek::overflow(filebuf::int_type __c)
{
int_type __ret = traits_type::eof();
const bool __testeof = traits_type::eq_int_type(__c, __ret);
const bool __testout = (_M_mode & ios_base::out
|| _M_mode & ios_base::app);
if (__testout)
{
if (_M_reading)
{
_M_destroy_pback();
const int __gptr_off = _M_get_ext_pos(_M_state_last);
if (_M_seek(__gptr_off, ios_base::cur, _M_state_last)
== pos_type(off_type(-1)))
return __ret;
}
if (this->pbase() < this->pptr())
{
// If appropriate, append the overflow char.
if (!__testeof)
{
*this->pptr() = traits_type::to_char_type(__c);
this->pbump(1);
}
// Convert pending sequence to external representation,
// and output.
if (_M_convert_to_external(this->pbase(),
this->pptr() - this->pbase()))
{
_M_set_buffer(0);
__ret = traits_type::not_eof(__c);
}
}
else if (_M_buf_size > 1)
{
// Overflow in 'uncommitted' mode: set _M_writing, set
// the buffer to the initial 'write' mode, and put __c
// into the buffer.
_M_set_buffer(0);
_M_writing = true;
if (!__testeof)
{
*this->pptr() = traits_type::to_char_type(__c);
this->pbump(1);
}
__ret = traits_type::not_eof(__c);
}
else
{
// Unbuffered.
char_type __conv = traits_type::to_char_type(__c);
if (__testeof || _M_convert_to_external(&__conv, 1))
{
_M_writing = true;
__ret = traits_type::not_eof(__c);
}
}
}
return __ret;
}
以下是如何使用 FilebufNoSeek 类。只需将其传递给std :: iostream的构造函数:
FilebufNoSeek serialFilebuf;
serialFilebuf.open("/dev/ttyGS0");
std::iostream serialStream(serialFilebuf);
serialStream << "I'm writing to a nonseekable file" << std::endl;
serialFilebuf.close();
希望这会有所帮助 - 祝你好运!