Solved

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

Posted on 2002-04-11
12
325 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Activex loadlibrary and show parented form issue 6 286
Sending Gmail through Delphi 3 91
Delphi inherited method 6 63
JAudiorecorder record freezing the app 29 60
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

14 Experts available now in Live!

Get 1:1 Help Now