Ref: "Ap1400ProgGuide1_2.docx"
Page 38 of 42
Document Revision: “1.2”
Document Date: “9 Apr. 2013”
ReadData
Read data transmitted from a device
Prototype
int
WINAPI ReadData(
int
iHandle,
LPSTR
pData,
LPINT
pLen);
Description.
Reads data from a device into the supplied buffer. If there is no data to retrieve,
then ReadData will wait for a small period of time(typically 1000m/s) before
returning. Data is read over a bulk endpoint which has a buffer size of 64 bytes,
Where the supplied buffer is greater than 64 bytes, the USB will be accessed
repeatedly until either there is no more data to receive or pLen has been reached.
Where a data stream ends before the supplied buffer is full then pLen is set to
indicate the actual number of bytes read from the device. If the number of bytes
read reaches the value held in pLen then the function returns immediately and any
remaining data remains pending. This data can be read by further calls to
ReadData
Arguments
ihandle – Handle of an open device. This handle must have been issued by a
previous call to OpenDevice.
pData - Pointer to a suitable buffer to receive the data. This buffer must be at least
the size of the value held in pLen.
pLen
Pointer to an int value containing the number of bytes to read. This value
must not be greater that the supplied buffer size. On return this value is
modified to indicate the actual number of bytes read.
Returns
ERROR_SUCCESS if successful, otherwise the value is set to the system error
code.
Example
LPSTR
pBuffer=new char[256];
int
iLen=256;
DWORD
m_dwStatusCode=ReadData(m_iPrinterHandle,
pbuffer,&iLen);
//Check return
if
(m_dwStatusCode!=ERROR_SUCCESS)
{
CString
csError;
csError.Format(_T(
"ReadData Failed - Reason=%d."
),
m_dwStatusCode));
AfxMessageBox(csError,MB_ICONSTOP|MB_OK);
}
else
{
CString
csMessage;
csMessage.Format(_T(
"ReadData success – Bytes read=%d.\n"
)
_T(“Data=%S.\n”),iLen,pBuffer);
AfxMessageBox(csMessage,MB_ICONINFORMATION|MB_OK);
}