I was trying to access serial port data into excel 2002 (VBA) using:
CreateFile in overlapped mode to open com port
ReadFile to receive the data (with the overlapped structure passed on)
Declerations are:
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _
lpOverlapped As OVERLAPPED) As Long
Function being called as
udtPorts(intPortID).lngHandle = CreateFile(strPort, GENERIC_READ Or _
GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
and,
Dim lngStatus As Long
Dim lngRdSize As Long, lngBytesRead As Long
Dim lngRdStatus As Long, strRdBuffer As String * 1024
intPortID = 2
lngRdStatus = ReadFile(udtPorts(intPortID).lngHandle, strRdBuffer, _
lngRdSize, 0, udtCommOverlap)
lngStatus = GetLastError
After calling the function ReadFile, When the data is present in receive buffer, there is no error.
But if no data is available in receive buffer, i am getting the return value in 'lngRdStatus' as '0', and 'lngStatus' as 87.
The error discription given for this code is 'ERROR_INVALID_PARAMETER'.
I thought i should be getting 'ERROR_IO_PENDING' or 997.
Can anyone tell me what is going wrong. If required, i can paste the whole code..
The commTimeout values are:
With udtCommTimeOuts
.ReadIntervalTimeout = 5000
.ReadTotalTimeoutMultiplier = 0
.ReadTotalTimeoutConstant = 1000
.WriteTotalTimeoutMultiplier = 0
.WriteTotalTimeoutMultiplier = 1000
End With