串口通信问题(C代码)

发布于 2024-07-20 10:21:01 字数 655 浏览 9 评论 0 原文

我最近尝试使用蓝牙 USB 适配器访问串行通信。 我使用下面的 C 代码,并不断收到错误 5,即“访问被拒绝”。 我是系统的管理员(这似乎是论坛上此问题的常见解决方案),并且没有其他应用程序访问我正在使用的同一端口(也是另一个常见的解决方案)。 我正在 Windows Vista Home Basic 32 位系统上运行。 我想知道是否有人有解决方案 我的C代码是:

HANDLE hComm;

hComm = CreateFile( _T("\\.\COM3"), GENERIC_READ | GENERIC_WRITE, 0, 无效的, OPEN_现有, 0, 无效的);

if (hComm == INVALID_HANDLE_VALUE)
    printf("Error number: %ld\n", GetLastError());
else
    printf("success\n");

I recently tried to get access to a serial communication using a Bluetooth usb dongle.
I used the C code below, and keep getting error 5, which is “Access denied”. I am the administrator for the system (which seemed to be the common solution to this problem on the forums) and no other application is accessing the same port I am using (also another common solution). I’m running on a Windows Vista Home Basic 32bit system. I was wondering if anyone had a solution for this
My C code is:

HANDLE hComm;

hComm = CreateFile( _T("\\.\COM3"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

if (hComm == INVALID_HANDLE_VALUE)
    printf("Error number: %ld\n", GetLastError());
else
    printf("success\n");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

永言不败 2024-07-27 10:21:01

我不知道这是否是您的问题,但我怀疑您需要转义路径中的反斜杠,如下所示: "\\\\.\\COM3"

I don't know if this is your problem or not, but I suspect you need to escape the backslashes in the path, like so: "\\\\.\\COM3"

子栖 2024-07-27 10:21:01

这看起来确实需要再次转义反斜杠。 您还可以使用对象查看器(例如 WinObj (http://technet.microsoft.com/en-us/sysinternals/bb896657.aspx),虽然我不知道WinObj是否可以在Vista上运行。

That does look like you have to escape your backslashes again. You can also verify that the COM port you're targeting exists on your system by using an object viewer, such as WinObj (http://technet.microsoft.com/en-us/sysinternals/bb896657.aspx), although I don't know if WinObj runs on Vista.

我最亲爱的 2024-07-27 10:21:01

根据我的经验,不需要反斜杠

hComm = CreateFile( _T("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

In my experience the backslashes are not needed

hComm = CreateFile( _T("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
念三年u 2024-07-27 10:21:01

感谢您的提示,但事实证明蓝牙密钥设置不正确,因此拒绝访问串行端口。

Thanks for the tips but it turns out the bluetooth passkey was not set properly and therefore it was denying access to the serial port.

悲欢浪云 2024-07-27 10:21:01

只需在代码中将 COM# 替换为 \.\COM# 即可,

  hComm = CreateFile("\\\\.\\COM15",

              GENERIC_READ | GENERIC_WRITE,
              0,
              0,
              OPEN_EXISTING,
              0,
              0);

Just replace your COM# with \.\COM# in your code,

  hComm = CreateFile("\\\\.\\COM15",

              GENERIC_READ | GENERIC_WRITE,
              0,
              0,
              OPEN_EXISTING,
              0,
              0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文