Hardware PropertiesInterfacing devices
Types of UARTs
Loop Back Plug
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.
HANDLE CreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDistribution, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
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:
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.
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:
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:
dwFlagsAndAttributes - defines the file attributes and flags. Any combination of the following attributes except the FILE_ATTRIBUTE_NORMAL attribute is possible:
Also possible is any combination of the following flags:
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.
BOOL CloseHandle(HANDLE hFile};
hFile — the descriptor of the open file of communication port
Example of getting a descriptor of the serial port COM1:
else cout << "Error Open" << endl;
fCoder © 1998 - 2002