Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to add an event handler for serial port timeout?

How to add an event handler for serial port timeout?

Scheduled Pinned Locked Moved C#
helptutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thompsons
    wrote on last edited by
    #1

    I have searched high and low for an answer to this one. I'm sure someone has run into a similar situation. Basically, I set up a serial port with a ReadTimeout of 10 seconds; open it; send a "command" character to a serially attached device and expect an answer within this period of time. If data is received, the serialPort_DataReceived event handler fires and reads the data. However, I want to be able to notify the operator of a problem, should the external device not reply within 10 seconds. This could indicate the device is not attached or not powered up. Appreciate the help, Steve.

    L G 2 Replies Last reply
    0
    • T thompsons

      I have searched high and low for an answer to this one. I'm sure someone has run into a similar situation. Basically, I set up a serial port with a ReadTimeout of 10 seconds; open it; send a "command" character to a serially attached device and expect an answer within this period of time. If data is received, the serialPort_DataReceived event handler fires and reads the data. However, I want to be able to notify the operator of a problem, should the external device not reply within 10 seconds. This could indicate the device is not attached or not powered up. Appreciate the help, Steve.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      thompsons wrote:

      I have searched high and low

      Not high and low enough then. It took me 10 seconds to find this[^] The SerialPort class provides a timeout mechanism on its Read methods. When the read timeout is exceeded, a TimeoutException gets thrown. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        thompsons wrote:

        I have searched high and low

        Not high and low enough then. It took me 10 seconds to find this[^] The SerialPort class provides a timeout mechanism on its Read methods. When the read timeout is exceeded, a TimeoutException gets thrown. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        T Offline
        T Offline
        thompsons
        wrote on last edited by
        #3

        Thanks for the reply. I also found that; it did not directly answer my question. This implies that I need to make a call to serialPort.ReadLine() before any notification occurs in the case of a timeout. One of the reasons folks join these forums is because they want answers in plain English and not the somewhat vague documentation offered on MSDN. Sorry I asked such a silly question that caused your sarcasm to manifest itself. Regards, Steven.

        D L 2 Replies Last reply
        0
        • T thompsons

          Thanks for the reply. I also found that; it did not directly answer my question. This implies that I need to make a call to serialPort.ReadLine() before any notification occurs in the case of a timeout. One of the reasons folks join these forums is because they want answers in plain English and not the somewhat vague documentation offered on MSDN. Sorry I asked such a silly question that caused your sarcasm to manifest itself. Regards, Steven.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          The quality of the question directly dictates the quality of the answer. If you don't start a read operation, you're not going to get a notification that anything showed up. What if your code isn't ready to recieve yet, but there's pending data? That's why it works this way.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          1 Reply Last reply
          0
          • T thompsons

            Thanks for the reply. I also found that; it did not directly answer my question. This implies that I need to make a call to serialPort.ReadLine() before any notification occurs in the case of a timeout. One of the reasons folks join these forums is because they want answers in plain English and not the somewhat vague documentation offered on MSDN. Sorry I asked such a silly question that caused your sarcasm to manifest itself. Regards, Steven.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            So you want to get an event because data that you did not request did not arrive in time? That is not how Windows (or any other OS I am aware of) works. You can get an event when something unexpected happens (e.g. DataReceived, ErrorReceived) or you can get an Exception when something you did order did not succeed for some reason. If you are not satisfied by the DataReceived events (and I can imagine several scenarios where it is insufficient), then you must create your own thread and have that perform synchronous read operations, with or without timeout. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • T thompsons

              I have searched high and low for an answer to this one. I'm sure someone has run into a similar situation. Basically, I set up a serial port with a ReadTimeout of 10 seconds; open it; send a "command" character to a serially attached device and expect an answer within this period of time. If data is received, the serialPort_DataReceived event handler fires and reads the data. However, I want to be able to notify the operator of a problem, should the external device not reply within 10 seconds. This could indicate the device is not attached or not powered up. Appreciate the help, Steve.

              G Offline
              G Offline
              Gideon Engelberth
              wrote on last edited by
              #6

              My normal way to do SerialPort uses an AutoResetEvent to do the timeout control. The general format is something like this:

              dim port as SerialPort
              dim ReceiveResponse as AutoResetEvent

              Sub SendCommand(data() as Byte)
              sendTries = 0

              while sendTries <= MaxTries
              sendTries += 1
              port.Write(data)
              if ReceiveResponse.Wait
              'you got a response
              else
              'you didn't get a response in time
              end if
              end while
              End Sub

              sub PortDataReceivedHandler(sender as object, e as DataReceivedEventArgs)
              if processIncomingData() then
              ReceiveResponse.Set()
              end
              end sub

              Obviously, this is missing a lot of actual code, but it should point you in the right direction.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups