c# - Stack overflow while reading from Serial Port -


this question has answer here:

i trying read , write serial port , have encountered 2 problems (the second 1 might derive first one).

the first 1 can't clear serial port buffer.i have tried discardinbuffer , out no success.

i continued , trying write serial port 1 separate thread.i keep getting "stackoverflow exception".

i mention want end reading thread using event fires when press "q"...which in turn uses function makes bool false.

i thought exception might because using string type ..and being immutable changed stringbuilder.

namespace arduinobidirectional {     class program     {          public static serialport serialport;          public static bool _continue;          public  delegate void del();          public static event del quitevent;          public static stringbuilder senddata { { return senddata; } set { senddata = value;if (senddata.equals("q")) quitevent(); } }          static void main(string[] args)         {              del ex = new del(stopthread);              serialport = new serialport();             serialport.portname = "com3";//portops.setportname(serialport.portname);             serialport.baudrate = 9600; //portops.setbaudrate(serialport.baudrate);             serialport.readtimeout = 2000;// portops.settimeout(serialport.readtimeout);             _continue = true;             serialport.open();             serialport.discardinbuffer();             serialport.discardoutbuffer();              thread readthread = new thread(readfromserial);             thread writethread = new thread(writetoserial);              readthread.start();             writethread.start();              readthread.join();             writethread.join();             serialport.close();              console.readline();          }         public static void writetoserial()         {             while(serialport.isopen)             {                 console.readline();             }         }         public static void stopthread()         {             _continue = false;          }         public static void readfromserial()         {              while (_continue)             {                 console.writeline(senddata.clear().append(serialport.readline()));              }         }     } } 

you confusing auto property regular property. written, have no actual senddata member variable stringbuilder. senddata getter , setter recursively call themselves, leads stack overflow.

this work better:

private static stringbuilder _senddata; public static stringbuilder senddata {      { return _senddata; }      set {          _senddata = value;         if (_senddata.equals("q")) quitevent();      }  } 

although fixes immediate stack overflow problem, there other problems (where want create stringbuilder?, why test q in setter?, don't console.readline(), etc.). think these separate questions.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -