CreateFile to open a COM port woes

G

Guest

Been struggling with the code below for the last couple of days:

m_hSerialHandle = CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED*/0, NULL);


With VC++6.0, this executes fine and I am able to establish a
connection w/ a device via COM1 however I recently installed VC++8
(.net 2005) and this code no longer works. It last error to
ERROR_INVALID_NAME. Any recommendations will be greatly appreciated.


Thanks in advance.
 
D

Dick Grier

Hi,

I have code examples in my book for VB. However, why not simply use the
built-in SerialPort class (System.IO.Ports)?

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
G

Guest

Hi Mr. Grier,

Thank you for the recommendation and appreciate your time. I am not familiar
with the new .NET constructs I'm afraid. In fact I'm considered a newbie. I'm
simply maintaining existing code. And have been tasked to migrate to the new
development suite. Any reason why the following code will no longer work
under .NET? And how would I resolve this w/out having to rewrite (hopefully
not) existing code?

Lastly could you provide a code snippet from your book that demonstrates it,
albeit in VB?

Thank you very much.
 
D

Dick Grier

Hi,

Your declare:

m_hSerialHandle = CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED*/0, NULL);

Perhaps it should be something like this:

m_hSerialHandle = CreateFile(devname, GENERIC_READ | GENERIC_WRITE,
0, 0, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED*/0, 0);

where devname is a string that looks like this:

devname = "\\.\COM1";

Note, this is a little different than your code. At least, this is the
declaration (variation) that I have used for a number of years.

Of course, this assumes that Com1 exists on you PC. Otherwise, create an
appropriate string for the actual port to be opened.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
G

Guest

Hi Mr Grier,

That did not work as well. It failed with the following error: ENOENT or "No
such file or directory". I did however find that there is an ANSI
implementation of CreateFile namely CreateFileA() which ultimately allowed me
to open the COM port and leave the application in unmanaged C++. Do you have
any insight as to why the executable using CreateFile worked when compiled w/
VC++6.0 but failed and needed to use CreateFileA() with dot NET 2005?

Thank you again for taking the time to answer my questions.
 
D

Dick Grier

Hi,
Do you have
any insight as to why the executable using CreateFile worked when compiled
w/
VC++6.0 but failed and needed to use CreateFileA() with dot NET 2005?
<<

No idea. I only write managed applications (thus the SerialPort obect that
I suggested, which would take 10-minutes, tops, to get working). CreateFile
works fine for me. Here is the declaration that I use (VB):

<DllImport("Kernel32")> Private Shared Function CreateFile( _

ByVal FileName As String, _

ByVal dwDesiredAccess As Integer, _

ByVal dwShareMode As Integer, _

ByVal SecurityAttributes As Integer, _

ByVal dwCreationDisposition As Integer, _

ByVal dwFlagsAndAttributes As Integer, _

ByVal hTemplateFile As Integer) As Integer

End Function

Dick
--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top