(3) sending and receiving of Serial Data
Similar to common file operations, when operating a serial port, the readfile function is usually used to read the data received by the serial port, and writefile is used to write the data to be sent as a serial port.
N receiving of Serial Data
The readfile function can be used to read data received by the serial port. The readfile function is prototype as follows:
Bool readfile (
Handle hfile
Lpviod lpbuffer
DWORD nnumberofbytestoread
Lpdword lpnumberofbytesread
Lpoverlapped
);
The hfile parameter points to the opened serial port handle; The lpbuffer points to a read data buffer; The nnumberofbytestoread specifies the number of bytes to be read from the serial device; and The lpnumberofbytesread indicates the number of bytes actually read from the serial port; lpoverlapped points to an overlapped structure variable, which contains a synchronization event.
If the call is successful, readfile returns a non-0 value; otherwise, the returned value is 0. However, for serial ports that receive operations in the background, the return value of 0 does not necessarily indicate that the function call fails. You can call the getlasterror function to obtain further information. If the returned value of getlasterror is error_io_pending, it means that the operation to read the serial port is still in the background waiting state, rather than a real error.
N Serial Data Transmission
The writefile function can be used to write data to the serial port. The writefile function is prototype as follows:
Bool writefile (
Handle hfile
Lpviod lpbuffer
DWORD nnumberofbytestowrite
Lpdword lpnumberofbyteswritten
Lpoverlapped
);
The hfile parameter points to the opened serial port handle; lpbuffer points to a sending data buffer; nnumberofbytestoread specifies the number of bytes to be sent from the serial device; lpnumberofbytesread indicates the number of bytes actually sent from the serial port; lpoverlapped points to an overlapped structure variable, which contains a synchronization event.
Generally, if the call is successful, writefile returns a non-0 value; otherwise, the return value is 0. However, if the returned value is 0 for the serial port sending operation in the background, the function call fails. You can call the getlasterror function to obtain further information. If the returned value of getlasterror is error_io_pending, it indicates that the operation to write data to the serial port is still in the background waiting state, rather than a real error.
(4) Close the serial port
Usually close a serial port after it is used up. If you forget to close it, the serial port will always be open, and other applications will not be able to open or use it.
You can use closehandle to disable the serial port. The function prototype is as follows:
Bool closehandle (
Handle hobject
);
The closehandle function is very simple, and the hobject is the handle to open the serial port. If the function is successfully called, the return value is not 0; otherwise, the return value is 0.