c# - Loop get paused/diactivated inside catch() block -


i trying send at commands com ports, can find gsm dongle. below code

public bool findgsmmodem()     {         bool sendstatus = false;          //get available ports         string[] serialports = serialport.getportnames();          (int = 0; < serialports.length; i++)         {             console.writeline(serialports[i]);         }          //iterate through ports sending @ commands find modem         (int = 0; < 1; i++)         {             try             {                 //port.portname = serialports[i].trim();                 port.portname = "com7";                 openport();                  string res = atcommandcaller("at", 300,"unable connect phone"); //connecting phone                 //res = atcommandcaller("at+cmgf=1", 300); //setting message format                  sendstatus = true;                  break;             }             catch (system.invalidoperationexception ex)             {                 //port.portname = null;                 port.close();                 autoinitializer();                 //port = new serialport();                 continue;                 //throw ex;             }         }          return sendstatus;     } 

here how call method inside class

if (sms.findgsmmodem()) {     messagebox.show("modem found: " + sms.getportname()); } else {     messagebox.show("no modem found"); } 

ok, in findgsmmodem() method if use port.portname = "com5"; above second code works , display message. because modem in com5 , value hard coded, statement not reach catch() block.

but, if use port.portname = serialports[i].trim(); or port.portname = serialports[i]; seems nothing happening instead of printing port names (inside findgsmmodem()). following ports being printed

com1 com2 com8 com9 com5 com4 com3 

as can see, com5, port gms modem exists in 5th element of array, findgsmmodem() calls catch() part before access com5.

i believe not getting when port.portname = serialports[i].trim() used because goes catch() part , terrible happens there.

any idea?

here openport() method

public void openport()         {             try             {                 port.datareceived += new serialdatareceivedeventhandler(port_datareceived);                  if (!port.isopen)                 {                     port.open();                 }                  port.rtsenable = true;                 port.dtrenable = true;             }             catch (exception ex)             {                 throw ex;             }         } 

edit here weirdest part. noticed catch() block never reached when loop called! tried ex.message print stack trace, , didn't print anything!

    catch (exception ex) 

this trouble catch-em-all exception handling. getting invalidoperationexception because change portname property on opened port. that's bug in code, nothing went wrong serial port.

you'll need call close() method if find out port not connected gsm modem.

then can't call open() again on same serialport instance, takes time internal worker thread shut down. best thing create new instance of serialport instead of trying keep using same 1 repeatedly.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -