global MSComm object ... Error: "not defined"

P

phabsvide

Hi,
I am new to VB and VBA and am working on a powerpoint presentation and
need to connect to the serial port.
If I use a form and create the MSComm object by adding a control
(button with phone on it), it works fine.

But as the serial connection is needed all over the powerpoint
presentation and also I do not want to have forms popping up all the
time, I tried to do use a global MSComm object but get an error and the
connection does not work.

It would be great, if you could tell me how to either use the
connection from the nice litte telephone button all over the
presentation or help me debug the following code.

I very much appreciate your help!

phabs


CODE:
If I run the following code, I get an error like:
"Object variable or With-Blockvariable not defined"
Also, the connection to the serial device failed
(I know that because after this line it is still possible to
connect to the serial device with an other program)
******************************************************************************************************

'declaring variable
'(I would like to use this all over the presentation in different
"modules")
Public MSComm3 As MSComm

Sub test()
On Error Resume Next 'Error handler

'init serial communication
'signal adjusted for 1 Byte, text mode
MSComm3 = New MSComm
MSComm3.CommPort = 1
MSComm3.DTREnable = True
MSComm3.EOFEnable = False
MSComm3.Handshaking = comNone
MSComm3.InBufferSize = 1024
MSComm3.InputLen = 1
MSComm3.InputMode = comInputModeText
MSComm3.NullDiscard = False
MSComm3.OutBufferSize = 512
MSComm3.ParityReplace = False
MSComm3.RThreshold = 0
MSComm3.RTSEnable = True
MSComm3.Settings = "9600.n,8,1"
MSComm3.SThreshold = 0
'MSComm3.Width = 28
'MSComm3.Height = 28
'MSComm3.Left = 10
'MSComm3.Top = 30
'MSComm3.Tag = "tag"


If MSComm3.PortOpen = False Then 'check if serial port is open
MSComm3.PortOpen = True 'open port '***** ERROR HERE
End If
If Err Then MsgBox Error$, 48 '***** ERROR HERE

'set text to be sent to COM1
MSComm3.Output = "k"

'"watchdog" if no answer from device within specified time..
Dim StartTime, WaitTime
StartTime = Timer
WaitTime = 2

'wait for response from serial device:
Dim Rx As String
Rx = MSComm3.Input

Do
Rx = MSComm3.Input
Loop While (Timer < (StartTime + WaitTime) And (StrComp(Rx, "") =
0))

'Show input from device and close connection
MsgBox Rx
MSComm3.PortOpen = False
End Sub

******************************************************************************************************
 
C

Cindy M -WordMVP-

Hi Phabsvide,
I am new to VB and VBA and am working on a powerpoint presentation and
need to connect to the serial port.
You've posted in a Word newsgroup; a Powerpoint group would have been
more appropriate...

But for this question, I strongly recommend you ask in a classic VB
(Visual Basic) group, as this will probably require going over the
Windows API.

Here's a tip for you, anyway: Comment out On Error Resume Next. Used
globally, as you do, this will simply have the effect of masking any
errors that you should be correcting. At the end, what you think is an
error may well be the result of a problem much higher up in the code.
Use a proper error handler, or none at all.

In addition, Err is not boolean. It's an object. So a construction such
as If Err Then is not logical, and not valid. Probably, you want to test
If Err.Number <> 0 Then

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
J

Jezebel

Entirely concur with your advice on error-handling, but this statement is
not quite correct --

First, although Err is an object, its default property is its number. So in
a context that calls for a simple value, Err and Err.Number are
syntactically equivalent. Second, booleans are just longs in disguise. For
purposes of logical testing, TRUE is defined not as -1, but as "not
false" -- ie any value other than zero.

So "If Err then ..." is not only valid, but syntactically identical with "If
Err.Number <> 0 then ..."

But it's still bad practice.
 
P

phabsvide

Thanks for your help (even though this is a word group).

I finally found a solution that ommits the use of forms
but still creates a new connection on every slide:

1. on the ppt slide:
Create the connection (MyConnection) via control button
(with telephone icon)
Create the button (MyButton) which will trigger the connection.

2. In the MyButton_OnClick it is still possible to access the
connection. (From here I just call the function I need and
pass the connection as an 'As Object' argument).

It is not a "nice" solution. It works, though the connection
works not excactly the same as if I had MyButton and MyConnection
on the same form. e.g. "If MyConnection.PortOpen = False Then .."
cannot be evaluated because its a read only property. ..

lg
phabs
 
C

Cindy M -WordMVP-

Hi Jezebel,

OK, I stand corrected: the code wouldn't be invalid. But definitely
bad practice and, in the long run, not reliable - you never know when
someone at MS will change their minds about what the default is, or
how boolean will be tested.

To say nothing of the confusion if the code ever needs to be migrated
to another language...

Cindy Meister
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top