
COM port access from VBA via the Win32 API - error code 87
Hi all,
I want to write an Access97 VBA module to import and export data to
and from one of these a CAISO organizers. I've found all the
neccessary documentation, but still did not find the bug in my code
that prevents me to initialize the COM2 port the interface is
connected to.
Since I don't have VB (and no licensed mscomm32.ocx), I have to use
Win32 API calls (CreateFile, SetCommState etc.).
Opening the port with CreateFile works, and I can read the DCB with
GetCommState (strangely enough, the DCBLength variable has a value of
28 instead of 80, as I would expect).
Now I initialize the DCB struct with the values required by the CASIO
organizer interface: 4800 baud, 7 data bits, 2 stop bits, any parity,
RTS on, DTR off (or vice versa), no XON-/XOFF-handling (has to be
processed by my program)...
When I try to initialize the COM port with these new settings, the
function always fails, returning the error code 87 - 'invalid
argument'.
Does anyone have a clue what might caus this problem?
Thanks a lot in advance,
Carsten
---
These are some snippets of my code you might need to answer this
posting:
'Declarations
-------------
Private Declare Function CreateFile Lib "Kernel32" Alias "CreateFileA"
(ByVal lpszName As String, ByVal fdwAccess As Long, ByVal fdwShareMode
As Long, lpsa As Any, ByVal fdwCreate As Long, ByVal fdwAttrsAndFlags
As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function GetCommState Lib "Kernel32" (ByVal hCommDev
As Long, lpDcb As DCB) As Boolean
Private Declare Function SetCommState Lib "Kernel32" (ByVal hCommDev
As Long, lpDcb As DCB) As Boolean
Type DCB
DCBlength As Long 'sizeof(DCB)
BaudRate As Long 'Baudrate at which running
fBinary As Long 'Binary Mode (skip EOF check)
fParity As Long 'Enable parity checking
fOUtxCtsFlow As Long 'CTS handshaking on output
fOutxDsrFlow As Long 'DSR handshaking on output
fDtrControl As Long 'DTR Flow control
fDsrSensitivity As Long 'DSR Sensitivity
fTXContinueOnXoff As Long 'Continue TX when Xoff sent
fOutX As Long 'Enable output X-ON/X-OFF
fInX As Long 'Enable input X-ON/X-OFF
fErrorChar As Long 'Enable Err Replacement
fNull As Long 'Enable Null stripping
fRtsControl As Long 'Rts Flow control
fAbortOnError As Long 'Abort all reads and writes on Error
fDummy2 As Long 'Reserved
wReserved As Integer 'Not currently used
XonLim As Integer 'Transmit X-ON threshold
XoffLim As Integer 'Transmit X-OFF threshold
ByteSize As Byte 'Number of bits/byte, 4-8
Parity As Byte '0-4=None,Odd,Even,Mark,Space
StopBits As Byte '0,1,2 = 1, 1.5, 2
XonChar As Byte 'Tx and Rx X-ON character
XoffChar As Byte 'Tx and Rx X-OFF character
ErrorChar As Byte 'Error replacement char
EofChar As Byte 'End of Input character
EvtChar As Byte 'Recieved Event character
wReserverd1 As Integer
End Type
'Function calls
---------------
Dim hCom As Long
hCom = CreateFile("COM2", GENERIC_WRITE Or GENERIC_READ, 0, ByVal 0&,
OPEN_EXISTING, 0, 0)
GetCommState hCom, dcbCom
'no errors so far, now changing dcbCom and
SetCommState hCom, dcbCom 'fails with error code 87
My return address is disguised to prevent junk mail.
Please remove the leading X.
=======================================
| Carsten Pohle
| WWW: http://www.*-*-*.com/ ~cpohle
=======================================