与 Windows XP/win32 进行串行通信的基本示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/237487/
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:31:30  来源:igfitidea点击:

Basic example of serial communication with Windows XP/win32

windowswinapiserial-port

提问by

I am working with a peripheral device that needs to be communicated through serial. I can send it commands using HyperTerminal, but now I need to write programs that will let me do it without HyperTerminal. Can somebody point me to a website and/or show me a sample hello world program to get me started? I have searched through many sites which give me uncompilable/ancient VC6 code.

我正在使用需要通过串行通信的外围设备。我可以使用超级终端向它发送命令,但现在我需要编写程序,让我在没有超级终端的情况下也能做到。有人可以将我指向一个网站和/或向我展示一个示例 hello world 程序以帮助我入门吗?我搜索了许多网站,这些网站提供了无法编译的/古老的 VC6 代码。

回答by Adam Rosenfield

In order to interface with the serial port, you open a file with one of the special filenames "COM1" through "COM9". For serial ports with higher numbers, the special filename begins with \\?\, which in C/C++ code must be escaped as "\\\\?\\COM10", etc.

为了与串行端口接口,您打开一个文件,其中一个特殊文件名是“COM1”到“COM9”。对于数字较大的串口,特殊文件名以 \\?\ 开头,在 C/C++ 代码中必须转义为“\\\\?\\COM10”等。

http://msdn.microsoft.com/en-us/library/ms810467.aspxhas a really good tutorial on using the serial port. Note that you should use the Windows file I/O functions such as CreateFile(), ReadFile(), and WriteFile(). I'm not sure if it will work to use standard I/O functions such as fopen(), fread(), and fwrite().

http://msdn.microsoft.com/en-us/library/ms810467.aspx有一个关于使用串口的非常好的教程。请注意,您应该使用Windows文件I / O功能,如CreateFile()ReadFile()WriteFile()。我不知道是否会工作到使用标准I / O功能,如fopen()fread()fwrite()

回答by Andru Luvisi

Microsoft provides an article with sample codedescribing how to do this under Win32.

Microsoft 提供了一篇文章,其中包含描述如何在 Win32 下执行此操作的示例代码

回答by MattyT

Boost:asiomay be able to help as a serial device was added recently.

Boost:asio可能会有所帮助,因为最近添加了串行设备。

Fair warning though; the serial port documentation is light, presumably since it's quite new (it was added in asio 1.1.1 which was included in boost 1.36).

公平的警告;串口文档是,大概是因为这是相当新的(它是在ASIO 1.1.1其中包括在提升1.36加)。

But working your way through asio is, IMHO, a better solution than using the raw Win32 API. Why? It'll be easier to read and maintain (it's a higher level API) and it'll be cross platform (except where you need to specify the OS-specific device name).

但是,恕我直言,通过 asio 工作是比使用原始 Win32 API 更好的解决方案。为什么?它将更易于阅读和维护(它是一个更高级别的 API)并且它将是跨平台的(除非您需要指定特定于操作系统的设备名称)。

The Boost - Usersand asio.usermailing lists are quite active and friendly and ought to be able to help you out if you get stuck.

The Boost - Usersasio.user邮件列表非常活跃和友好,如果您遇到困难,应该能够帮助您。

回答by kenny

If using .NET 2.0 see System.IO.Ports and this articleshould be helpful. If direct Win32, then Adam's answeris best.

如果使用 .NET 2.0,请参阅 System.IO.Ports,这篇文章应该会有所帮助。如果直接使用 Win32,那么Adam 的答案是最好的。

回答by Tim

I believe you will find plenty of sample code for C# as well if you find VC6 too ancient. I think there are also a bunch of "free" serial/COM port wrappers but I just wrote my own when I wrote an RS232 device controller piece of software.

如果您发现 VC6 太古老,我相信您也会找到大量的 C# 示例代码。我认为还有一堆“免费”串行/COM 端口包装器,但我在编写 RS232 设备控制器软件时只是编写了自己的。

google C# and serial port or rs232

谷歌 C# 和串口或 rs232

I got these:

我得到了这些:

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx

http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx

You should have no problem finding suitable code with a google search.

使用谷歌搜索找到合适的代码应该没有问题。