c# - GSM communication, cross threading -
i'm bit new in c# programming somehow managed pickup project requires great skill , knowledge gsm (sms) communication using serialport.
using system; using system.collections.generic; using system.componentmodel; using system.drawing; using system.data; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.io.ports; namespace smsget { public partial class smslogpanel : usercontrol { #region default constructor public smslogpanel() { initializecomponent(); serialport1 = new serialport(); serialport1.databits = 8; serialport1.dtrenable = true; serialport1.encoding.equals("iso-8859-1"); serialport1.handshake = handshake.requesttosend; serialport1.parity = parity.none; serialport1.writetimeout = 300; serialport1.stopbits = stopbits.one; checklink(); } #endregion #region checking communication , setting user controls... private void checklink() { getvalues value = new getvalues(); string com = value.getport(); int baud = value.getbaud(); int timeot = value.gettimeout(); serialport1.portname = com; serialport1.baudrate = baud; serialport1.readtimeout = timeot; serialport1.open(); if (serialport1.isopen) { label1.visible = true; } else { messagebox.show("komunikacija sa modemom se ne može uspostaviti, molimo postavite novu konfiguraciju...!"); this.controls.clear(); smsconfigpanel cfg = new smsconfigpanel(); cfg.show(); this.controls.add(cfg); } serialport1.close(); } #endregion #region setiranje timer-a... private void timer1_tick(object sender, eventargs e) { timer1.stop(); execcommand(); timer1.start(); } #endregion #region seting handler , executing @ commands public void execcommand() { serialport1.datareceived += new serialdatareceivedeventhandler(getresponse); serialport1.open(); //prazni se buffer da se ne pokupe neke vrijednosti koje ne trebaju... serialport1.discardinbuffer(); if (!serialport1.isopen) { messagebox.show("modem nije spojen, molimo provjerite konfiguraciju...!"); timer1.stop(); } serialport1.write("at+cmgf=1" + (char)(13)); serialport1.write("at+cmgl=\"all\"" + (char)(13)); } public void getresponse(object sender, serialdatareceivedeventargs e) { serialport serport = (serialport)sender; string input = serport.readexisting(); if (input.contains("error")) { //textbox2.text = ""; } else if (input.contains("+cmti:")) { serialport1.write("at+cmgf=1" + (char)(13)); serialport1.write("at+cmgl=\"all\"" + (char)(13)); } else if (input.contains("+cmgl:")) { textbox1.text = input; } else { return; } serialport1.close(); } #endregion }
}
i think somehow managed create bugs in these areas (not opening/closing serialport1 ports, input data in getresponse(object sender, serialdatareceivedeventargs e) uses separate thread cannot passed textbox1 due cross thread issue..., , wrong @ command reading unreaded messages has been received...)
if can me this, grateful...
thank , best regards.
reason cross thread exception:
you trying update gui element on thread other 1 on created. not allowed.
solution:
to avoid cross thread exception. should update code this
this.invoke((methodinvoker) delegate { // update textbox here });
Comments
Post a Comment