c# - Reading data from a RS232 port with RFID -


update

i'm reading rfid card, , signing it's value textbox.text.
it's working in first time pass card in rfid reader, form second time pass card, instead shows whole card's id on textbox.text, it's showing last letter of card's id. can see whole number appear , vanish fast on textbox, second time pass card, last letter remain in textbox.
may causing ?

here's current code:

using system; using system.windows.forms; using system.io.ports; using system.text; using system.text.regularexpressions;  namespace rfid_reader {     public partial class portaserial : form     {         private serialport porta_serial = new serialport("com1", 9600, parity.none, 8, stopbits.one);           public portaserial()         {             initializecomponent();         }          private void portaserial_load(object sender, eventargs e)         {             try             {                 if (porta_serial.isopen)                 {                     porta_serial.close();                 }                 porta_serial.open();                 porta_serial.dtrenable = true;                 porta_serial.datareceived += new serialdatareceivedeventhandler(recebe_data);             }             catch (exception ex)             {                 throw new exception(ex.message);             }         }          void recebe_data(object sender, serialdatareceivedeventargs e)         {             try             {                                string oi = porta_serial.readexisting().tostring();                 setlabel(oi);             }             catch (exception ex)             {                 throw new exception(ex.message);             }         }          void setlabel(string s)         {             try             {                 if (this.invokerequired)                 {                     this.invoke(new action<string>(setlabel), s);                     return;                 }                 textbox1.text = removespecialcharacters(s);                        }             catch (exception ex)             {                 throw new exception(ex.message);             }         }                 public static string removespecialcharacters(string str)         {             stringbuilder sb = new stringbuilder();                         foreach (char c in str)             {                 if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')                 {                     sb.append(c);                 }             }             return sb.tostring();         }     } } 

in order access elements of gui (textbox, label, ...) need running in ui thread. datareceived running in thread. can change ui thread invoke this

void setlabel(string s) {    if (this.invokerequired) {       this.invoke (new action<string>(setlabel), s);       return;    }     label1.text = s; } 

but beware - if need access different parts of gui (e.g. label and textbox), should "collect" invoke's because every call takes time. may consider begininvoke instead of invoke not blocking receive thread. however, should read details in msdn or google more difficult examples.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -