简体   繁体   English

处理跨平台程序的COM端口的最佳方法?

[英]Best way to deal with COM ports for a cross-platform program?

I am writing an application which need to send data to a serial device attached to a COM port. 我正在编写一个需要将数据发送到连接到COM端口的串行设备的应用程序。 I am confused as to what is the best way to deal with such a device? 我对处理这种设备的最佳方法感到困惑?

Shall I write the communication module in C++ or shall I write this in Java? 我应该用C ++编写通讯模块还是应该用Java编写通讯模块?

I want to run the application on Windows as well as Linux. 我想在Windows和Linux上运行该应用程序。

On Linux I would recommend libserial . 在Linux上,我建议libserial You could also consider using Python. 您也可以考虑使用Python。 There is a multi platform pySerial module. 有一个多平台的pySerial模块。 If you decide for Java, rxtx is a multi platform library. 如果决定使用Java,则rxtx是一个多平台库。 On Windows you may use the Windows API for serial communications and combine it with libserial using #ifndef as @Dharma suggested. 在Windows上,您可以使用Windows API进行串行通信 ,并按@Dharma的建议使用#ifndef将其与libserial结合使用。

I recommend you: 我推荐你:

Boost ASIO 提升ASIO

QextSerialPort (based on Qt) QextSerialPort (基于Qt)

You can write the module in c++ by specyfying the #ifndef "_WINDOWS_CODE" for windows code and else part for the linux code. 您可以通过为Windows代码指定#ifndef“ _WINDOWS_CODE”并为Linux代码指定部分来用c ++编写模块。

while compiling in Vc++ put the (_WINDOWS_CODE)macros in the projecty settings and in gc++ remove the macro from project file 在Vc ++中进行编译时,将(_WINDOWS_CODE)宏放入项目设置中,在gc ++中从项目文件中删除宏

COM port is just a plain bit pipe. COM端口只是一个普通的位管道。 The APIs are expected to be fairly simple. 这些API应该相当简单。 In C++, the world has much more experience with writing to ports. 在C ++中,世界在编写端口方面拥有更多的经验。

Other question would be - do you know what port number it is? 另一个问题是-您知道它是什么端口号吗? There're several ways to find out which one is yours, like setup API on windows. 有几种方法可以找出属于您的一种,例如Windows上的setup API。

My suggestion, if you want to run you application in both linux and Windows, go for JAVA!. 我的建议是,如果您想同时在Linux和Windows中运行应用程序,请使用JAVA!。 you can run your program without any need of compilation. 您无需编译即可运行程序。 otherwise you have to distribute seperate binary for both Windows and Linux. 否则,您必须为Windows和Linux分发单独的二进制文件。

Also programming model in both windows and linux for accessing SerialPort in C++, As i am preliminary a Windows C++ developer, you can use CreateFile WIn32 Api to access serial port. Windows和Linux中的编程模型也都可以访问C ++中的SerialPort。由于我是Windows C ++开发人员的初级人员,因此可以使用CreateFile WIn32 Api访问串行端口。

shall i write the communication module in C++ or shall i write this in JAVA? 我应该用C ++编写通讯模块还是用JAVA编写通讯模块?

Which are you more familiar with? 您更熟悉哪个? Are you limited to these two languages? 您是否限于这两种语言? You could also use python with pyserial. 您也可以将python与pyserial一起使用。

Are you doing this as a personal project? 您是否将此作为个人项目进行? Is it for work? 是上班吗? Is someone else working on this with you? 有人在与您合作吗? What are they more familiar with? 他们更熟悉什么?

For C++ you could use Boost.Asio . 对于C ++,您可以使用Boost.Asio

For Java ... I don't know. 对于Java ...我不知道。

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

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