Using ActiveXperts Serial Port Component with with ASP.NET VB.NET (Visual Basic)
Step 2: Create a new ASP.NET VB Project
Launch Microsoft Visual Studio from the Start menu. Choose 'New' from the 'File' menu and click on 'Web Site'. In the 'Web Site' dialog, select ASP.NET VB .NET Web Site. Select a name for the application, we used: 'DemoApp', and a name for the solution, we used: 'DemoSolution'. Finally, select the directory where you want to store the project, for example: 'C:\MyProjects':
Step 3: Refer to the ActiveXperts Serial Port Component Library and create the objects
Now that a new project has been created, you must add a reference to the ActiveXperts Serial Port Component library in the project to be able to use the ActiveXperts Serial Port Component object. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'Serial Port Component Type Library' as shown in the following picture:
Click 'OK' to close the 'Add Reference' dialog.
On top of your code, type the following line to use the ActiveXperts Serial Port Component namespace:
Imports AxSerial
In your Main function, declare and create the following object:
Dim objComport As ComPort objComPort = New ComPort()
Step 4: Send an AT command to a Hayes compatible modem
You can now send and/or receive data to and/or from a serial interface.
The following code shows how to query a modem:
Public Class WebForm1 Imports AxSerial Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents comboDevice As System.Web.UI.HtmlControls.HtmlSelect Protected WithEvents textCommand As System.Web.UI.HtmlControls.HtmlInputText Protected WithEvents textResponse As System.Web.UI.HtmlControls.HtmlTextArea Protected WithEvents textResult As System.Web.UI.HtmlControls.HtmlTextArea Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Init InitializeComponent() End Sub #End Region Public m_objComPort As ComPort Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim i As System.Int32 m_objComPort = New ComPort() comboDevice.Items.Clear() For i = 0 To m_objComPort.GetDeviceCount - 1 comboDevice.Items.Add(m_objComPort.GetDevice(i)) Next For i = 1 To 8 comboDevice.Items.Add("COM" + i.ToString()) Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Dim LastError m_objComPort.Device = Request.Form(comboDevice.UniqueID) m_objComPort.BaudRate = 9600 m_objComPort.ComTimeout = 500 m_objComPort.LogFile = "C:\ComLog.txt" m_objComPort.Open() LastError = m_objComPort.LastError If (LastError = 0) Then textResult.Value = "SUCCESS" Else textResult.Value = "ERROR " & LastError & " ( " & _ m_objComPort.GetErrorDescription(LastError) & " )" End If If (m_objComPort.IsOpened = -1) Then m_objComPort.WriteString(textCommand.Value) textResponse.Value = "" While (m_objComPort.LastError = 0) textResponse.Value += m_objComPort.ReadString() + vbCrLf End While m_objComPort.Close() End If End Sub End Class
You can download the full samples here.