C# Windows Form - Serial Port problem / BackgroundWorker












0















I developed a Windows Form application, using C#, that receives data through the serial port sent by a microcontroller. I'm receiving the data using the serialPort_DataReceived event in which I use ReadLine() to read the serial port. Then I use the Invoke method to call a function that will print the received string into a textBox. The problem is that the app can't keep up with the rate at which it receives the strings, for example, the string that I send from the microcontroller has the time at which it was sent and even though it has already passed 2 or 3 seconds the app is still printing the strings that were sent at the 0.2 second. And when I send a string to the microcontroller to stop sending data, the app onlys stops ptinting the data after a while, this is, it keeps printing the values stored in the receiving buffer.



I believe that is happens given the large amount of data that the app receives and the rate (I'm using a baud rate of 115200), one string for every millisecond. This is, I think that the app is always being interrupted by the DataReceived event and it doesn't has time to print the data and starts falling behind.



One way I though of overcome this problem was with multi-threading but I can't figure it out how. I already tried a few examples using the BackgroundWorker but I didn't manage to make it work at all. I'm still a noob in terms of programming Windows Form apps and even C#.



Can anyone confirm the cause of the problem? And how do I make a thread to print the received data?










share|improve this question



























    0















    I developed a Windows Form application, using C#, that receives data through the serial port sent by a microcontroller. I'm receiving the data using the serialPort_DataReceived event in which I use ReadLine() to read the serial port. Then I use the Invoke method to call a function that will print the received string into a textBox. The problem is that the app can't keep up with the rate at which it receives the strings, for example, the string that I send from the microcontroller has the time at which it was sent and even though it has already passed 2 or 3 seconds the app is still printing the strings that were sent at the 0.2 second. And when I send a string to the microcontroller to stop sending data, the app onlys stops ptinting the data after a while, this is, it keeps printing the values stored in the receiving buffer.



    I believe that is happens given the large amount of data that the app receives and the rate (I'm using a baud rate of 115200), one string for every millisecond. This is, I think that the app is always being interrupted by the DataReceived event and it doesn't has time to print the data and starts falling behind.



    One way I though of overcome this problem was with multi-threading but I can't figure it out how. I already tried a few examples using the BackgroundWorker but I didn't manage to make it work at all. I'm still a noob in terms of programming Windows Form apps and even C#.



    Can anyone confirm the cause of the problem? And how do I make a thread to print the received data?










    share|improve this question

























      0












      0








      0








      I developed a Windows Form application, using C#, that receives data through the serial port sent by a microcontroller. I'm receiving the data using the serialPort_DataReceived event in which I use ReadLine() to read the serial port. Then I use the Invoke method to call a function that will print the received string into a textBox. The problem is that the app can't keep up with the rate at which it receives the strings, for example, the string that I send from the microcontroller has the time at which it was sent and even though it has already passed 2 or 3 seconds the app is still printing the strings that were sent at the 0.2 second. And when I send a string to the microcontroller to stop sending data, the app onlys stops ptinting the data after a while, this is, it keeps printing the values stored in the receiving buffer.



      I believe that is happens given the large amount of data that the app receives and the rate (I'm using a baud rate of 115200), one string for every millisecond. This is, I think that the app is always being interrupted by the DataReceived event and it doesn't has time to print the data and starts falling behind.



      One way I though of overcome this problem was with multi-threading but I can't figure it out how. I already tried a few examples using the BackgroundWorker but I didn't manage to make it work at all. I'm still a noob in terms of programming Windows Form apps and even C#.



      Can anyone confirm the cause of the problem? And how do I make a thread to print the received data?










      share|improve this question














      I developed a Windows Form application, using C#, that receives data through the serial port sent by a microcontroller. I'm receiving the data using the serialPort_DataReceived event in which I use ReadLine() to read the serial port. Then I use the Invoke method to call a function that will print the received string into a textBox. The problem is that the app can't keep up with the rate at which it receives the strings, for example, the string that I send from the microcontroller has the time at which it was sent and even though it has already passed 2 or 3 seconds the app is still printing the strings that were sent at the 0.2 second. And when I send a string to the microcontroller to stop sending data, the app onlys stops ptinting the data after a while, this is, it keeps printing the values stored in the receiving buffer.



      I believe that is happens given the large amount of data that the app receives and the rate (I'm using a baud rate of 115200), one string for every millisecond. This is, I think that the app is always being interrupted by the DataReceived event and it doesn't has time to print the data and starts falling behind.



      One way I though of overcome this problem was with multi-threading but I can't figure it out how. I already tried a few examples using the BackgroundWorker but I didn't manage to make it work at all. I'm still a noob in terms of programming Windows Form apps and even C#.



      Can anyone confirm the cause of the problem? And how do I make a thread to print the received data?







      c# multithreading winforms serial-port backgroundworker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 29 '18 at 18:41









      M_LuisM_Luis

      33




      33
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can utilize SerialPort.ReceivedBytesThreshold property and SerialPort.ReadExisting() method (or a StringBuilder instance) to update UI with a batch of strings at once rather than a single string.



          For more information check:





          • Serial port DataReceived firing too much discussions on StackOverflow


          • Top 5 SerialPort Tips article by Kim Hamilton


          • Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly discussion on StackOverflow


          • Quickest way to Update Multiline Textbox with Large Amount of Text discussion on StackOverflow






          share|improve this answer
























          • Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

            – M_Luis
            Dec 29 '18 at 21:39











          • String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

            – Leonid Vasilev
            Dec 30 '18 at 10:31











          • ok thanks for the help.

            – M_Luis
            Dec 30 '18 at 15:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972358%2fc-sharp-windows-form-serial-port-problem-backgroundworker%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can utilize SerialPort.ReceivedBytesThreshold property and SerialPort.ReadExisting() method (or a StringBuilder instance) to update UI with a batch of strings at once rather than a single string.



          For more information check:





          • Serial port DataReceived firing too much discussions on StackOverflow


          • Top 5 SerialPort Tips article by Kim Hamilton


          • Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly discussion on StackOverflow


          • Quickest way to Update Multiline Textbox with Large Amount of Text discussion on StackOverflow






          share|improve this answer
























          • Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

            – M_Luis
            Dec 29 '18 at 21:39











          • String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

            – Leonid Vasilev
            Dec 30 '18 at 10:31











          • ok thanks for the help.

            – M_Luis
            Dec 30 '18 at 15:34
















          0














          You can utilize SerialPort.ReceivedBytesThreshold property and SerialPort.ReadExisting() method (or a StringBuilder instance) to update UI with a batch of strings at once rather than a single string.



          For more information check:





          • Serial port DataReceived firing too much discussions on StackOverflow


          • Top 5 SerialPort Tips article by Kim Hamilton


          • Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly discussion on StackOverflow


          • Quickest way to Update Multiline Textbox with Large Amount of Text discussion on StackOverflow






          share|improve this answer
























          • Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

            – M_Luis
            Dec 29 '18 at 21:39











          • String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

            – Leonid Vasilev
            Dec 30 '18 at 10:31











          • ok thanks for the help.

            – M_Luis
            Dec 30 '18 at 15:34














          0












          0








          0







          You can utilize SerialPort.ReceivedBytesThreshold property and SerialPort.ReadExisting() method (or a StringBuilder instance) to update UI with a batch of strings at once rather than a single string.



          For more information check:





          • Serial port DataReceived firing too much discussions on StackOverflow


          • Top 5 SerialPort Tips article by Kim Hamilton


          • Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly discussion on StackOverflow


          • Quickest way to Update Multiline Textbox with Large Amount of Text discussion on StackOverflow






          share|improve this answer













          You can utilize SerialPort.ReceivedBytesThreshold property and SerialPort.ReadExisting() method (or a StringBuilder instance) to update UI with a batch of strings at once rather than a single string.



          For more information check:





          • Serial port DataReceived firing too much discussions on StackOverflow


          • Top 5 SerialPort Tips article by Kim Hamilton


          • Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly discussion on StackOverflow


          • Quickest way to Update Multiline Textbox with Large Amount of Text discussion on StackOverflow







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 19:23









          Leonid VasilevLeonid Vasilev

          7,13921935




          7,13921935













          • Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

            – M_Luis
            Dec 29 '18 at 21:39











          • String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

            – Leonid Vasilev
            Dec 30 '18 at 10:31











          • ok thanks for the help.

            – M_Luis
            Dec 30 '18 at 15:34



















          • Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

            – M_Luis
            Dec 29 '18 at 21:39











          • String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

            – Leonid Vasilev
            Dec 30 '18 at 10:31











          • ok thanks for the help.

            – M_Luis
            Dec 30 '18 at 15:34

















          Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

          – M_Luis
          Dec 29 '18 at 21:39





          Thanks for answering, changing from ReadLine() to ReadExisting() solved the problem. But now I have another problem that is how do I split the string, to plot in a chart, given that now instead of having one string I have multiple strings stored in just one. The string has following format: "value time n" where value goes from 0 to 1024 and time is in seconds and has 4 decimal places. Before I just had one string at each time so I used .Split(' ') but now given that the string changed I can't make it work, I already tried to do in a loop but with no success. Any suggestion?

          – M_Luis
          Dec 29 '18 at 21:39













          String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

          – Leonid Vasilev
          Dec 30 '18 at 10:31





          String parsing problem seems unrelated to the initial question. If the answer solved your initial problem please consider accepting it and asking a new question to keep discussions of unrelated questions separate.

          – Leonid Vasilev
          Dec 30 '18 at 10:31













          ok thanks for the help.

          – M_Luis
          Dec 30 '18 at 15:34





          ok thanks for the help.

          – M_Luis
          Dec 30 '18 at 15:34


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972358%2fc-sharp-windows-form-serial-port-problem-backgroundworker%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas