Posted By: MisterEd |
5/12/2006 2:54:00 AM |
Reply To: sharmanikita who posted... |
|
If you have VB.NET 2005 then there is another option. They have added a new control for the serial port. Here is some sample code:
'Setup:
' Add SerialPort control to form:
' Type: System.IO.Ports.SerialPort
' Name: mySerialPort
Dim ModemPort As String
Dim USRModem As Boolean
Private Sub CommInit()
With mySerialPort
.PortName = ModemPort
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = System.IO.Ports.StopBits.One
.BaudRate = 9600
.DtrEnable = True
.NewLine = vbCrLf
End With
End Sub
Public Sub OpenPort()
mySerialPort.Open()
End Sub
Public Sub ClosePort()
mySerialPort.Close()
End Sub
Public Sub HangUp()
mySerialPort.WriteLine("ATH")
ClosePort()
End Sub
Public Sub WritePort(byval data as string)
mySerialPort.WriteLine(data)
End Sub
Public Function ReadPort()
ReadPort = mySerialPort.ReadExisting()
end funtion
Sub Pause(ByVal PauseTime As Object)
Dim StartTime As Double, EndTime
StartTime = Microsoft.VisualBasic.DateAndTime.Timer
EndTime = StartTime + PauseTime
Do While Microsoft.VisualBasic.DateAndTime.Timer
Application.DoEvents()
Loop
End Sub
Private Function GetModemPort() As Boolean
Dim ModemInput As String, FoundModem As Boolean
Dim port As String
Dim ports As String() = System.IO.Ports.SerialPort.GetPortNames()
FoundModem = False
For Each port In ports
ModemPort = port
CommInit
OpenPort
WritePort("ATV1Q0")
Pause(0.5)
ModemInput = ReadPort()
If InStr(ModemInput, "OK" & vbCrLf) 0 Then
.WriteLine("ATI3")
Pause(0.5)
ModemInput = ReadPort()
ClosePort
portName = port
If InStr(ModemInput, "U.S. Robotics") Then
USRModem = True
Else
USRModem = False
End If
FoundModem = True
ModemPort = port
Exit For
End If
ClosePort
Pause(0.5)
Next port
GetModemPort = FoundModem
End Function
Public Sub DialNumber(ByVal Number$)
Dim Message As String
Dim MessageFlag As Boolean
Dim DialString$
MessageFlag = False
If Modem.USRModem Then
DialString$ = "ATDT" + Number$ + ";"
Else
DialString$ = "ATDT" + Number$
End If
' Dial the number.
mySerialPort.WriteLine(DialString$)
Message = ""
Do
Application.DoEvents()
MessageFlag = False
If mySerialPort.BytesToRead > 0 Then
Message = mySerialPort.ReadExisting
MessageFlag = True
End If
' If there is data in the buffer, then read it.
If MessageFlag Then
If InStr(Message, "NO DIALTONE") Then
HangUp()
Exit Do
ElseIf InStr(Message, "NO CARRIER") Then
HangUp()
Exit Do
ElseIf InStr(Message, "BUSY") Then
HangUp()
Exit Do
ElseIf InStr(Message, "ERROR") Then
ActiveCall = False
HangUp()
Exit Do
ElseIf InStr(Message, "AT") Then
ElseIf InStr(Message, "OK") Then
End If
Message = ""
MessageFlag = False
End If
Loop
End Sub
|
|