USB转串口:设备无法打开

nono_bill 2010-04-24 05:16:16
我的机器上没有串口,用现在上面接的是两个USB转串口线(装好驱动后,设备管理器里显示的是COM11和COM13)。之前我用两个USB转串口线对接起来,两头用超级终端收发数据正常。现在我写了如下代码测试打开串口:


#include <Windows.h>
#include <cstdio>

int main()
{
HANDLE hd = CreateFileA ("COM13",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);

if (hd == INVALID_HANDLE_VALUE) {
fprintf (stderr, "COM13: %d\n", GetLastError() );
return -1;
}
else {
fprintf (stdout, "COM13: open device successfully.\n");
}

return 0;
}



结果显示,串口打开
失败,错误ID是:ERROR_FILE_NOT_FOUND。初次编写串口程序,不知道什么原因,请各位指点。先行谢过。
...全文
468 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Python 2010-04-27
  • 打赏
  • 举报
回复
学习了~~~
尹成 2010-04-27
  • 打赏
  • 举报
回复
学习LZ了,有探索精神,顶上!~
xianzi123 2010-04-27
  • 打赏
  • 举报
回复
学习了,原来大于10的串口号要这样写啊!
nono_bill 2010-04-24
  • 打赏
  • 举报
回复
原因已找到。

http://www.advancedvirtualcomport.com/faq.html

How do I open a virtual serial port with a number higher than 9 from my application?
Windows automatically maps COM1-COM9 ports to the right device name. COM ports with higher numbers should be opened as \\.\COMxxx, where xxx is a port number.

Example for C/C++/C#:
hPort = CreateFile("\\\\.\\COM15", ...);

Example for VB:
hPort = CreateFile("\\.\COM15", ...);

Example for Delphi:
hPort := CreateFile('\\.\COM15', ...);
nono_bill 2010-04-24
  • 打赏
  • 举报
回复
原因已找到。

http://www.advancedvirtualcomport.com/faq.html

How do I open a virtual serial port with a number higher than 9 from my application?
Windows automatically maps COM1-COM9 ports to the right device name. COM ports with higher numbers should be opened as \\.\COMxxx, where xxx is a port number.

Example for C/C++/C#:
hPort = CreateFile("\\\\.\\COM15", ...);

Example for VB:
hPort = CreateFile("\\.\COM15", ...);

Example for Delphi:
hPort := CreateFile('\\.\COM15', ...);

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧