Solved

Why this Win API serial port access doesn't work under W2k?

Posted on 2002-04-11
12
327 Views
Last Modified: 2010-04-04
I need to write to and read from the COM port. I have the following code which works under Win98, but fails under Win2k. What on earth I'm doing wrong?

Thank you for you help,
Levente

--------------------------------------------------------
function TComport.Open(const ComPort: String): boolean;
var DCB : TDCB;
begin
   Handle := CreateFile( PChar('\\.\' + ComPort),
                         GENERIC_READ or GENERIC_WRITE,
                         0,
                         nil,
                         OPEN_EXISTING,
                         FILE_FLAG_OVERLAPPED,
                         0);

   DCB.DCBlength := sizeof(TDCB);
   GetCommState(Handle, DCB);
   DCB.BaudRate := CBR_9600;
   SetCommState(Handle,DCB);
end;

procedure TComPort.Close;
begin
  CloseHandle(Handle);
end;

procedure TComPort.WriteStr(const Buffer: string);
var WriteBuffer:  array [0..15] of char;
    CharsToWrite, CharsWritten: cardinal;
begin
   CharsToWrite:= length(buffer);
   move(Buffer[1], WriteBuffer, CharsToWrite);
   WriteFile(Handle, WriteBuffer, CharsToWrite, CharsWritten, NIL);
end;

procedure TComPort.ReadStr(var Buffer: string; const CharToRead: Word);
var CharsRead: Cardinal;
    ReadBuffer: array [0..15] of char;
begin
  ReadFile( Handle, ReadBuffer, CharToRead, CharsRead, NIL);
  Buffer:= ReadBuffer;
end;

0
Comment
Question by:Levente
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 6933617
I have a component called Async32, it works fine for me for more than 2 years without any problems, and it is very easy to use.

Tell me your E-Mail and I can send it to you if you want.
Motaz
0
 
LVL 6

Expert Comment

by:zebada
ID: 6933788
Change FILE_FLAG_OVERLAPPED to FILE_ATTRIBUTE_NORMAL.

I don't know why/if it works on Win95 but if you open a com port with FILE_FLAG_OVERLAPPED you MUST supply a valid pointer to an overlapped structure as the last parameter to readfile and writefile. Since you are supplying a NIL pointer you should not have opened the file with FILE_FLAG_OVERLAPPED.
0
 

Author Comment

by:Levente
ID: 6933899
Motaz,

I'd appreciate if you sent me that component to slv@freemail.hu. It may give me hints.

zebada,

I tried FILE_ATTRIBUTE_NORMAL but it still fails under Win2K.

Levente
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Expert Comment

by:SteveWaite
ID: 6934217
You do not say in what way it goes wrong!

Maybe you need to call:
SetupComm(Handle, RxBufferSize, TxBufferSize);
To ensure the buffer sizes are OK.
Or if you do not get data in/out:
NT/Win2k has a different default serial port setting for handshaking. Since you do not setup handshaking in your code the settings the com port had last are used. So you may need you to wire the cable slightly differently: i.e. connect DTR to DSR (at the plug on the NT machine) to get NT/Win2k to enable the port.
Let me know.....
 
0
 

Author Comment

by:Levente
ID: 6944327
I tried SetupComm, but it doesn't work under W2k.

Is there any tool which catches and logs the bytes sent to the serial port? This way I may be able to debug what's going wrong.

Thanks,
Levente
0
 
LVL 6

Expert Comment

by:zebada
ID: 6944332
0
 

Expert Comment

by:TomazB
ID: 6945824
Hi!

Like zebada already said you have problem in FILE_FLAG_OVERLAPPED. I tried your code and on my
Win2000 it definitively works. Try again nad change to FILE_ATTRIBUTE_NORMAL.

It should be

Handle := CreateFile( PChar('\\.\' + ComPort),
                        GENERIC_READ or GENERIC_WRITE,
                        0,
                        nil,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        0);

If ComPort is 'Com1' it is opened and it works for read and write. About your read procedure > Before open and close port you should always set handle to INVALID_HANDLE_VALUE and always check it before read or write. Also before read it is good to check how much to read. Perhaps like this.

function GetInCount:LongInt;
var statPort:TCOMSTAT;
    dwErrorCode:DWORD;
begin
 Result:=0;
 if Handle<>INVALID_HANDLE_VALUE then
  begin
   ClearCommError(Handle,dwErrorCode,@statPort);
   Result:=statPort.cbInQue;
  end;
end;

I am doing it like this for 5 years on NT and Win2000 mascines without problems, except that for ports beyond 8 I use sintaks '//./COM'+ value of port.
Good luck!
0
 

Author Comment

by:Levente
ID: 6965329
TomazB,

Thank you for your help. I tried your code and the result is that GetInCount function returns 0, even under Win98.

What device do you attach to the COM port to test your code? I'm using a proprietary hardware device (a friend of mine has made a device to control stepping motors), so is it possible that our device works in a way that Win2k cannot read data from it?

Thank you,
Levente
0
 

Accepted Solution

by:
TomazB earned 300 total points
ID: 6965829
Hello!

Well, actually I comunicate with annother PC while testing communication of my programs. I didn't have any problems with palette of PLCs and fire protection units which can communicate trough serial port. I you have problems it is possible that some flags of TDCB structure you set during initialisation of serial port are not properly set. Are you
sure that your device actualy send any characters to port? Check flag settings and control lines CTS, RTS, DTR and DSR if you use them. Serial port is standard device and if you made your device inside standards there should be no problems.

0
 

Expert Comment

by:FJani
ID: 8286914
Szia!

Azt hiszem nem tévedek a neveddel kapcsolatban, úgyhogy írok magyarul inkább, mint angolul.
Nagyon nagy gondban vagyok, szakdolgozathoz kellene a soros port kezelés. Olvastam a te régi problémádat, hogy winNT alatt ment a programod, Win9x alatt nem. Nekem ez pont fordítva van! :-( Tudnál segíteni?

Üdv:
Jani
trantor@europe.com
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9003138
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from TomazB

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004737
Thank you for your response

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 170
select query - oracle 16 100
PHP preg_replace code convert to Delphi 14 55
Wincontrol not (correctly) drawn 15 38
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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