Use of serial-CreateFile, serial-createfile

Source: Internet
Author: User

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 );

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.