ReadFile and WriteFile function of serial port

Source: Internet
Author: User
Tags readfile

BOOL ReadFile (

HANDLE hfile,//handle to file

lpvoid lpbuffer,//a buffer to hold read-in data

DWORD nNumberOfBytesToRead,//number of characters to be read in

Lpdword Lpnumberofbytesread,//pointer to the actual number of bytes read

lpoverlapped lpoverlapped//If file_flag_overlapped is specified when the file is opened, a special structure must be referenced with this parameter. The structure defines an asynchronous read operation. Otherwise, this parameter should be set to null

);

function Description

The data is read out to a file starting at the point where the file pointer is pointing, and it supports both synchronous and asynchronous operations.

If the file is opened without indicating file_flag_overlapped, when the program call succeeds, it saves the number of bytes actually read out of the file to the address space indicated in Lpnumberofbytesread.

if the file is to be used interactively, remember to adjust the file pointer when the function call is complete.

read the data from the file. This function is significantly more flexible than the Lread function. This function is capable of manipulating communication devices, pipelines, sockets, and postal slots.

parameter Description

HANDLE hfile, a file pointer that needs to be written to the data, the file that the pointer points to must be the file that generic_readaccess access the property.

lpoverlapped lpoverlapped overlapped struct pointer, this pointer cannot be null if the file is opened in file_flag_overlapped mode.

return value

call succeeded, return not 0

The call was unsuccessful and returned as 0

The GetLastError is set. If you start an asynchronous read operation, the function returns a value of 0 and sets error_io_pending as the result of GetLastError. If the result is not a value of 0, but the number of bytes read is less than the value specified by the nNumberOfBytesToRead parameter, it indicates that the end of the file has already been reached.

*************************************************************************************************************** *****************

WriteFile

The WriteFile function writes data to a file and are designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation have been completed, the file pointer is adjusted by the number of bytes actually written, except When the file was opened with File_flag_overlapped. If The file handle is created for overlapped input and output (I/O), the application must adjust the position of the file Pointer after the write operation is finished.

This function was designed for both synchronous and asynchronous operation. TheWriteFileEx function is a designed solely for asynchronous operation. It lets an application perform other processing during a file write operation.

BOOL WriteFile (  HANDLE  hfile,                    //HANDLE to file  lpcvoid  lpbuffer  ,                //Data buffer  DWORD  nnumberofbytestowrite,     // Number of bytes to write  lpdword  lpnumberofbyteswritten,  //number of bytes Written  lpoverlapped  lpoverlapped        //overlapped buffer);
Parameters
hfile
[In] Handle to the file to is written to. The file handle must has been created with Generic_write access to the file.

Windows nt/2000/xp: For asynchronous write operations, hfile can is any handle opened with the File_flag_overlapped FLAG by theCr Eatefile function, or a socket handle returned by thesocket or accept function.

Windows 95/98/me: For asynchronous write operations, hfile can is a communications resource opened with the file_flag_overlapped FL AG byCreateFile, or a socket handle returned by socket oraccept. You cannot perform asynchronous write operations on MailSlots, named pipes, or disk files.

Lpbuffer
Pointer to the buffer containing the data to is written to the
file.
Nnumberofbytestowrite
[In] Specifies the number of bytes to write to the file.

A value of zero specifies a null write operation. The behavior of a null write operation depends on the underlying file system. To truncate or extend a file, use thesetendoffile function.

Named Pipe write operations across a network is limited to 65,535 bytes.

Lpnumberofbyteswritten
[out] Pointer to the variable that receives the number of bytes written. WriteFileSets this value to zero before doing any work or error checking.

Windows nt/2000/xp: If lpoverlapped is null, the Lpnumberofbyteswritten cannot is null. Iflpoverlapped is not null, the lpnumberofbyteswritten can be null. If This is a overlapped write operation, you can get the number of bytes written by callingGetOverlappedResult. If hfile is associated with an I/O completion port, you can get the number of bytes written by callingGetqueu Edcompletionstatus.

If I/O completion ports is used and you is using a callback routine to free the memory allocated to theoverlapped structure pointed to by the lpoverlapped parameter, specify NULL as the value of this parameter to avoid a mem Ory corruption problem during the deallocation. This memory corruption problem would cause an invalid number of bytes to being returned in this parameter.

Windows 95/98/me: This parameter cannot is NULL.

lpoverlapped
[in] Pointer to an OVERLAPPED structure.  This structure is required if hfile were opened with file_flag_overlapped.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ReadFile and WriteFile function of serial port

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.