COM port access from VBA via the Win32 API - error code 87 
Author Message
 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
=======================================



Thu, 30 Mar 2000 03:00:00 GMT  
 COM port access from VBA via the Win32 API - error code 87


Quote:

>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).

this is the problem. The declarations in win32api.txt are wrong. The API uses
bitfields to represent the various flags. The no-brained machine translation
converted them to individual longs.

Quote:
>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 cause this problem?

try this insted:

Quote:
>Type DCB
>   DCBlength As Long             'sizeof(DCB)
>   BaudRate As Long              'Baudrate at which running

    flowCtrlFlags As Long

Quote:
>   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

And setup suitable bit masks. Email me if you want a code example. This was
written for excel95 which does use VBA but doesn't support call backs, so it's
rather simplistic.

--
Michael Rickard - Computing Support
Physiology Dept - Bristol University

phone: (44) 117 928 7808



Fri, 31 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Need Help Writting a VBA Module In Access 87

2. RegEnumKeyEx results in API error 87 - Parameter Incorrect

3. API Error 87

4. API Error 87

5. Error 87 using Registry API's and NTFS

6. Netsharedel on NT with VB5 thru API-callgives error 87

7. ReadEventLog - Please Post Working Code - error 87

8. Windows NT Error Code = 87

9. replace bug (VBA 6.0.87)

10. re win32 api - error code definitions

11. how to access COM port from VBA

12. Solution: Dr. Watson attach to app, nt code 87

 

 
Powered by phpBB® Forum Software