简体   繁体   English

如何在Visual Studio C ++中实现Windows和linux之间可移植的串行端口编程?

[英]How to do serial port programming in visual studio C++ that portable between Windows & linux?

I'm writing a test program that need to access serial port. 我正在编写一个需要访问串行端口的测试程序。 I'm doing it in Visual studio 2012 now, but I want to port to Linux later. 我现在在Visual Studio 2012中进行操作,但是我想稍后再移植到Linux。 (For using in my Pandaboard) (用于在我的Pandaboard中使用)
Can you suggest me a way to access serial port that has almost the same interface between Win & Linux? 您能为我提供一种访问Win和Linux之间几乎相同接口的串行端口的方法吗?
I used to do it in Labview, but now I want to turn to C++ 我曾经在Labview中做过,但现在我想转向C ++
Thank you so much for your help! 非常感谢你的帮助!

Boost.ASIO is well-documented, well-tested, has been peer reviewed by thousands of people, and fully supports serial port communication in a cross-platform manner. Boost.ASIO的文档记录齐全,测试良好,经过数千人的同行审查,并全面支持跨平台方式的串行端口通信。 Specific documentation can be found here . 具体的文档可以在这里找到。

That being said, it does assume a moderate skill level in modern C++, so if you're new to the language then the learning curve may be a bit steep. 话虽这么说,但它确实在现代C ++中具有中等技能,因此,如果您是该语言的新手,则学习曲线可能会有些陡峭。

There isn't much to a serial port interface...you should be able to wrap all of the implementation details underneath it. 串行接口没有什么...您应该能够在其下面包装所有实现细节。

class ISerialPort
{
public:
   void open(const std::string &serialPortName) = 0;
   void close() = 0;

   void write(const vector<char> &data) = 0;
   vector<char> read(size_t bytesToRead) = 0;
}

Although it isn't much of an "interface" as it is a common header that two different platforms would implement. 尽管它不是一个“接口”,但它是两个不同平台将要实现的通用标头。

Edit: Serial Ports under Linux are accessed by opening device nodes. 编辑:通过打开设备节点可以访问Linux下的串行端口。 Such as /dev/ttyS0 (Serial Port 0). 例如/ dev / ttyS0(串行端口0)。 In windows you're doing the same thing, but instead of opening a device node, you're opening a file (a COM port). 在Windows中,您正在做相同的事情,但是不是打开设备节点,而是打开文件(COM端口)。 Such as COM1. 如COM1。

In linux you're diving into platform dependent issues such as opening device nodes. 在Linux中,您将深入研究与平台有关的问题,例如打开设备节点。 In Linux it'll be opening files (COM ports). 在Linux中,它将打开文件(COM端口)。

Google c++ windows/linux serial port and come back with a more specific question. Google c ++ Windows / Linux串行端口,然后返回一个更具体的问题。 You just asked about an interface;) 您刚刚询问了一个接口;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM