c# - Reading Serial Communication Visual Studios -


i need background worker. trying read data serial port (works fine button) problem need continuously read serial port, until presses button (close button) on form stop reading. tried doing adding loop, ran infinitely , froze form. have following code, whenever press button read, file created, when press close port button,it says

the i/o operation has been aborted because of either thread exit or application request

any ideas on how fix this?

    using system;     using system.collections.generic;     using system.componentmodel;     using system.data;     using system.drawing;     using system.linq;     using system.text;     using system.windows.forms;     using system.io.ports;     using system.io;      namespace serialcommunication {    public partial class form1 : form {     public form1()     {         initializecomponent();         getavaliableportnames();     }      bool indicator;     streamwriter sw = new streamwriter(@"c:\users\slahiri\desktop\data\jumbo.txt");      void getavaliableportnames()     {         string[] ports = serialport.getportnames();         cmbport.items.addrange(ports);     }      private void btnopen_click(object sender, eventargs e)     {         try         {             if (cmbport.text == "" || cmbbaud.text == "")             {                 messagebox.show("please select port , baud rates");             }             else             {                 serialport1.portname = cmbport.text;                 serialport1.baudrate = convert.toint32(cmbbaud.text);                 serialport1.open();                 progressbar1.value = 100;                  groupbox2.enabled = true;                 btnread.enabled = true;                  btnopen.enabled = false;                 btnclose.enabled = true;             }         }         catch (unauthorizedaccessexception)         {             txtread.text = "unauthorized acess";         }     }      private void btnclose_click(object sender, eventargs e)     {         serialport1.close();         progressbar1.value = 0;         btnclose.enabled = false;         btnread.enabled = false;          btnopen.enabled = true;         indicator = true;       }        private void btnread_click(object sender, eventargs e)     {         if (btnread.text == "read")         {              btnread.text = "stop , close port";             progressbar1.maximum = 1000;             progressbar1.value = 0;             backgroundworker1.runworkerasync(progressbar1.maximum);             indicator = false;         }         else         {              backgroundworker1.workersupportscancellation = true;              backgroundworker1.cancelasync();          }     }       private void backgroundworker1_dowork(object sender, doworkeventargs e)     {         try         {             int max = (int)e.argument;             int n = 0;             indicator = false;                         {                 //write serial writer                 sw.writeline(serialport1.readline());                 if (backgroundworker1.cancellationpending)                 {                     sw.flush();                     sw.close();                     e.cancel = true;                     break;                 }                // backgroundworker1.reportprogress(n++);             }              while (indicator ==false);         }         catch (timeoutexception)         {             //close serial writer & messagebox             //txtread.text = "time out!";             messagebox.show("timeout exception");         }      }      private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e)     {         //progressbar1.value = e.progresspercentage;     }      private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e)     {          btnread.text = "read";         //close serial writer     }  } 

}

since have do-while construct evaluate condition @ end! when hit close button , close serialport backgroundworker still active , running in background trying read serialport. enters compartment , tries read closed port, running agains closed door :) leads exception.

you solve

1) putting while-evaluation on top , rid of do statement (making simple while-loop). way serialport.readline not executed anymore, because set indicator = true; when hitting close button.

may in case should put setting of indicator first line before closing port:

private void btnclose_click(object sender, eventargs e) {     try     {         indicator = true;         serialport1.close(); 

or

2) put if clause makes sure read if port open!

if (serialport1.isopen) {     sw.writeline(serialport1.readline()); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -