Home All Groups Group Topic Archive Search About

Bug in SerialPort: Event Late

Author
14 Jun 2006 6:05 PM
herbert
Since the feedback site doesn't work for the 2nd day, I'm posting this here:

Handling binary IO using serialport class is a mess. The following source
reads all bytes received after sending hex 88. The connected device returns 6
binary bytes.

The loop in the main thread works perfect.
The received event does not fire for every byte on time! However no data is
lost: When the next  6-byte message is received, the event handler is fired
and all the bytes are returned to the app.

Imports System.IO.Ports
Imports System.Threading
Imports System.IO
Imports System.Text

Module XXX
    Const PortName As String = "COM4"   'Com4 is USB/RS-232 Adapter
    Dim myPort As SerialPort

    Sub Main()
        Console.Title = "xxx"

        'get the list of all ports known on this computer
        'if parameter port is not included then throw exception
        If Not My.Computer.Ports.SerialPortNames.Contains(PortName.ToUpper)
Then
            'you must connect the USB Adapter!
            Throw New Exception("Port " & PortName & " does not exist.")
        End If

        myPort = New SerialPort(PortName.ToUpper)           'convert
portname to upper case before accessing it
        myPort.BaudRate = 9600
        myPort.DataBits = 8
        myPort.Parity = IO.Ports.Parity.None
        myPort.StopBits = IO.Ports.StopBits.One
        myPort.Handshake = Handshake.None                  
        myPort.DtrEnable = True                                        
myPort.ReceivedBytesThreshold = 1
        myPort.Encoding = System.Text.Encoding.UTF8         'support 8bit
per byte transparent!

        'open the port
        Try
            AddHandler myPort.DataReceived, AddressOf Received
            AddHandler myPort.ErrorReceived, AddressOf ErrRcvd
            myPort.Open()                                       'open the port
        Catch ex As UnauthorizedAccessException
            Console.WriteLine("Cannot access port " & PortName & ". Maybe in
use by other application.")
            Console.WriteLine("Hit ENTER to exit.")
            Console.ReadLine()
            Exit Sub
        End Try
        myPort.DiscardOutBuffer()
        myPort.DiscardInBuffer()

        Try
            Console.WriteLine("Send x88.")
            'myPort.Write(Chr(&H88))  'this doesn't work
            Dim wb(0) As Byte
            wb(0) = &H88
            myPort.Write(wb, 0, 1)

            While True                  'read all bytes from line and output
them in hex to console
                Dim myByte As Byte = CType(myPort.ReadByte, Byte)  
'readbyte returns integer!!!
                Console.Write("." & String.Format("{0:X2}", myByte))
            End While
            Thread.Sleep(Threading.Timeout.Infinite)
        Catch ex As Exception
            Console.WriteLine("Error: " & ex.Message)
        Finally
            myPort.Dispose()
        End Try
        Console.WriteLine("Hit enter to exit.")
        Console.ReadLine()
    End Sub

    Private Sub Received(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs)
        Console.WriteLine(",")
        'Dim myByte As Byte = CType(myPort.ReadByte, Byte)   'readbyte
returns integer!!!
        'Console.Write("." & String.Format("{0:X2}", myByte))

    End Sub
    Private Sub ErrRcvd(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialErrorReceivedEventArgs)
        Console.WriteLine("Error received")
    End Sub
    'Private Sub Pinchanged(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialPinChangedEventArgs)
    '    Console.WriteLine("Pin changed")
    'End Sub
End Module

AddThis Social Bookmark Button