2
votes

I have a problem connecting to any serial port (want to exchange with an Arduino) using c++ on visual studio.

I'm using the SerialClass given in the Arduino playground, but can't open an handle to my port. It throws ERROR_FILE_NOT_FOUND on every try..

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. Additionally it isn't blocked by other processes.

I'm using Windows 7 x64, but project is 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.

3
you will probably also want to extend that class to be able to modify the ports settings once you fix your error. It currently doesn't have a way to programmatically configure the port, and all settings are hard coded. The data read/sent will possibly not mesh with w/e you're trying to connect to. Here is also a good example (RIGHT from MS) on how to utilize their .NET framework which has a serial port class directly available. msdn.microsoft.com/en-us/library/… - g19fanatic
If you're looking for an alternative but still using c++, you could try Qt and use the open sourced QextSerialPort library. I've had some VERY good success using it and it has a very small learning curve, considering most of the examples are easily c/p'able to what you're trying to accomplish. - g19fanatic

3 Answers

4
votes

A good example of others manually using the api to open a serial port is here. 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.

1
votes

Found the Answer!

I casted the string using (LPCTSTR), but this wouldn't return a valid string. Had to use

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

for conversion.

0
votes

Are you sure this isnt a driver problem with Windows 7 64 bit ?