?
Solved

Reading From Serial Port - Access Denied

Posted on 2005-03-16
7
Medium Priority
?
1,710 Views
Last Modified: 2012-06-27
Hi,

I am using win32 API call to CreateFile in my Delphi app in order to read bytes from the Serial Port COM1. However I get an 'Access Is Denied' error after calling CreateFile.

This is my call:
-------------------------------------------
m_strComName := 'COM1';

m_hCom := CreateFile(PChar(m_strComName),GENERIC_READ , 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
-------------------------------------------

Am I doing anything wrong?  What else could be causing Access Denial to COM1?

Many Thanks,

AT.
0
Comment
Question by:Attack_Trax
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:Voodooman
ID: 13554317
Watching only

Hi

Can't answer your question directly.

However, wouldn't it be easier to use a Comm Control (why reinvent the wheel?).

Working with the Comm Port is very tricky indeed.  I did a lot of work with this a few years ago in VB.  Basically we were dialling up and transferring data to a remote location (no broadband then!).

The Microsoft comm control is free to use with any version of VB and I think also Office.

I used the comm control from http://www.greenleafsoft.com/

Sorry I can't help more directly

Voodooman
0
 
LVL 5

Expert Comment

by:Voodooman
ID: 13554372

Sorry to bother you but just an idea.

You cant use Comm1 and Comm3 together or Comm2 and Comm4 together ( I stand to be corrected on this but I clearly remember that we used to have problems installing modems if this was attempted). If you are already using comm3 try setting up to use comm4 etc

Make sure your commports are correctly configured by checking the settings in control panel against the BIOS. Comm Ports are sometimes configured here.



Also, check the 'system ' settings to make sure your ports are working properly - plug an old serial mouse into the port and see if its working properly.

Voodooman
0
 

Author Comment

by:Attack_Trax
ID: 13554475
Hi,

Thanks for the posts. I've gone with Win32 API as I am porting some code I've written in C++. The Object relies on the CComPort C++ class which I have just converted into Delphi, hence I'd rather stick with the API calls for the time being..

Thanks,

AT.

0
Independent Software Vendors: 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!

 
LVL 17

Assisted Solution

by:mokule
mokule earned 80 total points
ID: 13554495
My opinion is that
some other device is using COM1 or
You are trying to open it twice

mokule
0
 
LVL 17

Expert Comment

by:mokule
ID: 13554550
I'm using it this way many years with no problem

var
  dcbPort:TDCB;
begin
  result := true;
  if hPort = INVALID_HANDLE_VALUE then
    begin
    result := False;
    hPort := CreateFile( Port,
                      GENERIC_READ or GENERIC_WRITE,
                      0, nil,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL,
                      LongInt(0));
    if hPort <> INVALID_HANDLE_VALUE then
      begin
      if GetCommState(hPort, dcbPort) then
        begin
            { fill in the fields of the structure }
        dcbPort.BaudRate := Body;
        dcbPort.ByteSize := Bity;
        dcbPort.Parity := Parz;
        dcbPort.StopBits := Stop;
        dcbPort.Flags := 0;
            { flag bit fields:
            dcb_Binary, dcb_Parity, dcb_OutxCtsFlow, dcb_fOutxDsrFlow,
            dcb_fOutX, dcb_fInX, dcb_DtrFlow, dcb_RtsFlow
            }
        SetCommState(hPort, dcbPort);
        SetReadTimeouts(hPort);
        SetupComm(hPort,1200,1200);
        result := true;
        end;
    end;
  end
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 120 total points
ID: 13555492
Check you can open it using, say, Hyperterminal - to make sure nothing else is using it..
0
 

Author Comment

by:Attack_Trax
ID: 13556337
Just checkedwith Hyper Terminal and ActiveSync (I'd forgotten it was even installed) has been holding COM1.

With ActiveSync disabled, my code now Connects/Disconnects fine.

Many Thanks,

AT.
0

Featured Post

Independent Software Vendors: 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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