0% found this document useful (0 votes)
10 views2 pages

Serial Port Connection Monitoring Tool

This document contains code for a monitoring form application in Visual Basic. It includes code to: 1) Close the form and open a login form when closing. 2) Center and initialize the form on load. 3) Scan and populate available COM ports in a dropdown and enable connecting when a port is selected. 4) Connect to a selected COM port and baud rate, start timers, and disable/enable buttons accordingly. 5) Disconnect from the COM port, stop timers, and enable/disable buttons in the opposite manner of connecting.

Uploaded by

Harits Cool
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Serial Port Connection Monitoring Tool

This document contains code for a monitoring form application in Visual Basic. It includes code to: 1) Close the form and open a login form when closing. 2) Center and initialize the form on load. 3) Scan and populate available COM ports in a dropdown and enable connecting when a port is selected. 4) Connect to a selected COM port and baud rate, start timers, and disable/enable buttons accordingly. 5) Disconnect from the COM port, stop timers, and enable/disable buttons in the opposite manner of connecting.

Uploaded by

Harits Cool
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Imports System.

IO
Imports [Link]
Imports [Link]
Imports [Link]

Public Class FormMonitoring

Private Sub FormMonitoring_FormClosing(sender As Object, e As


[Link]) Handles [Link]
[Link]()
End Sub

Private Sub FormMonitoring_Load(ByVal sender As [Link], ByVal e As


[Link]) Handles [Link]
[Link]()
[Link] = False
[Link] = False
[Link] = 2

[Link]()
[Link]()

End Sub

Private Sub ButtonScanPort_Click(ByVal sender As [Link], ByVal e As


[Link])
[Link]()
Dim myPort As Array
Dim i As Integer
myPort = [Link]()
[Link](myPort)
i = [Link]
i = i - i
Try
[Link] = i
[Link] = True
Catch ex As Exception
MsgBox("Com port not detected", [Link], "Warning !!!")
[Link] = ""
[Link]()
[Link] = False
Return
End Try
[Link] = True
End Sub

Private Sub ButtonConnect_Click(ByVal sender As [Link], ByVal e As


[Link])
[Link] = [Link]
[Link] = [Link]
[Link]()

[Link]()
[Link]()

[Link] = False
'[Link] = False
[Link] = False
[Link] = False
[Link] = False
[Link] = True
[Link] = [Link]
[Link] = "Status : Connected"
End Sub

Private Sub ButtonDisconnect_Click(ByVal sender As [Link], ByVal e As


[Link])
[Link] = [Link]
[Link] = True
[Link] = "Status : Disconnect"

[Link] = True
'[Link] = True
[Link] = True
[Link] = True
[Link] = True
[Link] = False

[Link]()
[Link]()

[Link]()
End Sub

Public Sub Timer1_Tick_1(ByVal sender As [Link], ByVal e As


[Link]) Handles [Link]

End Sub

Private Sub ConnectionToolStripMenuItem_Click(ByVal sender As [Link],


ByVal e As [Link])
[Link]()

End Sub

Private Sub MonitoringToolStripMenuItem_Click(ByVal sender As [Link],


ByVal e As [Link])
[Link]()

End Sub

End Class

Common questions

Powered by AI

ComboBoxBaudRate is pre-selected at index 2, providing a default baud rate setting for communication. This pre-selection streamlines the setup process for users, reducing the need for immediate configurational changes and potentially reflecting the most commonly used baud rate, thus enhancing usability and minimizing user errors in initial configuration .

Hiding GroupBoxConnection and Panel2 on the load event could be a design decision to streamline the user interface, presenting users only with necessary elements initially. This minimizes interface clutter, potentially simplifying user tasks by displaying additional options only as required, thus enhancing user focus and reducing initial interface complexity .

ButtonConnect_Click initiates a series of actions: it sets the serial port's BaudRate and PortName based on the user's selection, opens the serial port, and starts timers for data logging. The user interface is updated to disable port selection and scan buttons, and connection indicators are adjusted to show a green icon. This ensures users cannot change settings mid-operation, reducing potential errors .

The form closing event in the FormMonitoring application is handled by showing the FormLogin. This serves to redirect the user back to the login interface, ensuring a seamless transition and likely providing an opportunity for the application to manage session closure or user logout activities .

Encapsulation in FormMonitoring is applied by defining class-specific methods for managing connections, UI updates, and event handling, such as ButtonConnect_Click and ButtonDisconnect_Click. This encapsulation localizes functionality, promoting maintainability by isolating changes within manageable segments, thereby simplifying debugging and future development .

Disabling the ButtonScanPort and enabling ButtonDisconnect upon connection prevents user interference with the established connection, facilitating stable device communication. This design choice mitigates risks such as inadvertent scanning, which could disrupt active connections, thereby maintaining data integrity and preventing user-induced errors during operations .

Error handling during the port scanning operation is managed using a Try-Catch block. If an exception occurs while attempting to set the ComboBoxPort.SelectedIndex, a message box alerts the user with 'Com port not detected', and the ComboBoxPort is cleared and reset to ensure it is ready for another scan attempt .

Timer1 and TimerDataLogRecord likely perform periodic tasks essential for data monitoring and logging. Timer1 might refresh the UI or update status indicators, while TimerDataLogRecord could continuously record data from the connected device, ensuring seamless and real-time monitoring alongside reliable logging of information .

Upon a successful connection, the user is provided visual feedback through a green image in PictureBoxConnectionInd and the status label updates to 'Status : Connected'. Conversely, when disconnected, the image changes to red, and the status label updates to 'Status : Disconnect', clearly indicating the connection status .

ComboBoxPort is used to list and select available serial ports on the system. It is populated with port names using IO.Ports.SerialPort.GetPortNames() when the ButtonScanPort is clicked, allowing the user to select the appropriate port for establishing a connection .

You might also like