Look RS232 > RS232C Hardware  
fCoder SIA
fCoder SIA Look RS232 Download Order Support Cookie Policy
Look RS232
Download
Order
Support
Testimonials
FAQ
Online help
Modem Programming
Serial port
RS232 Hardware
Programming
  Open and Close Port

Opening a port is actually getting the descriptor of the serial port. Due to API it can be done by using CreateFile function. This function results in the creation of a file with a reserved name.It is important when getting access to the corresponding port or device.After the descriptor has been obtained the work with the port is carried out the same way it is with files.

Let's examine CreateFile function. Its parameters are listed below.

Function syntax:

HANDLE CreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDistribution, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);

Parameters specifications:

lpFileName - pointer to a null-terminated string which defines the object name (in our case it is "COMx", where x is the system port number, or "\\\\.\\COM1" - if there's a need to open the port with its number above 9);

dwDesiredAccess - defines the type of the access to the object. The application can get 'read'-access, 'write'-access, 'read/write'-access or device request access. This parameter can use any combination of the following values:

  • 0 - specifies that the device request refers to an object. The application can make a request of the device attributes without referring to the device.

GENERIC_READ - specifies the 'read' access from the object. The data can be read from the file and the file pointer can be relocated.

GENERIC_WRITE - specifies the 'write' access to the object. The data can be written to the file and the pointer can be relocated.

  • Combination of GENERIC_WRITE and GENERIC_READ grants 'read/write' access to the object.

dwShareMode - a set of bit flags which specifies shared access to the object. If dwShareMode - 0, then access to the object cannot be shared. Further object opening operations will be rejected until the descriptor is closed. To use the object in the shared mode use the combination of the following values:

  • FILE_SHARE_DELETE - Windows NT only: Subsequent open operations on the object will succeed only if delete access is requested.
  • FILE_SHARE_READ - Subsequent open operations on the object will succeed only if read access is requested.
  • FILE_SHARE_WRITE - Subsequent open operations on the object will succeed only if write access is requested. Add _READ to allow read-write access.

lpSecurityAttributes - pointer to the SECURITY_ATTRIBUTES structure which specifies if the descriptor returned can be inherited by the child procedures. If lpSecurityAttributes is NULL, the descriptor cannot be inherited.

Windows NT: lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure defines the protection descriptor for the object. If lpSecurityAttributes is NULL the object gets the default protection descriptor. The system of the end file must support the protection on files and folders for this parameter to apply this effect to the files.

Windows 95: lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure is ignored.

DwCreationDistribution - defines the ways of opening existing files and the actions taken if the file doesn't exist. This parameter must contain one of the following values:

  • CREATE_NEW - creates a new file. The function fails if the specified file already exists.
  • CREATE_ALWAYS - creates a new file. If the file already exists the function replaces it with a new one.
  • OPEN_EXISTING - opens a file. Fails if the file doesn't exist.
  • OPEN_ALWAYS - opens the file if it exists. If the file doesn't exist the function creates a file just like CREATE_NEW.
  • TRUNCATE_EXISTING - opens the file. After the file is opened it is reduced until it's of zero size. The request process must open the file with at least GENERIC_WRITE access. Fails if the file doesn't exist.

dwFlagsAndAttributes - defines the file attributes and flags. Any combination of the following attributes except the FILE_ATTRIBUTE_NORMAL attribute is possible:

  • FILE_ATTRIBUTE_ARCHIVE - the file must be archived. The applications use this attribute to mark the file for reserve copying or deletion.
  • FILE_ATTRIBUTE_COMPRESSED - file or catalogue is compressed . If it's a file, it means that all data in the file are compressed. If it's a catalogue it means that compression is the default value for recently created files and subdirectories.
  • FILE_ATTRIBUTE_HIDDEN - the file is hidden. It shouldn't be included into the usual directory list.
  • FILE_ATTRIBUTE_NORMAL - the file has no attributes.
  • FILE_ATTRIBUTE_OFFLINE - the data of the file are not available at the moment. It shows that the data of the file have been moved to an off-line archive.
  • FILE_ATTRIBUTE_READONLY - the file is read-only. The applications can read the file but cannot write or delete the data.
  • FILE_ATTRIBUTE_SYSTEM - a part of the file is used by the operation system only.
  • FILE_ATTRIBUTE_TEMPORARY - the file is used for temporary data storing. File systems try to keep all data in memory for quicker access rather than flushing the data back to mass archive. A temporary file should be deleted by the application as soon as it is no longer needed.

Also possible is any combination of the following flags:

  • FILE_FLAG_WRITE_THROUGH
  • FILE_FLAG_OVERLAPPED
  • FILE_FLAG_NO_BUFFERING
  • FILE_FLAG_RANDOM_ACCESS
  • FILE_FLAG_SEQUENTIAL_SCAN
  • FILE_FLAG_DELETE_ON_CLOSE
  • FILE_FLAG_BACKUP_SEMANTICS
  • FILE_FLAG_POSIX_SEMANTICS

hTemplateFile - specifies that the descriptor with GENERIC_READ refers to a temporary file. The temporary file supports the file attributes and enhanced attributes for the created file.

To open the communication ports the CreateFile function can create a descriptor for the port like COM1serial port. For the ports dwCreationDistribution parameter should be OPEN_EXISTING, hTemplate parameter should be NULL. Read access, write access or read/write access can be specified; the descriptor should also be open for overlapped I/O.

If the port was opened successfully the function returns the handle descriptor to work with further on. If the port wasn't opened successfully the function wil return INVALID_HANDLE_VALUE.

CloseHandle function is used to close the port.

Function syntax:

BOOL CloseHandle(HANDLE hFile};

Parameters specifications:

hFile � the descriptor of the open file of communication port

Example of getting a descriptor of the serial port COM1:

HANDLE hCOM=CreateFile("\\\\.\\COM1",GENERIC_WRITE,0,NULL,OPEN_EXISTING,

FILE_FLAG_OVERLAPPED,NULL);

if (hCOM!=INVALID_HANDLE_VALUE)

{

...

CloseHandle(hCOM);

}

else cout << "Error Open" << endl;

 
  Contact us
© fCoder SIA