| ||||
|
Look RS232
Download Order Support Testimonials FAQ Online help Modem Programming Serial port RS232 Hardware
Interface RS232C
ProgrammingHistory RS232C Likely problems Hardware
Hardware Properties
Interfacing devicesBit Rates DTE/DCE Speeds Flow control The UART Types of UARTs Null modems Loop Back Plug Registers |
|
||||||||
As work with the ports in Windows is carried out in the same way as work with files, reading from and writing into the port are carried out with the help of ReadFile and WriteFile functions correspondingly. ReadFile function is used to read the information from the port Function syntax:
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped);
BOOL WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped); Parameters specifications:
hFile � descriptor of the open communication port file;
lpBuffer � buffer address. In case of Write operation the data from this buffer will be transmitted to the port. In case of Read operation the data received from the line will be placed into this buffer; nNumOfBytesToRead, nNumOfBytesToWrite � number of bytes to be received or or intended for transmission; nNumOfBytesRead, nNumOfBytesWritten � number of actually received or transmitted bytes. If the number of received or transmitted bytes is lower than requested it's an error for a disc file but not necessarily for a communication port. The cause is to be found in timeouts. LpOverlapped � the address of OVERLAPPED structure used for asynchronous operations. In case of abnormal temination the functions return NULL, in case of normal termination - any other value. Example of read-write operation: DWORD numbytes, numbytes_ok, temp; COMSTAT ComState; OVERLAPPED Overlap; char buf_in[6] = "Hello!"; numbytes = 6; ClearCommError(handle, &temp, &ComState); // if temp is not null, the port is in the error state if (!temp) WriteFile(handle, buf_in, numbytes, &numbytes_ok, &Overlap); ClearCommError(handle, &temp, &ComState); if(!temp) ReadFile(handle, buf_in, numbytes, &numbytes_ok, &Overlap); // the numbytes_ok variable contains the actual number of I/O bytes Note
This example uses two structures COMSTAT and OVERLAPPED which we haven't yet discussed, and also ClearCommError function. In case of �three wires� connection we needn't consider OVERLAPPED structure (just use it as shown in the example).
| |||||||||
Contact us © fCoder SIA |