SerialPort read VB.Net 2005 Beta 2

T

Tolgay Gül

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
 
T

Thomas Wenning

Tolgay Gül said:
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/SerialCommunication.asp

Greeting

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top