Link to home
Start Free TrialLog in
Avatar of Attack_Trax
Attack_Trax

asked on

Reading From Serial Port - Access Denied

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.
Avatar of Voodooman
Voodooman

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

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
Avatar of Attack_Trax

ASKER

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.

SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.