如何调用成功的串行通信createfile? [英] How do I call successful createfile of serial communications?

查看:71
本文介绍了如何调用成功的串行通信createfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用VC ++进行串口通信的第一个代码中



我尝试调用CreateFile,如下所示:



m_hcomm = CreateFile(L///+ Portname,

GENERIC_READ | GENERIC_WRITE,

0,0,OPEN_EXISTING,0,0);



犯了错误m_hcomm = INVALID_HANDLE_VALUE。



现在

电脑有一个串口端口并用RS232电缆与另一个PC端口连接。

(调用CreateFile时这是必需条件吗?)



最后一个错误代码= 2.



因为我是串口通讯的新手。我该如何处理代码或环境?



提前感谢。



什么我试过了:



这个问题浪费了2天。

解决方案

我的提示是,Portname字符串在某种程度上是不正确的。 (我会在没有斜线和点的情况下使用它)在系统设置中查看端口可用以及端口的正确名称是什么。



阅读 Micorsoft文档,包含一些有趣的代码片段。



阅读本文关于在Win32上创建串行通信


错误代码2是 ERROR_FILE_NOT_FOUND (系统找不到指定的文件。。



您正在尝试使用文件名前缀来支持COM端口> = 10.但是这些使用反斜杠(\\.\)而不是正斜杠(//./) 。所以试试这个:

 m_hcomm = CreateFile(L\\\\.\\+ Portname,
GENERIC_READ | GENERIC_WRITE,
0,0,OPEN_EXISTING,0,0);



如果仍然出错,请检查端口名称是否有效且端口是否存在。


In my first code of serial communications with VC++

I try to call CreateFile like as follows:

m_hcomm = CreateFile(L"//./" + Portname,
GENERIC_READ | GENERIC_WRITE,
0,0, OPEN_EXISTING, 0,0);

made error m_hcomm = INVALID_HANDLE_VALUE.

Now
The pc has one serial port and connected with RS232 cable with another port of PC.
(Is this required condition when call CreateFile?)

The Last error code = 2.

As I am novice of serial comm. What do I take care of code or environment?

Thank in advance.

What I have tried:

More 2 days wasted for this problem.

解决方案

My tip is, that the Portname string is somehow incorrect. (I would use it without slashes and dot) Look in the system settings that the port is available and what the correct name of the port is.

Read the Micorsoft documentation with some interesting code snippets.

Read this article about Creating a Serial communication on Win32


Error code 2 is ERROR_FILE_NOT_FOUND ("The system cannot find the file specified.").

You are trying to use the file name prefix to support COM ports >= 10. But these use back slashes ("\\.\") instead of forward slashes (//./). So try this:

m_hcomm = CreateFile(L"\\\\.\\" + Portname,
    GENERIC_READ | GENERIC_WRITE,
    0,0, OPEN_EXISTING, 0,0);


If you still get an error check if the port name is valid and the port exists.


这篇关于如何调用成功的串行通信createfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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