Visual Basic 2010 sample code on how to check com port, checking modem, send AT command and send USER command. Download Project
Visual Basic Source Code
Visual Basic Source Code
.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 checks for available ports on the local machine. It does
' this by attempting to open ports 1 through 4.
Private Sub CheckForPortsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckForPortsButton.Click
' Check for Availability of each of the 4 Comm Ports, and
' place a check in the list box items that have openable ports.
Dim i As Integer
For i = 1 To 4
WriteMessage("Testing COM" + i.ToString())
If IsPortAvailable(i) Then
' Check the box for available ports.
Me.PortsList.SetItemChecked(i - 1, True)
Else
' Uncheck the box for unavailable ports.
Me.PortsList.SetItemChecked(i - 1, False)
End If
Next
' Enable the Find Modems button.
Me.CheckModemsButton.Enabled = True
End Sub
' This subroutine attempts to send an AT command to any active Comm Ports.
' If a response is returned then a usable modem has been detected
' on that port.
Private Sub CheckModemsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckModemsButton.Click
Dim i As Integer
For i = 0 To 3
If Me.PortsList.GetItemChecked(i) Then
' Item is checked so it MIGHT be a valid port.
' Test for validity.
If IsPortAvailable(i + 1) Then
' Check if port responds to an AT command.
If IsPortAModem(i + 1) Then
' Set the class variables to the last modem found.
Me.IsModemFound = True
Me.ModemPort = i + 1
' Write message to the user.
WriteMessage("Port " + (i + 1).ToString() + _
" is a responsive modem.")
Else
' Write message to the user.
WriteMessage("Port " + (i + 1).ToString() + _
" is not a responsive modem.")
End If
End If
End If
Next
' If a modem was found, enable the rest of the buttons, so the user
' can interact with the modem.
If Me.IsModemFound Then
Me.SelectedModemTextbox.Text = "Using Modem on COM" + _
Me.ModemPort.ToString()
Me.UserCommandTextbox.Enabled = True
Me.SendATCommandButton.Enabled = True
Me.SendUserCommandButton.Enabled = True
End If
End Sub
' 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 an AT command to the modem, and records its response.
' It depends on the timer to do the reading of the response.
Private Sub SendATCommandButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendATCommandButton.Click
' Always wrap up working with Comm Ports in exception handlers.
Try
' Enable the timer.
Me.tmrReadCommPort.Enabled = True
' Attempt to open the port.
CommPort.Open(ModemPort, 115200, 8, RS232.DataParity.Parity_None, _
RS232.DataStopBit.StopBit_1, 4096)
' Write an AT Command to the Port.
CommPort.Write(Encoding.ASCII.GetBytes("AT" & 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 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.
Me.tmrReadCommPort.Enabled = True
' Attempt to open the port.
CommPort.Open(ModemPort, 115200, 8, RS232.DataParity.Parity_None, RS232.DataStopBit.StopBit_1, 4096)
' 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 function checks to see if the port is a modem, by sending
' an AT command to the port. If the port responds, it is assumed to
' be a modem. The function returns a Boolean.
Private Function IsPortAModem(ByVal port As Integer) As Boolean
' Always wrap up working with Comm Ports in exception handlers.
Try
' Attempt to open the port.
CommPort.Open(port, 115200, 8, RS232.DataParity.Parity_None, _
RS232.DataStopBit.StopBit_1, 4096)
' Write an AT Command to the Port.
CommPort.Write(Encoding.ASCII.GetBytes("AT" & Chr(13)))
' Sleep long enough for the modem to respond.
System.Threading.Thread.Sleep(200)
Application.DoEvents()
' Try to get info from the Comm Port.
Try
' Try to read a single byte. If you get it, then assume
' that the port contains a modem. Clear the buffer before
' leaving.
CommPort.Read(1)
CommPort.ClearInputBuffer()
CommPort.Close()
Return True
Catch exc As Exception
' Nothing to read from the Comm Port, so set to False
CommPort.Close()
Return False
End Try
Catch exc As Exception
' Port could not be opened or written to.
Me.PortsList.SetItemChecked(port - 1, False)
MsgBox("Could not open port.", MsgBoxStyle.OKOnly, Me.Text)
Return False
End Try
End Function
' This function attempts to open the passed Comm Port. If it is
' available, it returns True, else it returns False. To determine
' availability a Try-Catch block is used.
Private Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
Try
CommPort.Open(ComPort, 115200, 8, RS232.DataParity.Parity_None, _
RS232.DataStopBit.StopBit_1, 4096)
' If it makes it to here, then the Comm Port is available.
CommPort.Close()
Return True
Catch
' If it gets here, then the attempt to open the Comm Port
' was unsuccessful.
Return False
End Try
End Function
' 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
End
End Sub
End Class
7 comments:
There is great insight in your post. Why don't you check these out too:
How to Remove Mintnav from Android Phone
Remove Mintnav from Android Phone
Truly African
Loopsie Android
Netnaija Movie Downloader App
There is great insight in your post ktm bike price
Explore Seekware's comprehensive suite of custom software development services, meticulously crafted to align with your unique business requirements. Our team of seasoned developers and technology experts are dedicated to delivering cutting-edge solutions that optimize your operations and drive sustainable growth. With a commitment to innovation and a client-centric approach, Seekware empowers businesses to thrive in an ever-evolving digital landscape. Partner with us to unlock the full potential of custom software tailored to your specific needs.
here's a basic example of how you can achieve those tasks using Visual Basic 2010. This example assumes you have a modem connected to a COM port.
First, you'll need to add a SerialPort control to your form. You can find it in the toolbox under Components.
Here's the sample code:
vb
Copy code
Imports System.IO.Ports
Public Class Form1
' Declare SerialPort object
Dim WithEvents serialPort As New SerialPort
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Populate combo box with available COM ports
For Each port As String In SerialPort.GetPortNames()
ComboBox1.Items.Add(port)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Open selected COM port
If ComboBox1.SelectedIndex <> -1 Then
serialPort.PortName = ComboBox1.SelectedItem.ToString()
serialPort.BaudRate = 9600 ' Set baud rate
serialPort.Parity = Parity.None
serialPort.StopBits = StopBits.One
serialPort.DataBits = 8
serialPort.Handshake = Handshake.None
Try
serialPort.Open()
MessageBox.Show("COM port opened successfully.")
Catch ex As Exception
MessageBox.Show("Error opening COM port: " & ex.Message)
End Try
Else
MessageBox.Show("Please select a COM port.")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Send AT command
If serialPort.IsOpen Then
serialPort.WriteLine("AT")
MessageBox.Show("AT command sent.")
Else
MessageBox.Show("COM port is not open.")
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' Send USER command
If serialPort.IsOpen Then
serialPort.WriteLine("USER")
MessageBox.Show("USER command sent.")
Else
MessageBox.Show("COM port is not open.")
End If
End Sub
Private Sub serialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles serialPort.DataReceived
' Handle data received from serial port if needed
Dim data As String = serialPort.ReadLine()
MessageBox.Show("Received data: " & data)
End Sub
End Class
In this code:
We populate a ComboBox with available COM ports when the form loads.
The user selects a COM port from the ComboBox and clicks the "Open Port" button to open it.
We have buttons for sending AT commands and USER commands. When clicked, these buttons send the corresponding commands over the serial port.
The serialPort_DataReceived event handler is triggered when data is received from the serial port. You can use this to handle responses from the modem if needed.
Remember to handle exceptions appropriately and add error checking for production-level code.
kamrun seo
Here's a simple Visual Basic code example for a basic "Hello, World!" program:
vb
Copy code
Module HelloWorld
Sub Main()
Console.WriteLine("Hello, World!")
End Sub
End Module
This code will output "Hello, World!" to the console when executed. It's a simple starting point for learning Visual Basic programming. Let me know if you need more specific examples or have any questions!<a href="https://dev-rupahw.pantheonsite.io”>SEO zannatul</a>
Glad I found this blog! It will help me to start from scratch.
Indiana Bowed Wall
I'm not into codes but, you explained it well.
Aqualine Steel Rainwater Tanks
Post a Comment