windows 如何使用 Win32 API 与 com 端口 (RS232) 通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5345077/
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
How to use Win32 API to talk to a com port (RS232)
提问by Alfred Zhong
I am trying to use win32 API to talk to a com port I found this http://www.robbayer.com/files/serial-win.pdf
我正在尝试使用 win32 API 与我发现的 com 端口通信 http://www.robbayer.com/files/serial-win.pdf
hSerial = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
I use VS2008 and it complains error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR'
我使用 VS2008,它抱怨错误 C2664:'CreateFileW':无法将参数 1 从 'const char [5]' 转换为 'LPCWSTR'
OK, I guess it doesn't like "COM1" to be char* type,
好吧,我猜它不喜欢“COM1”是 char* 类型,
I tried casting it LPCWSTR("COM1"), then it compiles with no problem.
我尝试将它转换为 LPCWSTR("COM1"),然后它编译没有问题。
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?
但是,它返回“错误打开串行端口-1”,因此它没有成功找到com端口。我想直接铸造不是正确的方法吗?
Please tell me what I should do to make this work.
请告诉我我应该怎么做才能完成这项工作。
the msdn is not that helpful http://msdn.microsoft.com/en-us/library/ms810467.aspx
msdn 不是很有帮助 http://msdn.microsoft.com/en-us/library/ms810467.aspx
I don't know what the "gszPort" means there
我不知道那里的“gszPort”是什么意思
回答by Michael
Try _T("COM1")
instead of LPCWSTR("COM1")
. It's called Generic-Text Mapping. I don't actually know whether that is the problem, but using _T
is the right way to it.
尝试_T("COM1")
代替LPCWSTR("COM1")
. 它被称为通用文本映射。我实际上不知道这是否是问题所在,但使用_T
是正确的方法。
Also, when Windows API calls return an error condition (like -1) you can use the Last-Error Code (using GetLastError
and FormatMessage
)to get a more detailed description of the error.
此外,当 Windows API 调用返回错误条件(如-1)时,您可以使用上次错误代码(使用GetLastError
和FormatMessage
)来获取更详细的错误描述。
回答by Adrian McCarthy
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:
对于 Unicode 构建,CreateFile
映射到CreateFileW
期望“宽”字符串。您可以通过在字符串常量前加上 L 来解决当前的问题,如下所示:
CreateFile(L"COM1", ...);
Some people would suggest explicitly using the wide version:
有些人会建议明确使用宽版:
CreateFileW(L"COM1", ...);
Or you can explicitly use the "ANSI" version, even in a Unicode build:
或者您可以显式使用“ANSI”版本,即使在 Unicode 版本中:
CreateFileA("COM1", ...);
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.
如果您希望能够构建 Unicode 和 ANSI 构建,您可以使用包含 L 前缀的宏。这个宏有两个版本: TEXT(x)
和_T(x)
。如果我没记错的话,前者来自 Windows API via <tchar.h>
,后者来自 Microsoft 的 C 运行时库实现。由于这是一个 Windows API,我会使用该TEXT
版本。
CreateFile(TEXT("COM"), ...);
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.
如今,保持对 ANSI 的向后兼容性可能不值得。过去十年中发布的所有 Windows 版本在内部都使用 Unicode,因此如果您尝试使用 ANSI 版本,字符串将在运行时被加宽。所以我不会担心宏,只是L
在非常特殊的情况下为字符串文字加上前缀。