Imports System.Text
Public Class MainForm
' Declare necessary class variables.
Private CommPort As New RS232()
Private IsModemFound As Boolean = False
Private ModemPort As Integer = 0
' This subroutine clears the TextBox.
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
Me.StatusTextbox.Clear()
End Sub
' This subroutine sends a user specified command to the modem, and records its
' response. It depends on the timer to do the reading of the response.
Private Sub SendUserCommandButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendUserCommandButton.Click
' Always wrap up working with Comm Ports in exception handlers.
Try
' Enable the timer.
'If Not Me.tmrReadCommPort.Enabled Then
Me.tmrReadCommPort.Enabled = True
'End If
'If Not CommPort.IsOpen Then
' Attempt to open the port.
CommPort.Open(8, 115200, 8, RS232.DataParity.Parity_None, RS232.DataStopBit.StopBit_1, 4096)
'End If
'Write an user specified Command to the Port.
CommPort.Write(Encoding.ASCII.GetBytes(Me.UserCommandTextbox.Text & Chr(13)))
' Sleep long enough for the modem to respond and the timer to fire.
System.Threading.Thread.Sleep(200)
Application.DoEvents()
CommPort.Close()
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to communicate with Modem")
Finally
' Disable the timer.
Me.tmrReadCommPort.Enabled = False
End Try
End Sub
' This subroutine is fired when the timer event is raised. It writes whatever
' is in the Comm Port buffer to the output window.
Private Sub tmrReadCommPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadCommPort.Tick
Try
' As long as there is information, read one byte at a time and
' output it.
While (CommPort.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(CommPort.InputStream(0)), False)
End While
Catch exc As Exception
' An exception is raised when there is no information to read.
' Don't do anything here, just let the exception go.
End Try
End Sub
' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)
Me.StatusTextbox.Text += message + vbCrLf
End Sub
' This subroutine writes a message to the txtStatus TextBox and allows
' the line feed to be suppressed.
Private Sub WriteMessage(ByVal message As String, ByVal linefeed As Boolean)
Me.StatusTextbox.Text += message
If linefeed Then
Me.StatusTextbox.Text += vbCrLf
End If
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
CommPort.Close()
End
End Sub
Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
CommPort.Close()
End Sub