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. Algorithms
  4. Serial port takes a long time to respond

Serial port takes a long time to respond

Scheduled Pinned Locked Moved Algorithms
winformsperformancehelp
3 Posts 3 Posters 9 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.
  • M Offline
    M Offline
    Member_14809996
    wrote on last edited by
    #1

    Hi, Im currently making an app on windows forms trying to program a micro over UART. I can achieve it, although it is taking a long time to carry out the program. I have used stopwatches to determine it is my read function taking up the majority of the time. When i try to read the micros response from each command I have to wait for all of it, which is why im using a while loop in the code below, if the returned message size is not what i expect. What im wondering is, if there is any way to speed up this process. The response from the micro should be pretty fast, its running at a baudrate of 115200, meaning the whole 512 kb file should in theory take just over 30 seconds to complete, at the moment it is more than double that, at 80 seconds.

        private string CheckResponse(int mSize)
        {
            //ComPort.DataReceived += SerialPortDataReceived;
    
            string response;
            int intBuffer;
            intBuffer = ComPort.BytesToRead;
            byte\[\] byteBuffer = new byte\[intBuffer\];
            ComPort.Read(byteBuffer, 0, intBuffer);
    
            response = Encoding.ASCII.GetString(byteBuffer);
    
            while(response.Length != mSize)
            {
                intBuffer = ComPort.BytesToRead;
                byteBuffer = new byte\[intBuffer\];
                ComPort.Read(byteBuffer, 0, intBuffer);
                response += Encoding.ASCII.GetString(byteBuffer);
            }
    
            return response;
        }
    

    Any help greatly appreciated. Blair

    Greg UtasG L 2 Replies Last reply
    0
    • M Member_14809996

      Hi, Im currently making an app on windows forms trying to program a micro over UART. I can achieve it, although it is taking a long time to carry out the program. I have used stopwatches to determine it is my read function taking up the majority of the time. When i try to read the micros response from each command I have to wait for all of it, which is why im using a while loop in the code below, if the returned message size is not what i expect. What im wondering is, if there is any way to speed up this process. The response from the micro should be pretty fast, its running at a baudrate of 115200, meaning the whole 512 kb file should in theory take just over 30 seconds to complete, at the moment it is more than double that, at 80 seconds.

          private string CheckResponse(int mSize)
          {
              //ComPort.DataReceived += SerialPortDataReceived;
      
              string response;
              int intBuffer;
              intBuffer = ComPort.BytesToRead;
              byte\[\] byteBuffer = new byte\[intBuffer\];
              ComPort.Read(byteBuffer, 0, intBuffer);
      
              response = Encoding.ASCII.GetString(byteBuffer);
      
              while(response.Length != mSize)
              {
                  intBuffer = ComPort.BytesToRead;
                  byteBuffer = new byte\[intBuffer\];
                  ComPort.Read(byteBuffer, 0, intBuffer);
                  response += Encoding.ASCII.GetString(byteBuffer);
              }
      
              return response;
          }
      

      Any help greatly appreciated. Blair

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      You might want to post this under Hardware & Devices[^], though I imagine someone who can help will find it nonetheless.

      Robust Services Core | Software Techniques for Lemmings | Articles

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      1 Reply Last reply
      0
      • M Member_14809996

        Hi, Im currently making an app on windows forms trying to program a micro over UART. I can achieve it, although it is taking a long time to carry out the program. I have used stopwatches to determine it is my read function taking up the majority of the time. When i try to read the micros response from each command I have to wait for all of it, which is why im using a while loop in the code below, if the returned message size is not what i expect. What im wondering is, if there is any way to speed up this process. The response from the micro should be pretty fast, its running at a baudrate of 115200, meaning the whole 512 kb file should in theory take just over 30 seconds to complete, at the moment it is more than double that, at 80 seconds.

            private string CheckResponse(int mSize)
            {
                //ComPort.DataReceived += SerialPortDataReceived;
        
                string response;
                int intBuffer;
                intBuffer = ComPort.BytesToRead;
                byte\[\] byteBuffer = new byte\[intBuffer\];
                ComPort.Read(byteBuffer, 0, intBuffer);
        
                response = Encoding.ASCII.GetString(byteBuffer);
        
                while(response.Length != mSize)
                {
                    intBuffer = ComPort.BytesToRead;
                    byteBuffer = new byte\[intBuffer\];
                    ComPort.Read(byteBuffer, 0, intBuffer);
                    response += Encoding.ASCII.GetString(byteBuffer);
                }
        
                return response;
            }
        

        Any help greatly appreciated. Blair

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

        There is no point "reading" when the "BytesToRead" equals 0. If you're expecting data, and BytesToRead == 0, you need to "wait" until the count > 0 and "time out" after a certain time if it doesn't. A loop of synchronous reads with no data may be your problem.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        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