windows 如何配置串行端口并与之通信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:04:52  来源:igfitidea点击:

How do I configure and communicate with a serial port?

javac++windowsunix

提问by Greg Case

I need to send and receive data over serial connections (RS-232 and RS-422).

我需要通过串行连接(RS-232 和 RS-422)发送和接收数据。

How do I set up and communicate with such a connection? How do I figure out what the configuration settings (e.g. baud rate) should be and how do I set them?

我如何设置这样的连接并与之通信?我如何确定配置设置(例如波特率)应该是什么以及如何设置它们?

In particular I am looking to do this in Java, C/C++, or one of the major Unix shells but I also have some interest in serial programming using Windows/Hyperterminal.

特别是我希望在 Java、C/C++ 或主要的 Unix shell 之一中执行此操作,但我对使用 Windows/超级终端的串行编程也有一些兴趣。

采纳答案by Louis Brandy

Build a time machine and go back to 1987? Ho ho.

建造时光机,回到 1987 年?呵呵。

Ok, no more snarky comments.

好了,不要再发表尖刻的评论了。

How do I figure out what the configuration settings (e.g. baud rate) should be...

我如何弄清楚配置设置(例如波特率)应该是什么......

Read the datasheet? Ok, ok. Seriously, last one. If you don't know the baud rate of the device you are trying to communicate with, you have two choices. Start guessing, or possibly bust out an o-scope. If you need a good starting point, let me suggest 9600-8-N-1. My suspicion is you can get there with brute force relatively quickly. There's a third option of having an old-school ninja who can tell just by the LOOK of the garbled characters at some standard baud rate what actual baud rate is. An impressive party trick to be sure.

阅读数据表?好的好的。说真的,最后一个。如果您不知道要与之通信的设备的波特率,您有两种选择。开始猜测,或者可能破坏 o 型示波器。如果您需要一个好的起点,我建议您使用 9600-8-N-1。我怀疑你可以用蛮力相对较快地到达那里。还有第三种选择,那就是让老派忍者在某个标准波特率下仅通过乱码字符的外观就可以判断实际波特率是多少。肯定是一个令人印象深刻的派对技巧。

Hopefully though you have access to this information. In unix/linux, you can get ahold of minicom to play with the serial port directly. This should make it fairly quick to get the configuration figured out.

希望您可以访问这些信息。在unix/linux下,可以直接用minicom来玩串口。这应该可以相当快地弄清楚配置。

one of the major Unix shells

主要的 Unix shell 之一

In Unix the serial port(s) is/are file-mapped into the /dev/ subdir. ttyS0, for example. If you setup the correct baud rate and whatnot using minicom, you can even cat stuff to that file to send stuff out there.

在 Unix 中,串行端口被文件映射到 /dev/ 子目录中。ttyS0,例如。如果您使用 minicom 设置了正确的波特率和诸如此类的东西,您甚至可以将内容放入该文件以将内容发送到那里。

On to the meat of the question, you can access it programmatically through the POSIX headers. termios.h is the big one.

关于问题的核心,您可以通过 POSIX 标头以编程方式访问它。termios.h 是最重要的。

See: http://www.easysw.com/~mike/serial/serial.html#3_1(NOT AVAILABLE ANYMORE)

请参阅:http: //www.easysw.com/~mike/serial/serial.html#3_1(不再提供)

but I also have some interest in serial programming using Windows/Hyperterminal.

但我也对使用 Windows/Hyperterminal 进行串行编程感兴趣。

Hyperterminal and minicom are basically the same program. As for how Windows let's you get access to the serial port, I'll leave that question for someone else. I haven't done that in Windows since the Win95 days.

Hyperterminal 和 minicom 基本上是同一个程序。至于 Windows 如何让您访问串行端口,我将把这个问题留给其他人。自 Win95 时代以来,我还没有在 Windows 中这样做过。

回答by Marcel

If you want to code in Java I really recommend SerialIOs SerialPort. It is very easy to use and saves you days of work. I've never found an open source library as good as SerialIO, REALLY!

如果你想用 Java 编码,我真的推荐 SerialIOs SerialPort。它非常易于使用,可为您节省数天的工作时间。我从来没有找到像 SerialIO 这样好的开源库,真的!

My advice: do not use Sun's serial IO framework! It is from 1998 and full of bugs. You can use rxtxbut serialio is better!

我的建议:不要使用 Sun 的串行 IO 框架!它是从 1998 年开始的,充满了错误。您可以使用rxtx但 serialio 更好!

回答by smh

For C/C++ on Windows you have (at least) two choices:

对于 Windows 上的 C/C++,您(至少)有两个选择:

  1. Use the SerialPort class provided by .NET.
  2. Use the Win32 API. There is an extensive MSDN articledating back to 1995, and many free libraries and examples on the web to get you started.
  1. 使用 .NET 提供的 SerialPort 类。
  2. 使用 Win32 API。有一篇可追溯到 1995 年的大量 MSDN 文章,以及网络上的许多免费库和示例可帮助您入门。

The .NET option will be much easier.

.NET 选项会容易得多。

回答by Ferruccio

If it needs to be cross platfrom, I would suggest looking at Boost Asio.

如果需要跨平台,我建议查看 Boost Asio

回答by Lawrence Dol

Awhile back I wrote a decent sized application to route connections from a farm of modems through to a TCP/IP network address.

不久前,我编写了一个体面的应用程序来将连接从调制解调器群路由到 TCP/IP 网络地址。

Initially I looked for an unencumbered (free) Serial IO library. I tried Sun's, IBM's and RxTx. They were fine for developing the application, and in initial testing, but in production they each proved unstable.

最初我寻找一个无障碍(免费)串行 IO 库。我尝试了 Sun 的、IBM 的和 RxTx。它们在开发应用程序和初始测试方面都很好,但在生产中它们都被证明不稳定。

Finally I paid for SerialIO's SerialPort. Converting over was literally an exercise in changing imports, and the library has been absolutelyrock solid - I cannot recommend it enough. My application has been running in the field 24/7 for a couple of years now, with not a single problem encountered by multiple customers.

最后我支付了 SerialIO 的 SerialPort。转换实际上是更改导入的一种练习,并且该库绝对坚如磐石 - 我不能完全推荐它。我的应用程序已经在现场 24/7 运行了几年,多个客户没有遇到任何问题。

If you start development using SerialPort, they have a better API and I would use it.

如果你开始使用 SerialPort 进行开发,他们有更好的 API,我会使用它。

If you need cross platform support, Java with SerialPort was the best choice I could find.

如果您需要跨平台支持,带有 SerialPort 的 Java 是我能找到的最佳选择。

Lastly, their licensing is pretty darn reasonable as long as you are not preinstalling software on the equipment for your customer(s).

最后,只要您没有在设备上为您的客户预装软件,他们的许可就非常合理。

回答by graham.reeds

At work we use teraterm and realterm for checking serial data is correctly formatted. Also we have a hardware splitter with a switch so we can monitor traffic to our application via a cable back to another port.

在工作中,我们使用 teraterm 和 realterm 来检查串行数据的格式是否正确。此外,我们还有一个带有交换机的硬件分离器,因此我们可以通过返回另一个端口的电缆监控到我们应用程序的流量。

Windows allows you access to the serial port via CreateFile. That gives you a handle and from there you can configure access.

Windows 允许您通过 CreateFile 访问串行端口。这为您提供了一个句柄,您可以从那里配置访问权限。

回答by Patrick

From the other side, if you want to do it using C#, which will run on both Windows and Linux--with some limitations(EDIT: which may be out of date. I have no way to test it.). Just create a SerialPortobject, set its baudrate, port and any other odd settings, call open on it, and write out your byte[]s. After all the setup, the SerialPort object acts very similar to any networked stream, so it should be easy enough to figure out.

另一方面,如果你想使用 C# 来做它,它可以在 Windows 和 Linux 上运行——有一些限制(编辑:这可能已经过时了。我没有办法测试它。)。只需创建一个SerialPort对象,设置它的波特率、端口和任何其他奇怪的设置,调用 open 并写出你的 byte[]s。完成所有设置后,SerialPort 对象的行为与任何网络流非常相似,因此应该很容易弄清楚。

And as ibrandy states, you need to know all these settings, like baud rate, before you even start attempting to communicate to any serial device.

正如 ibrandy 所说,在开始尝试与任何串行设备通信之前,您需要了解所有这些设置,例如波特率。

回答by Maciej Hehl

Depending on the device You are trying to communicate with, there may be more parameters than the baud rate, number of data bits, type of parity checking and number of stop bits to consider. If I recall correctly, modems use nine lines of the RS-232C interface. Some devices like, for example cash registers, may use hardware handshaking on RTS/CTS lines or on DTR/STR lines.

根据您尝试与之通信的设备,可能需要考虑比波特率、数据位数、奇偶校验类型和停止位数更多的参数。如果我没记错的话,调制解调器使用 9 条 RS-232C 接口线。某些设备(例如收银机)可能会在 RTS/CTS 线路或 DTR/STR 线路上使用硬件握手。

In general it's good to know how the interface works. You can't communicate if the baud rate doesn't match, but wrong setting of other parameters might kind of work. For example You can easily send data to the device expecting 1 stop bit with 2 stop bits set. Problems start when You try to receive data in such case. You can also use appropriately set parity bit as one of stop bits, etc.

一般来说,最好了解界面的工作原理。如果波特率不匹配,您将无法通信,但其他参数的错误设置可能会起作用。例如,您可以轻松地将数据发送到需要 1 个停止位并设置 2 个停止位的设备。在这种情况下,当您尝试接收数据时就会出现问题。您还可以使用适当的设置奇偶校验位作为停止位之一等。

回答by Marco

If you are not forced to use a particular compiler I suggest to use Qt and in the new 5.3 version you will find a class dedicated to serial ports:

如果您不是被迫使用特定的编译器,我建议您使用 Qt,并且在新的 5.3 版本中,您会发现一个专用于串行端口的类:

http://qt-project.org/doc/qt-5/qserialport.html

http://qt-project.org/doc/qt-5/qserialport.html

The code you will write will run on all supprited Qt platforms, at least those that have serial ports.

您将编写的代码将在所有支持的 Qt 平台上运行,至少是那些具有串行端口的平台。

回答by Mzzl

I have been using purejavacomm: It is an implementation of javax.comm written in pure java + JNA

我一直在用purejavacomm它是用纯java+JNA写的javax.comm的实现

Unlike rxtx, you don't need to install a dll. It is written in pure Java + JNA, which solved the problem of portability between Windows and Linux for me. It should be easy to port to other OS-es that JNA supports, such as Solaris and FreeBSD, but I haven't tried it.

与 rxtx 不同,您不需要安装 dll。它是用纯Java+JNA编写的,为我解决了Windows和Linux之间的可移植性问题。移植到 JNA 支持的其他操作系统(例如 Solaris 和 FreeBSD)应该很容易,但我还没有尝试过。

You might expect a pure java library to lag behind a native implementation such as rxtx in performance, but with modern CPU's, the bottleneck is very likely to be the bitrate of your serial port, not CPU cycles. Also, it's much easier to debug than a mixed Java/Native library or pure compiled native code.

您可能希望纯 Java 库在性能上落后于本机实现,例如 rxtx,但是对于现代 CPU,瓶颈很可能是串行端口的比特率,而不是 CPU 周期。此外,与混合的 Java/本机库或纯编译本机代码相比,调试要容易得多。