473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

CreateFile to open a COM port woes

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.

Apr 18 '07 #1
5 12492
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.
Apr 18 '07 #2
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.
"Dick Grier" wrote:
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.
Apr 18 '07 #3
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.
Apr 19 '07 #4
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.
"Dick Grier" wrote:
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.
Apr 19 '07 #5
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.
Apr 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and...
3
by: Antoine | last post by:
Hello, I'm writing a program to send requests to my wlan pocket pc device (UIO1: driver) in C#. Here how I import CreateFile functions from coredll.dll with DllImport: public static extern...
3
by: red | last post by:
I have this: using System; using System.Runtime.InteropServices; using System.Text; class FileReader { const uint GENERIC_READ = 0x80000000; const uint OPEN_EXISTING = 3;
0
by: Steve N. | last post by:
I'm looking to do some basic (to begin with anyway) Serial IO in a __gc class. I use this code open a port: m_hSerialComm = CreateFile(LpstrPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING,...
5
by: Andrew Clark | last post by:
Hello, Thanks for all replies on this subject. I still cannot get CreateFile to retun a good value though. I went to PInvoke.net and saw the VB.NET declaration of this function: ...
0
by: JohnnyBoy | last post by:
I am playing with a Microsoft code example that uses the creatFile call to open a stream to a serial COM port. When I execute the example, the createFile call fails with an error code 5...
3
by: Scott Bell | last post by:
Hello all, What is the equivalent in the .NET Framework for accessing a device at the block/sector level such as one would when using CreateFile on a device like "\\.\D:"? Attempting to create...
2
by: Kerem Gümrükcü | last post by:
Hi all, sorry for Crossposting, but i dont know whether someone can help me here. If i remember right, there was a Class or a Member function in the .NET Framework that could do mostly the same...
11
by: sevak316 | last post by:
Trying to open COM1 in XP using CreateFile and I keep getting INVALID_HANDLE_VALUE. here is the code I am using: //main ............. ..... .... .. .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.