Question serilal communication doubt

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hello,
I am new VB.net and trying to make a program to communicate with my serial USB device. I have been doing this from an example here Windows SerialPort Sample in VB.NET (C#) in C#, VB.NET for Visual Studio 2008 and i stucked with one error. I could not able to fix this error unfortuantely. Can any one help me to slove this?

hee is the code

Imports System
Imports System.IO.Ports
 
Public Class Form1
Public serialport1 As New System.IO.Ports.SerialPort
Dim readBuffer As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' read avaiable COM Ports:
Dim Portnames As String() = System.IO.Ports.SerialPort.GetPortNames
If Portnames Is Nothing Then
MsgBox("There are no Com Ports detected!")
Me.Close()
End If
ComboBox1.Items.AddRange(Portnames)
ComboBox1.Text = Portnames(0)
ComboBox2.Text =
"115200"
With serialport1
.ParityReplace = IO.Ports.
Parity.None ' replace ";" when parity error occurs
.PortName = ComboBox1.SelectedItem
.BaudRate = 115200
.Parity = IO.Ports.
Parity.None
.DataBits = 8
.StopBits = IO.Ports.
StopBits.One
.Handshake = IO.Ports.
Handshake.None
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
SerialPort1.Open()
CheckBox1.Checked = serialport1.IsOpen
Catch ex As Exception
CheckBox1.Checked = False
MsgBox("Error Open: " & ex.Message)
End Try
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialport1.DataReceived
 
If serialport1.IsOpen Then
Try
readBuffer = serialport1.ReadLine()
'data to UI thread
Me.Invoke(New EventHandler(AddressOf DoUpdate))
Catch ex As Exception
MsgBox("read " & ex.Message)
End Try
End If
End Sub
Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
TextBox1.Text = readBuffer
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If serialport1.IsOpen Then
' clear input buffer
serialport1.DiscardInBuffer()
serialport1.Close()
End If
End Sub
End
Class
 
Back
Top