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

Serial port takes a long time to respond

Scheduled Pinned Locked Moved Hardware & Devices
winformsperformancehelp
3 Posts 3 Posters 13 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

    L P 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

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

      Take a look at Serial Port I/O[^]

      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

        P Offline
        P Offline
        Patrice T
        wrote on last edited by
        #3

        Member 14809996 wrote:

        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.

        You missed a couple things: - In serial communications, every byte is preceded by at least a start bit and a stop bit, other settings like parity can add other delay. This make at least 10 bits for a byte transmitted, this make sending 512kB just under 45 secondes at 100% efficient. - Any operation like reading the 512kB from storage will add delay. - Any one serious with serial communication, for such a file, will encapsulate the data within a communication protocol to ensure data is not corrupted on the other side. This imply mode data to transmit and some time to ensure data integrity. So 80 secondes is not that bad.

        Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

        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