Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO.Ports
Imports System.Threading
Imports System.Text.Encoding
Class frmmain
Inherits Form
Const cID As String = "0"
Const cRM As String = "1"
Const cMD As String = "2"
Const cST As String = "3"
Const cMP As String = "4"
Const cFP As String = "5"
Const cVL As String = "6"
Const cCL As String = "7"
Const cAC As String = "8"
Const cDC As String = "9"
Const cPG As String = "10"
Const cIG As String = "11"
Const cDG As String = "12"
Const cDB As String = "13"
Const cOF As String = "14"
Dim log As String
Dim csettings() As String
Dim device As String = "99"
Dim rx As String = ""
Dim lock As New Object()
Public Sub New()
InitializeComponent()
End Sub 'New
Private validportname As New ArrayList()
' Send the text in the text box to the serial port.
' The data should stay in the buffer until received by the event handler.
Private Sub SendButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsend.Click
Dim sendstr As String = txtsend.Text
Try
' Write a line to the serial port
send(sendstr)
txtsend.Text = ""
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'SendButton_Click
Private Sub textupdate(ByVal txt As String)
Dim readdata As String = ""
Dim intNumBytes As Integer = SerialPort1.BytesToRead
Dim bytCharData(intNumBytes - 1) As Byte
SerialPort1.Read(bytCharData, 0, intNumBytes)
readdata = ASCII.GetString(bytCharData)
rx = readdata
log = log + readdata
'For Each b As Byte In bytCharData
' b = ASCII.GetString(b)
' ' 'b = CByte(b.ToString("X"))
' ' ' 'readdata = readdata + CStr(Asc(b.ToString))
'Next
txtreceived.Text = readdata
RichTextBox1.Text = log
Monitor.Exit(lock)
End Sub
Private Delegate Sub DlgDataReceivedTextBoxUpdate(ByVal txt As String)
' Event handler for data reception
Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
Dim readdata As String = ""
Try
Monitor.Pulse(lock)
'Dim readdata As String = SerialPort1.ReadLine
Static dlg As New DlgDataReceivedTextBoxUpdate(AddressOf textupdate)
' Read the buffer to the text box using separate thread.
Me.Invoke(dlg, New Object() {readdata})
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'serialPort_DataReceived
' Load the form and set up parameters from the default serial port.
' Open the port and prepare it for IO.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Try
' Open the serial port
SerialPort1.Encoding = Encoding.Default
SerialPort1.Open()
' Set the event handler for data reception
AddHandler SerialPort1.DataReceived, AddressOf serialPort_DataReceived
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
' Populate the list box with all availble com ports on the local machine.
updatestatusbar()
GetSerialPortNames()
End Sub 'Form1_Load
Private Sub GetSerialPortNames()
' Show all available COM ports.
For Each pn As String In My.Computer.Ports.SerialPortNames
cbxportname.Items.Add(pn)
validportname.Add(pn)
Next
'load the current port into the combobox
cbxportname.SelectedItem = SerialPort1.PortName.ToString
End Sub
' Respond to the form closing event by closing the SerialPort instance.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
SerialPort1.Close()
End Sub 'Form1_FormClosing
Private Sub cbxportname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxportname.SelectedIndexChanged
Try
SerialPort1.Close()
SerialPort1.PortName = CType(validportname(cbxportname.SelectedIndex), String)
updatestatusbar()
SerialPort1.Open()
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub showaboutbox()
Dim objabout As New about
objabout.ShowDialog(Me)
End Sub
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
showaboutbox()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
SerialPort1.Close()
Me.Close()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
End Sub
Private Sub updatestatusbar()
Dim stopbits As Double
Select Case SerialPort1.StopBits.ToString
Case "One"
stopbits = 1
Case "None"
stopbits = 0
Case "OnePointFive"
stopbits = 1.5
Case "Two"
stopbits = 2
End Select
tslblsettings.Text = SerialPort1.PortName.ToString & " " & SerialPort1.BaudRate.ToString & "|" & SerialPort1.DataBits.ToString & Mid(SerialPort1.Parity.ToString, 1, 1).ToString & stopbits.ToString
End Sub
Private Sub send(ByVal text As String)
SerialPort1.Write(text & Chr(13))
End Sub
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click
Try
Monitor.Enter(lock)
send("99ID")
Monitor.Wait(lock)
Monitor.Exit(lock)
MessageBox.Show(rx.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class 'Form1
'Build 1.0.0.1 - 10.3.2007 A tool used to communicate to another device via the serial port.