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
******************************************************************************************************
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
******************************************************************************************************