locked
TimeoutException in serial port

    Question

  •  

    I am using in "SerialPort_DataReceived" event to received data from serial port.

    I would like to use "TimeoutException" for "ReadTimeout" and set it to 1 sec.

    How can I integrate "TimeoutException" with "SerialPort_DataReceived"?

     

     

    See sample relevant code:

     

     

    'Form

    Public Class Serial_Port_Form

     

    Private Sub SendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendButton.Click

            Dim XmtBuffre(5) As Byte

           

            XmtBuffre(0) = ENUM_OPCODE_TYPE.EN_WRITE_FLASH_CMD_LSB

            XmtBuffre(1) = ENUM_OPCODE_TYPE.EN_WRITE_FLASH_CMD_MSB

     

            XmtBuffre(2) = BytesNumberLSB

            XmtBuffre(3) = BytesNumberMSB

     

     

      ChecksumLSM = ENUM_OPCODE_TYPE.EN_WRITE_FLASH_CMD_MSB Xor _

                    ENUM_OPCODE_TYPE.EN_WRITE_FLASH_CMD_LSB Xor _

                          BytesNumberLSB Xor BytesNumberMSB

     

     

            For i = 0 To (XmtBuffre.Length - 3)

                ChecksumLSM = ChecksumLSM Xor XmtBuffre(i)

            Next

     

            XmtBuffre(4) = ChecksumLSM

            XmtBuffre(5) = ChecksumMSM

     

            SerialPort.Write(XmtBuffre, 0, XmtBuffre.Length)

        End Sub

     

    End Class

     

     

     

    'Module

    Module Serial_Port_Module

     

    Public Err As New IO.Ports.SerialError

    Public WithEvents SerialPort As New IO.Ports.SerialPort

     

    Public RxWholeBuffer As New ArrayList

     

     

    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived

           

      Count = SerialPort.BytesToRead

            Dim RxSubBuffer(0 To Count - 1) As Byte

            SerialPort.Read(RxSubBuffer, 0, Count)

     

            RxWholeBuffer.AddRange(RxSubBuffer)

    End Sub

     

      End Module

     

    Monday, April 23, 2007 8:58 AM

Answers

  •  

    Hopefully I have understood what you are seeking; if not, please accept my apologies.

     

    The call to the Write method is blocking therefore the timeout exception can be raised by it ....

     

    serialPort.WriteTimeout = 1000

    Try : serialPort.Write(msg, 0, msg.Length)

    Catch ex As TimeoutException ...........

    End Try

     

    Is that what you were seeking?

     

    Richard

    Monday, April 23, 2007 10:24 AM

All replies

  •  

    Hopefully I have understood what you are seeking; if not, please accept my apologies.

     

    The call to the Write method is blocking therefore the timeout exception can be raised by it ....

     

    serialPort.WriteTimeout = 1000

    Try : serialPort.Write(msg, 0, msg.Length)

    Catch ex As TimeoutException ...........

    End Try

     

    Is that what you were seeking?

     

    Richard

    Monday, April 23, 2007 10:24 AM
  • Thanks Ricdard, sorry it is not what I am looking for.

    Somebody, any ideas…

    Wednesday, April 25, 2007 6:31 AM
  • Can you explain a little more with regards to the problem you are facing / the needs of your application?
    Wednesday, April 25, 2007 8:34 AM
  • My application communicates with external device under RS232 or RS485.

    I need to implement the following requirements:

      

    Retransmission is required when one of the following scenarios occurs:

    §         Only part of the message received (not all N bytes arrived).

    §         Checksum error.

    §         Error occurred on communication port.

     

     Retransmission flow:

    1)            Retransmission every 1 second (Timeout).

    2)            Maximum 3 retransmissions – when 3 failed send Error NACK message.

    Monday, April 30, 2007 7:06 AM