如何使用 Win32 API 与 com 端口 (RS232) 通信 [英] How to use Win32 API to talk to a com port (RS232)

查看:123
本文介绍了如何使用 Win32 API 与 com 端口 (RS232) 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 win32 API 与 com 端口通信我找到了这个http://www.robbayer.com/files/serial-win.pdf

I am trying to use win32 API to talk to a com port I found this http://www.robbayer.com/files/serial-win.pdf

hSerial = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

我使用 VS2008 并且它抱怨错误 C2664:CreateFileW":无法将参数 1 从const char [5]"转换为LPCWSTR"

I use VS2008 and it complains error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR'

好吧,我猜它不喜欢将COM1"设为 char* 类型,

OK, I guess it doesn't like "COM1" to be char* type,

我尝试将它转换为 LPCWSTR("COM1"),然后它编译没有问题.

I tried casting it LPCWSTR("COM1"), then it compiles with no problem.

然而,它返回ERROR opening serial port -1",因此没有成功找到com端口.我想直接投射不是正确的方法吗?

However, it returns "ERROR opening serial port -1", so it doesn't find the com port successfully. I guess direct casting is not the right way?

请告诉我我应该怎么做才能完成这项工作.

Please tell me what I should do to make this work.

msdn 不是很有帮助http://msdn.microsoft.com/en-us/library/ms810467.aspx

the msdn is not that helpful http://msdn.microsoft.com/en-us/library/ms810467.aspx

我不知道那里的gszPort"是什么意思

I don't know what the "gszPort" means there

推荐答案

对于 Unicode 版本,CreateFile 映射到 CreateFileW 需要宽"字符串.您可以通过在字符串常量前加上 L 来解决当前的问题,如下所示:

For a Unicode build, CreateFile maps to CreateFileW which expects "wide" character strings. You can solve the immediate problem by prefixing your string constant with L, like this:

CreateFile(L"COM1", ...);

有些人会明确建议使用宽版:

Some people would suggest explicitly using the wide version:

CreateFileW(L"COM1", ...);

或者您可以明确使用ANSI"版本,即使在 Unicode 版本中:

Or you can explicitly use the "ANSI" version, even in a Unicode build:

CreateFileA("COM1", ...);

如果您希望能够构建 Unicode 和 ANSI 构建,您可以使用包含 L 前缀的宏.这个宏有两个版本:TEXT(x)_T(x).如果我没记错的话,前者通过 <tchar.h> 来自 Windows API,而后者来自 Microsoft 的 C 运行时库实现.由于这是一个 Windows API,我会使用 TEXT 版本.

If you want to be able to build Unicode and ANSI builds, you can use a macro that optionally includes the L prefix. There are two versions of this macro: TEXT(x) and _T(x). If I recall correctly, the former comes from the Windows API via <tchar.h> and the latter comes from Microsoft's implementation of the C runtime library. Since this is a Windows API, I'd use the TEXT version.

CreateFile(TEXT("COM"), ...);

如今,保持对 ANSI 的向后兼容性可能不值得.过去十年中发布的所有 Windows 版本都在内部使用 Unicode,因此如果您尝试使用 ANSI 版本,字符串将在运行时被加宽.所以我不会担心宏,只用 L 前缀字符串文字,除非在非常特殊的情况下.

Nowadays, it's probably not worth keeping the backward compatibility for ANSI. All versions of Windows released in the past decade use Unicode internally, so if you try to use the ANSI versions, the strings are going to be widened at run time. So I wouldn't worry about the macros and just prefix string literals with L except in very special circumstances.

这篇关于如何使用 Win32 API 与 com 端口 (RS232) 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆