?
Solved

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

Posted on 2002-04-11
12
Medium Priority
?
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

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