Solved

CreateFile

Posted on 1998-10-16
16
1,313 Views
Last Modified: 2013-12-03
If I open a serial port by CreateFile, do I have to use COM1 or //./COM1 ?

Andrew
0
Comment
Question by:andrewyu
  • 8
  • 8
16 Comments
 
LVL 3

Accepted Solution

by:
stefanr earned 0 total points
Comment Utility
They both work fine for COM1, but if you have more that 9 ports (COM10 and up) you must use the later form ("//./COM10").
0
 

Author Comment

by:andrewyu
Comment Utility
Thank you very much !

Is the following right ?

CreateFile(COM, GENERIC_READ || GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

Anyway, where can I find resources about this issue ?

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
Don't use the || operator. It should be only |.
I.e.
CreateFile(szCOM, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

You should also look up other WIN32 functions like SetCommTimeouts, BuildCommDCB, SetCommState, ReadFile, WriteFile, etc.

My source of information is Microsoft's MSDN. Try their website. It contains tons of information.
0
 

Author Comment

by:andrewyu
Comment Utility
Can I use WriteFile without setting DCB ?

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
Yes. But you can't be sure what the default settings are, or if they could work in your program.
0
 

Author Comment

by:andrewyu
Comment Utility
But, the GetLastError always return 3220713404l ! Why ? What is this? Actually, it was also return before calling CreateFile !

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
That number from GetLastError seem arbitrarily, but maybe it is an COM/OLE error code, or something more obscure. Do you call GetLastError immediately after that the call of CreateFile fails?
0
 

Author Comment

by:andrewyu
Comment Utility
But, my program is running on console !
Yes, I call GetLastError just before and just after CreateFile !

Andrew
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:stefanr
Comment Utility
The error code does just not seem right...

You say that you get an invalid handle value (INVALID_HANDLE_VALUE) returned from CreateFile, and that GetLastError returns 3220713404 (== 0xBFF82FBC). That should be impossible, since it is not defined to be returned by any WIN32 function. COM error codes can be rather high, but they mostly are 0x800XXXXX something. And COM components can be called from a console application. Programatically someone could set the error code themselves by calling SetLastError.

I just don't know what could be happening in your code. Could you maybe show an extract of a few lines around your call of CreateFile?
0
 

Author Comment

by:andrewyu
Comment Utility
NO, I think the CreateFile retrurn a correct handle and it is not INVALID_HANDLE_VALUE.
Actually, the GetLastError still return the same code even  I call SetLastError(0) just before it !

So, do you have any idea about this error code ?

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
No ideas really. Does GetLastError return the same value each time you run the program? Did you call SetLastError(0) from the same thread you call GetLastError? SetLastError/GetLastError only works together for the same thread. But if CreateFile returns a correct handle, you should not have to worry about the error code anyway.
0
 

Author Comment

by:andrewyu
Comment Utility
Yes, it return the same value when I run the console program even I use SetLastError(0) !
Actually, I think my program is not implement with multithread, unless, Visual C++ did it for me !

Although there is no promlem with CreateFile, I have problem with WriteFile(hCOM, data, 2, written, 0) ! Why ? So, I want to know what is this error code !

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
Do you get the same error code when WriteFile fails? That is, when WriteFile returns FALSE. Or is there some other problem, such as written data does not arrive at the COM port, or the lpNumberOfBytesWritten (== written) indicates that 0 bytes was written?
0
 

Author Comment

by:andrewyu
Comment Utility
When I use GetLastError, the error code is still the same as before !

Yes, it return false and written==0 !
BUT, can I write the data that is including ASCII 0, 1, 2 or 3 ?

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
It doesn't matter what bytes the data you are writing contains. It's all binary data to WriteFile. Regardless of what the weird errorcode means, the fact that WriteFile fails and returns FALSE could mean that the port is not setup correctly. Take a look at
the other comm functions (especially SetCommTimeouts, SetCommState, and SetCommMask). There could be some device-specific behaviour lurking somewhere here, if you somehow are not using the standard comm drivers. You could also try
CreateFile(_T("\\\\.\\COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
instead of "//./COM1", if not done already (this is a wild shot).
0
 

Author Comment

by:andrewyu
Comment Utility
I have already finished it in Delphi and it seems ok !

Anyway, thank you very much !

Andrew
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now