Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading From Serial Port - Access Denied

Posted on 2005-03-16
7
Medium Priority
?
1,781 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
  • 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
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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
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…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

571 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