serial port multi thread issue

I have a serial port app that works on a laptop but seems to freeze on the pocketpc. The incoming communication is a constant stream at 9600 baud and it looks like the "comport data received" function doesn't quit even though it is called using "me.invoke".

So my question is, why does this work on the laptop and not on the Dell Axim x51

Here is the code:

Public Class Form1

Public comport As New SerialPort()

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

Try

AddHandler comport.DataReceived, AddressOf OnSerialDataReceived

comport.DataBits = 8

comport.StopBits = StopBits.One

comport.Parity = Parity.Odd

comport.BaudRate = 9600

comport.PortName = "Com1"

comport.Open()

comport.DtrEnable = True

comport.RtsEnable = True

comport.DiscardNull = True

If comport.IsOpen = True Then

Button1.BackColor = System.Drawing.Color.Green

Else

Button1.BackColor = System.Drawing.Color.Red

End If

Catch ex As Exception

MsgBox(ex.Message.ToString)

End Try

End Sub

Private Sub OnSerialDataReceived(ByVal sender As Object,

ByVal args As SerialDataReceivedEventArgs)

Try

Me.Invoke(New EventHandler(AddressOf ReceiveSerialData))

Catch ex As Exception

End Try

End Sub

Sub ReceiveSerialData(ByVal s As Object, ByVal e As EventArgs)

Try

txtReceive.Text = comport.ReadTo("H")

Catch ex As Exception

Label2.Text = ex.Message.ToString

End Try

End Sub

Private Sub Send_Text()

Try

comport.WriteLine(txtSend.Text)

txtSend.Text = ""

Catch ex As Exception

Label2.Text = ex.Message.ToString()

End Try

End Sub



Answer this question

serial port multi thread issue

  • Patrick Sears

    Are you sure COM1 is the appropriate port COM1 is often used by WinCE for debugging purposes and a single serial port on a device can be called as high as COM3 or COM4.

  • GusaMusa

    The code snippet you supplied will call the ReceiveSerialData method in the UI thread. This will lock the UI until the application reads an "H" from the serial stream. You probably want to spin up a new thread or use the thread pool to read the data if you want the application to continue running.

    Ryan Chapman

    .NET Compact Framework


  • enric vives

    COM1 is the right port because I am reading the communication coming in. The problem is the communication is a continuous stream and the read function isn't allowing the rest of the program to run. That is why I used a seperate thread to do the read. This should have solved the problem, but I'm obviously not doing something right.
  • Acco1953

    Thanks Ryan, with the serialport support in 2.0 this is the first multithreaded app I have created. I need to understand how to handle the threads better.
  • serial port multi thread issue