如何打开COMXX文件(串口) [英] How to open COMXX files(serial port)

查看:586
本文介绍了如何打开COMXX文件(串口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有串口的问题。

我写了一个程序,可以读取COM1到COM9端口,但可以打开COMXX(像com10,com11,等等)

I've written a program that can read ports COM1 to COM9, but can open COMXX(like com10, com11, etc.)

我搜索并得知tCOM1-COM9是NT命名空间中保留名称的一部分。
它说,要访问文件像COMXX你需要写\.\COMXX。

I've searched and learnt that tCOM1–COM9 are part of the reserved names in the NT namespaces. it says that "To acces files like COMXX you need to write "\.\COMXX".

我试过这个,但didn'

I've tried this, but didn't make it.

有人有想法吗?
我最好的问候...

Does anybody have an idea? My best regards...

下面的代码由于此问题返回无效的句柄值:

myPort = CreateFile("\\.\COM14",
              GENERIC_READ | GENERIC_WRITE,
              0,    /* exclusive access  */
              NULL, /* no security attrs */
              OPEN_EXISTING,
              0,
              NULL );


推荐答案

您需要在文件名参数中转义反斜杠:

You need to escape the backslashes in the filename parameter:

myPort = CreateFile("\\\\.\\COM14",
              GENERIC_READ | GENERIC_WRITE,
              0,    /* exclusive access  */
              NULL, /* no security attrs */
              OPEN_EXISTING,
              0,
              NULL );

您还试图打开COM端口14.您的机器上真的有14个COM端口?尝试 COM1

You're also trying to open COM port 14. Do you really have 14 COM ports on your machine? Try COM1 instead.

这篇关于如何打开COMXX文件(串口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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