none
serialPort.BytesToRead is always zero in .net framewor 3.0, It works fine on .net 2.0 RRS feed

  • Question

  • Hi all,

    I am working on winForm with serialPort. but it works fine on .net 2.0. However, it doesn't work on .net 3.0. fllowing is my code.

    Public Function init(ByVal portName As String, ByVal portRate As Integer, ByVal portDataBits As Integer, ByVal PortstopBits As StopBits, ByVal portParity As Parity) As Boolean
            sport.PortName = portName
            sport.BaudRate = portRate
            sport.DataBits = portDataBits
            sport.StopBits = PortstopBits
            sport.Parity = portParity
            sport.Open()
        End Function
    Public Sub sendData(ByVal str As String)
            Dim sList As New Generic.List(Of String)
            While str.Length >= 2
                sList.Add(Mid(str, 1, 2))
                If str.Length = 2 Then
                    str = ""
                Else
                    str = Mid(str, 3)
                End If
            End While
            Dim SendDataByte(sList.Count - 1) As Byte
            For i As Integer = 1 To sList.Count
                SendDataByte(i - 1) = Long.Parse(sList.Item(i - 1), Globalization.NumberStyles.HexNumber)
            Next
            Try
                sport.Write(SendDataByte, 0, SendDataByte.Length)
            Catch ex As Exception
            End Try
        End Sub
     Public Function reciveData() As String
            Dim str As String = ""
            Dim quitLoop As Boolean = False
            Dim startReading As Boolean = False
            Dim times As Integer = 0
            Try
                Do
                    times = times + 1
                    If startReading Then Threading.Thread.Sleep(800)
                    Dim byteToRead As Integer = sport.BytesToRead
                    If byteToRead > 0 Then
                        Dim dataBuf(byteToRead) As Byte
                        Dim bytesRead As Integer = 0
                        bytesRead = sport.Read(dataBuf, 0, byteToRead)
                        For i As Integer = 0 To bytesRead - 1
                            str = str & handleDecToHex(dataBuf(i))
                        Next
                        startReading = True
                    Else
                        If startReading Then
                            quitLoop = True
                            startReading = False
                        End If
                    End If
                    System.Threading.Thread.Sleep(500)
                Loop While (Not quitLoop) AndAlso times < 20
            Catch ex As Exception
                MsgBox("reciveData Error!" & ex.Message)
            End Try
            If times >= 20 Then
                str = "time out"
            End If
            Return str
        End Function

    I have tageted my app to .net 3.0, .net 3.5, .net 4.0, It also doesn't work in that machine (installed .net 3.0 ), I'm not sure what's wrong with it. 

    thx.

    Monday, January 14, 2013 3:05 AM

Answers

  • @ after_base

    Your code with all that use of Sleep is what I would regard as very bad programming. Make it event driven instead based on the DataReceived event. You can find all the information you need about how to do it in my SerialPort tutorial.

    That said, .Net 2.0 is so far the only version of .net where you can live with the bugs (it crashes if you remove a USB-serial converter, but otherwise it works)! In .Net 3.0, nothing works when it comes to serialPort so forget that! If you don't use a USB-serial converter, you can be lucky that .Net 3.5 SP1 works, but with a USB-serial converter, it looses bytes! Due to an "Unknown error" I have not been able to test .Net 4.0.

    I use .Net 2.0 myself.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Monday, January 14, 2013 8:27 AM

All replies

  • Add a handler for the DataReceived event.  Put a Console.WriteLine("Data received") statement in the handler.  Does the DataReceived event fire?
    Monday, January 14, 2013 3:27 AM
  • @ after_base

    Your code with all that use of Sleep is what I would regard as very bad programming. Make it event driven instead based on the DataReceived event. You can find all the information you need about how to do it in my SerialPort tutorial.

    That said, .Net 2.0 is so far the only version of .net where you can live with the bugs (it crashes if you remove a USB-serial converter, but otherwise it works)! In .Net 3.0, nothing works when it comes to serialPort so forget that! If you don't use a USB-serial converter, you can be lucky that .Net 3.5 SP1 works, but with a USB-serial converter, it looses bytes! Due to an "Unknown error" I have not been able to test .Net 4.0.

    I use .Net 2.0 myself.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Monday, January 14, 2013 8:27 AM