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. SerialPort class DataReceived event question

SerialPort class DataReceived event question

Scheduled Pinned Locked Moved C#
questioncsharp
5 Posts 3 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.
  • K Offline
    K Offline
    Ken Booth
    wrote on last edited by
    #1

    In my current C# project, I am using an instance of the SerialPort class. I wanted to examine each character as it came in, so I registered a handler for the DataReceived event and set the ReceivedBytesThreshold property to 1. With these settings, I expected that the event handler would be called for each character received but I find that it only gets called once for every 5-8 characters. This is good for communication efficiency, but for what I want to do, I have to loop through the received characters in my event handler. Does anybody know if this is the expected behavior of this class?

    Richard Andrew x64R L 2 Replies Last reply
    0
    • K Ken Booth

      In my current C# project, I am using an instance of the SerialPort class. I wanted to examine each character as it came in, so I registered a handler for the DataReceived event and set the ReceivedBytesThreshold property to 1. With these settings, I expected that the event handler would be called for each character received but I find that it only gets called once for every 5-8 characters. This is good for communication efficiency, but for what I want to do, I have to loop through the received characters in my event handler. Does anybody know if this is the expected behavior of this class?

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      There is an interesting discussion of this question here[^]

      The difficult we do right away... ...the impossible takes slightly longer.

      K 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        There is an interesting discussion of this question here[^]

        The difficult we do right away... ...the impossible takes slightly longer.

        K Offline
        K Offline
        Ken Booth
        wrote on last edited by
        #3

        Thanks for the pointer.

        -- Regards, Ken I AM

        1 Reply Last reply
        0
        • K Ken Booth

          In my current C# project, I am using an instance of the SerialPort class. I wanted to examine each character as it came in, so I registered a handler for the DataReceived event and set the ReceivedBytesThreshold property to 1. With these settings, I expected that the event handler would be called for each character received but I find that it only gets called once for every 5-8 characters. This is good for communication efficiency, but for what I want to do, I have to loop through the received characters in my event handler. Does anybody know if this is the expected behavior of this class?

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

          I am not surprised, asynchronous receive is a complex process, there are several actors, and some buffers with their sizes and thresholds, some of which one cannot control at all. You should not be lazy and refuse to loop over the bytes you get in one go, that is pretty easy to accomplish. It gets really nasty when you have to implement a message-oriented protocol, where the sender sends two or more messages back-to-back, and your receiver unfortunately receives three or more DataReceived events, each covering an arbitrary part of the messages sent (say first half of first msg, then second half+first half of second msg, finally second half of second message). The only serial receive that goes smoothly is the one that has a fixed message separator (say a NEWLINE) which is guaranteed to appear in between messages and never inside a message (in that case set the NewLine property and use ReadLine). If that is not the case, get ready to collect what you get, whatever amount comes in, then re-packetize it according to your needs. And then, you should carefully consider whether you need some recovery mechanism, something that helps you out in case some character gets damaged or lost... :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          K 1 Reply Last reply
          0
          • L Luc Pattyn

            I am not surprised, asynchronous receive is a complex process, there are several actors, and some buffers with their sizes and thresholds, some of which one cannot control at all. You should not be lazy and refuse to loop over the bytes you get in one go, that is pretty easy to accomplish. It gets really nasty when you have to implement a message-oriented protocol, where the sender sends two or more messages back-to-back, and your receiver unfortunately receives three or more DataReceived events, each covering an arbitrary part of the messages sent (say first half of first msg, then second half+first half of second msg, finally second half of second message). The only serial receive that goes smoothly is the one that has a fixed message separator (say a NEWLINE) which is guaranteed to appear in between messages and never inside a message (in that case set the NewLine property and use ReadLine). If that is not the case, get ready to collect what you get, whatever amount comes in, then re-packetize it according to your needs. And then, you should carefully consider whether you need some recovery mechanism, something that helps you out in case some character gets damaged or lost... :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            K Offline
            K Offline
            Ken Booth
            wrote on last edited by
            #5

            I am quite familiar with the intricacies of communication code. I have written a number of serial drivers for embedded systems over the past 20 years or so. My use of C# has been limited to writing "quick & dirty" utilities for testing my embedded systems. In this case, my code was working fine - I was just seeking a better understanding of the SerialPort class which did not behave as I expected and the Microsoft documentation for it is shallow at best.

            -- Regards, Ken I AM

            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