?
Solved

CreateFile

Posted on 1998-10-16
16
Medium Priority
?
1,358 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
ID: 1415309
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
ID: 1415310
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
ID: 1415311
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:andrewyu
ID: 1415312
Can I use WriteFile without setting DCB ?

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1415313
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
ID: 1415314
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
ID: 1415315
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
ID: 1415316
But, my program is running on console !
Yes, I call GetLastError just before and just after CreateFile !

Andrew
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1415317
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
ID: 1415318
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
ID: 1415319
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
ID: 1415320
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
ID: 1415321
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
ID: 1415322
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
ID: 1415323
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
ID: 1415324
I have already finished it in Delphi and it seems ok !

Anyway, thank you very much !

Andrew
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

621 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