简体   繁体   English

串口C ++问题

[英]Serial port C++ problem

I have a problem connecting to any serial port (want to exchange with an Arduino) using c++ on visual studio. 我在Visual Studio上使用C ++连接到任何串行端口(想与Arduino交换)时遇到问题。

I'm using the SerialClass given in the Arduino playground, but can't open an handle to my port. 我正在使用Arduino运动场中提供的SerialClass ,但无法打开端口的句柄。 It throws ERROR_FILE_NOT_FOUND on every try.. 每次尝试都会引发ERROR_FILE_NOT_FOUND。

I've been testing many other ways of notating the port: 我已经测试了许多其他标记端口的方法:

"COM1"
"COM1:"
"\\.\COM1"

but nothing's working. 但没有任何效果。

The port is availiable on the system, and working fine using Putty or the Arduino IDE. 该端口在系统上可用,并且可以使用Putty或Arduino IDE正常工作。 Additionally it isn't blocked by other processes. 此外,它不会被其他进程阻止。

I'm using Windows 7 x64, but project is Win32. 我正在使用Windows 7 x64,但项目是Win32。

The class is constructed with 该类的构造

serialPort = new Serial((char*)port);

and the error string shows the right portname. 错误字符串显示正确的端口名。

What I'm doing wrong? 我做错了什么?

File opening isn't working, too. 文件打开也不起作用。

A good example of others manually using the api to open a serial port is here . 其他使用手动API来打开一个串行端口的一个很好的例子是在这里 The relevant part (regarding your question) shows them using the port number as follows: 相关部分(关于您的问题)使用以下端口号显示它们:

...
CreateFile("\\\\.\\COM1",GENERIC_WRITE
...

so it could be \\\\\\\\.\\\\COM1 instead of the options you have tried. 因此它可能是\\\\\\\\.\\\\COM1而不是您尝试过的选项。

Found the Answer! 找到答案了!

I casted the string using (LPCTSTR), but this wouldn't return a valid string. 我使用(LPCTSTR)强制转换了字符串,但这不会返回有效的字符串。 Had to use 不得不使用

const WCHAR FileFullPath[] = {L"COM4"} ;

for conversion. 进行转换。

您确定这不是Windows 7 64位驱动程序问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM