与Windows XP/win32串行通信的基本示例
我正在使用需要通过串行通信的外围设备。 我可以使用超级终端向其发送命令,但现在我需要编写程序,让我无需使用超级终端即可执行此操作。 有人可以向我指出一个网站和/或向我展示一个示例 hello world 程序来帮助我入门吗? 我搜索过许多网站,这些网站给了我无法编译/古老的 VC6 代码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了与串行端口连接,请打开一个具有特殊文件名“COM1”到“COM9”之一的文件。 对于编号较大的串口,特殊文件名以 \\?\ 开头,在 C/C++ 代码中必须转义为“\\\\?\\COM10”等。
http://msdn.microsoft.com/en-us/library/ms810467.aspx 有一个关于使用串行端口的非常好的教程。 请注意,您应该使用 Windows 文件 I/O 函数,例如
CreateFile()
、ReadFile()
和WriteFile()
。 我不确定使用标准 I/O 函数是否有效,例如fopen()
、fread()
和fwrite()
>。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.
http://msdn.microsoft.com/en-us/library/ms810467.aspx has a really good tutorial on using the serial port. Note that you should use the Windows file I/O functions such as
CreateFile()
,ReadFile()
, andWriteFile()
. I'm not sure if it will work to use standard I/O functions such asfopen()
,fread()
, andfwrite()
.Microsoft 提供了一篇包含示例代码的文章,描述了如何在 Win32 下执行此操作。
Microsoft provides an article with sample code describing how to do this under Win32.
Boost:asio 可能会有所帮助作为最近添加的串行设备。
不过,这是公平的警告; 串行端口文档很轻,大概是因为它是相当新的(它是在 asio 1.1.1 中添加的,它包含在 boost 1.36 中)。
但恕我直言,通过 asio 工作是比使用原始 Win32 API 更好的解决方案。 为什么? 它将更容易阅读和维护(它是一个更高级别的 API),并且它将是跨平台的(除非您需要指定特定于操作系统的设备名称)。
Boost - 用户 和 asio.user 邮件列表非常活跃和友好,应该能够帮助您,如果您卡住了。
Boost:asio may be able to help as a serial device was added recently.
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).
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).
The Boost - Users and asio.user mailing lists are quite active and friendly and ought to be able to help you out if you get stuck.
如果使用 .NET 2.0,请参阅 System.IO.Ports 和 这篇文章 应该会有所帮助。 如果直接 Win32,那么Adam 的答案是最好的。
If using .NET 2.0 see System.IO.Ports and this article should be helpful. If direct Win32, then Adam's answer is best.
我相信,如果您发现 VC6 太古老了,您也会发现大量 C# 示例代码。 我认为还有一堆“免费”串行/COM 端口包装器,但我只是在编写 RS232 设备控制器软件时编写了自己的包装器。
谷歌C#和串行端口或rs232
我得到了这些:
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
通过 Google 搜索找到合适的代码应该没有问题。
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.
google C# and serial port or rs232
I got these:
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
You should have no problem finding suitable code with a google search.