Use of serial-CreateFile, serial-createfile
In 32-bit windows systems, serial ports and other communication devices are processed as files. The operations on the serial port are exactly the same as those on the file. Communication starts by calling CreateFile.
The function prototype is as follows:
HANDLE CreateFile (LPCTSTR lpFileName, // pointer to the file name: Specifies the logic name of the serial port to be opened, represented by a string, such as COM1 and COM2
DWORD dwDesiredAccess, // access mode (write/read)
DWORD dw1_mode, // sharing mode: Port Sharing attribute. The serial port is 0, which is the biggest difference between the file and the communication device. A program opens a serial port, and an error occurs when another program is created using CreateFile.
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to the Security Attribute
DWORD dwCreationDisposition, // how to create: OPEN_EXISTING must be set in the serial port. It indicates that you cannot create a new port. You can only open an existing port.
DWORD dwFlagsAndAttributes, // File Attribute HANDLE hTemplateFile // used to copy the file HANDLE
);
If you use the CreateFile () function to open COM1, the Code is as follows:
HANDLE hCom; hCom = CreateFile ("COM1", // file name, both COM1 and GENERIC_READ | GENERIC_WRITE, // allow read and write 0, // NULL exclusive mode, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // The overlap mode is NULL );