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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. SerialPort.ReadTimeout issue

SerialPort.ReadTimeout issue

Scheduled Pinned Locked Moved C#
helpquestionperformance
6 Posts 5 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.
  • Y Offline
    Y Offline
    yeah1000
    wrote on last edited by
    #1

    Hello I am using serialport to send and receive some data. The problem is that when i use the serialport.Read(buffer, 0, x) method (x being the number of bytes i need to receive) it does not actually wait for the x nr of bytes to be available in the serialport buffer but reads what is available at certain time. I can use the BytesToRead property to wait for the x bytes to be available in the serialport buffer but then i have no way of detecting readTimeout(). I figured i could use a custom timer, but is it the best way (since i would need to start and stop the timer at high speed)? Any ideas are welcome. ty

    R L P 3 Replies Last reply
    0
    • Y yeah1000

      Hello I am using serialport to send and receive some data. The problem is that when i use the serialport.Read(buffer, 0, x) method (x being the number of bytes i need to receive) it does not actually wait for the x nr of bytes to be available in the serialport buffer but reads what is available at certain time. I can use the BytesToRead property to wait for the x bytes to be available in the serialport buffer but then i have no way of detecting readTimeout(). I figured i could use a custom timer, but is it the best way (since i would need to start and stop the timer at high speed)? Any ideas are welcome. ty

      R Offline
      R Offline
      Rashmi_Karnam
      wrote on last edited by
      #2

      I have worked with timers, but it is not good practice to use it, as it consumes much of CPU memory and slows down the process . Better go for Threads, where you have Sleep() method which can be used till you receive all data to your serial port and then use Stop() method. I am not sure this is the Best way. Its just a suggession. :)

      Rashmi.M.K

      1 Reply Last reply
      0
      • Y yeah1000

        Hello I am using serialport to send and receive some data. The problem is that when i use the serialport.Read(buffer, 0, x) method (x being the number of bytes i need to receive) it does not actually wait for the x nr of bytes to be available in the serialport buffer but reads what is available at certain time. I can use the BytesToRead property to wait for the x bytes to be available in the serialport buffer but then i have no way of detecting readTimeout(). I figured i could use a custom timer, but is it the best way (since i would need to start and stop the timer at high speed)? Any ideas are welcome. ty

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Try to look at it differently. Why don't you simply buffer the bytes yourself. 1) Read serial bytes and add them to your buffer 2) if you have the required amount of bytes or more in your buffer, remove the number you expect and process them (leaving those which are not yet processed in the buffer) 3) back to step 1 This is obviously a polling loop. If you have a WinForms user interface, this loop will lock it up. Therefore you would have to run this loop in a separate worker thread. And it might be a good idea, not to let this thread run under full steam. I don't know what timing interval might be sufficient for your application. Let's just say you want the loop check for new serial bytes once every second, determining a suitable timing value is up to you. You would then have to add the line Thread.Sleep(1000); This excludes the thread from getting any processor time for 1000 milliseconds(= one second). This way your application will use the CPU more economically. Edit: I just saw that someone had the same idea before me

        A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

        modified on Wednesday, March 10, 2010 7:20 AM

        M 1 Reply Last reply
        0
        • Y yeah1000

          Hello I am using serialport to send and receive some data. The problem is that when i use the serialport.Read(buffer, 0, x) method (x being the number of bytes i need to receive) it does not actually wait for the x nr of bytes to be available in the serialport buffer but reads what is available at certain time. I can use the BytesToRead property to wait for the x bytes to be available in the serialport buffer but then i have no way of detecting readTimeout(). I figured i could use a custom timer, but is it the best way (since i would need to start and stop the timer at high speed)? Any ideas are welcome. ty

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          In the past I've used sort of a two-part technique; the first part is as the previous poster describes: get all the available data, pass it along, sleep, repeat The second part receives those bytes/characters and performs the higher-level task of determining whether or not enough data has arrived, breaking it into proper records/messages and passing those up the chain. The bottom is breaking the task into smaller parts, encapsulation, detail hiding, etc. P.S. I still remember a football coach chanting, "get it, get rid of it; get it, get rid of it; get it, get rid of it..." :sigh:

          1 Reply Last reply
          0
          • L Lost User

            Try to look at it differently. Why don't you simply buffer the bytes yourself. 1) Read serial bytes and add them to your buffer 2) if you have the required amount of bytes or more in your buffer, remove the number you expect and process them (leaving those which are not yet processed in the buffer) 3) back to step 1 This is obviously a polling loop. If you have a WinForms user interface, this loop will lock it up. Therefore you would have to run this loop in a separate worker thread. And it might be a good idea, not to let this thread run under full steam. I don't know what timing interval might be sufficient for your application. Let's just say you want the loop check for new serial bytes once every second, determining a suitable timing value is up to you. You would then have to add the line Thread.Sleep(1000); This excludes the thread from getting any processor time for 1000 milliseconds(= one second). This way your application will use the CPU more economically. Edit: I just saw that someone had the same idea before me

            A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

            modified on Wednesday, March 10, 2010 7:20 AM

            M Offline
            M Offline
            mmdullah
            wrote on last edited by
            #5

            Would you please send me sample code in c#

            L 1 Reply Last reply
            0
            • M mmdullah

              Would you please send me sample code in c#

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Yes, sure. Meanwhile, would you please get this many-to-many relationship between two database tables, both with composite primary keys to work? With NHibernate of course. My bosses have little understanding for me playing around with serial ports right now. Seriously, you already had the code for reading from the serial port working. All else you need you will find in the articles here. Search for 'threads' or 'worker threads'. The rest is up to you. Isn't it a bit unlikely that somebody has just the solution to your problem at hand?

              A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

              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