serial port


Posted by Mark Edwards on October 04, 2001 1:11 AM

Hi,
I want to be able to access the serial port from excel!
I want to send a command to the serial port 'IAVE' and then read back the four digit number that is returned and store this in a cell in excel!
Any ideas?

Posted by Jerid on October 05, 2001 5:16 AM

Mark, here is some code I passed along a few weeks ago to someone that was trying to use Excel to talk to a modem. I think this holds the most promise for you. Good Luck.

Original Post

My Response
Paul, here is some code I came across, I don't have an analog phone line at my desk so I couldn't test it 100%, but it seemed to work.

You need to set a reference to the Microsoft Comm Control 6.0 through Tools, References before it will work. You will have to play with the code to make it work for you.

I coded it so that it dials when I double click on a cell with the phone number.


-----START OF CODE-----
Dim CancelFlag As Integer

Private Sub cmdStop_Click()
CancelFlag = 1
End Sub

Private Sub Worksheet_Activate()
'Setting InputLen to 0 tells MSComm to read the entire contents of the
'input buffer when the Input property is used.

MSComm1.InputLen = 0
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim Number As String

' Get the number to dial.
Number = Target.Value
If Number = "" Then Exit Sub

' Dial the selected phone number.
Dial (Number)

End Sub

Private Sub Dial(Number)
Dim DialString As String
Dim FromModem As String
Dim dummy As Integer

' AT is the Hayes compatible ATTENTION command and is required to send commands to the modem.
' DT means "Dial Tone." The Dial command uses touch tones, as opposed to pulse (DP = Dial Pulse).
' Numbers is the phone number being dialed.
' A semicolon tells the modem to return to command mode after dialing (important).
' A carriage return, vbCr, is required when sending commands to the modem.
DialString = "ATDT" + Number + ";" + vbCr

' Communications port settings.
' Assuming that a mouse is attached to COM1, CommPort is set to 2
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"

' Open the communications port.
On Error Resume Next
MSComm1.PortOpen = True
If Err Then
MsgBox "COM1: not available. Change the CommPort property to another port."
Exit Sub
End If

' Flush the input buffer.
MSComm1.InBufferCount = 0

' Dial the number.
MSComm1.Output = DialString

' Wait for "OK" to come back from the modem.
Do
dummy = DoEvents()
' If there is data in the buffer, then read it.
If MSComm1.InBufferCount Then
FromModem = FromModem + MSComm1.Input
' Check for "OK".
If InStr(FromModem, "OK") Then
' Notify the user to pick up the phone.
Beep
MsgBox "Please pick up the phone and either press Enter or click OK"
Exit Do
End If
End If

' Did the user choose Cancel?
If CancelFlag Then
CancelFlag = False
Exit Do
End If
Loop

' Disconnect the modem.
MSComm1.Output = "ATH" + vbCr

' Close the port.
MSComm1.PortOpen = False
End Sub
-----END OF CODE-----

Hope this helps.

Jerid



Posted by Steve on October 05, 2001 6:07 AM

Would it be easier to just write a command line program to do it, have the command line program output the result to stdout, and pass it on to excel? Not exactly VB programming I know, but maybe easier. Just an idea,

Steve

' Did the user choose Cancel?