By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,736 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

SerialPort read VB.Net 2005 Beta 2

P: n/a
I need some codes that able to send and read data by serialport in VB.Net
2005 beta 2. I wrote some codes and It can send data via serialport but It
cannot read what I send. I have looked up on internet and msdn and found
some codes and samples but they are also can't read serial port. . It was so
easy in VB 6.0 with mscomm object and oncommread events but now I don't
understand how it reads data from serialport. You can see the codes bellow
but while It trys to read data from serialport the program is frozen. What
is it worng and how can I fix it?

Thank you.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
GetSerialPortNames()

End Sub

Private Sub cmdOpenPort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdOpenPort.Click

If comPort.IsOpen Then comPort.Close()

OpenPort(cboPortName.Text)

End Sub

Private Function OpenPort(ByVal PortNo As String) As Boolean

With comPort

If .IsOpen = True Then Close() 'close the port

..PortName = PortNo

..BaudRate = 9600

..Open()

End With

OpenPort = True

End Function

Sub GetSerialPortNames()

' Show all available COM ports.

For Each sp As String In My.Computer.Ports.SerialPortNames

cboPortName.Items.Add(sp)

Next

End Sub

Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSend.Click

SendSerialData(txtOutput.Text)

End Sub

Sub SendSerialData(ByVal data As String)

' Send strings to a serial port.

If comPort.IsOpen = False Then

MsgBox("The port is not open")

Else

comPort.WriteLine(data)

End If

End Sub

Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdRead.Click

txtOutput.Text = ReveiveSerialData()

End Sub

Function ReveiveSerialData() As String

' Receive strings from a serial port.

Dim returnStr As String = ""

Do

Dim Incoming As String = comPort.ReadLine

If Incoming Is Nothing Then

Exit Do

Else

returnStr &= Incoming & vbCrLf

End If

Loop

Return returnStr

End Function

End Class

Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Tolgay Gül" <to****@gul.com> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
I need some codes that able to send and read data by serialport in VB.Net
2005 beta 2. I wrote some codes and It can send data via serialport but It
cannot read what I send. I have looked up on internet and msdn and found
some codes and samples but they are also can't read serial port. . It was so easy in VB 6.0 with mscomm object and oncommread events but now I don't
understand how it reads data from serialport. You can see the codes bellow
but while It trys to read data from serialport the program is frozen. What
is it worng and how can I fix it?

Thank you.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
GetSerialPortNames()

End Sub

Private Sub cmdOpenPort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdOpenPort.Click

If comPort.IsOpen Then comPort.Close()

OpenPort(cboPortName.Text)

End Sub

Private Function OpenPort(ByVal PortNo As String) As Boolean

With comPort

If .IsOpen = True Then Close() 'close the port

.PortName = PortNo

.BaudRate = 9600

.Open()

End With

OpenPort = True

End Function

Sub GetSerialPortNames()

' Show all available COM ports.

For Each sp As String In My.Computer.Ports.SerialPortNames

cboPortName.Items.Add(sp)

Next

End Sub

Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSend.Click

SendSerialData(txtOutput.Text)

End Sub

Sub SendSerialData(ByVal data As String)

' Send strings to a serial port.

If comPort.IsOpen = False Then

MsgBox("The port is not open")

Else

comPort.WriteLine(data)

End If

End Sub

Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdRead.Click

txtOutput.Text = ReveiveSerialData()

End Sub

Function ReveiveSerialData() As String

' Receive strings from a serial port.

Dim returnStr As String = ""

Do

Dim Incoming As String = comPort.ReadLine

If Incoming Is Nothing Then

Exit Do

Else

returnStr &= Incoming & vbCrLf

End If

Loop

Return returnStr

End Function

End Class


Hi,

read this:
http://www.codeproject.com/csharp/Se...munication.asp

Greeting

Thomas
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.