Quantcast
Viewing all articles
Browse latest Browse all 12583

How to speed up my code?

Hi,

My program reads values coming from a microcontroller, writes them to a file and adds the values to a chart plot. The code is composed of a backgroundworker triggered by a timer. The user has the ability to control how fast reading should take place. My problem is: the program is intended to run for a timer interval of at least 0.01s, however it only functions properly for timer intervals greater than 0.1s . Here is the related block of my code:

private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {


                 for (int n=0;n<8;n++){
                     adc_array[n]= this->serialPort1->ReadByte();

                 }

                 *myStream<<period<<";";

                 for(int f=0; f<8; f++){
                     *myStream<<adc_array[f]<<";";
                 }

                 *myStream<<endl;

                

                 backgroundWorker1->CancelAsync();
                 if(backgroundWorker1->CancellationPending){
                     e->Cancel=true;
                     return;
                 }

             }

private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
                
                 chart1->Series["ADC1"]->Points->AddXY(period,adc_array[0]);
                 chart1->Series["ADC2"]->Points->AddXY(period,adc_array[1]);
                 chart1->Series["ADC3"]->Points->AddXY(period,adc_array[2]);
                 chart1->Series["ADC4"]->Points->AddXY(period,adc_array[3]);
                 chart1->Series["ADC5"]->Points->AddXY(period,adc_array[4]);
                 chart1->Series["ADC6"]->Points->AddXY(period,adc_array[5]);
                 chart1->Series["ADC7"]->Points->AddXY(period,adc_array[6]);
                 chart1->Series["ADC8"]->Points->AddXY(period,adc_array[7]);
                 period+=1000*w;
             }

private: System::Void interval_Tick(System::Object^  sender, System::EventArgs^  e) {
                 this->serialPort1->Write(START);

                 backgroundWorker1->RunWorkerAsync();

             }

   running the program for timer interval less than 0.1s pops up an error message: "This BackgroundWorker is currently busy and cannot run multiple tasks concurrently".

Thanks in advance




Viewing all articles
Browse latest Browse all 12583

Trending Articles