c# - System.Reflection.TargetInvocationException was unhandled in mscorlib.dll -
i'm trying read serial data dmm , think data gets corrupted sporadically. 95% of time can read data , display textbox fine once in while error:
system.reflection.targetinvocationexception unhandled in mscorlib.dll.
the innerexception is: message=input string not in correct format.
i'm new windows wpf , c# programming think means data received wasn't terminated properly.
here receive() code:
private delegate void updateuitextdelegate(string text); private void recieve(object sender, system.io.ports.serialdatareceivedeventargs e) { // collecting characters received our 'buffer' (string). received_data = serial.readline(); dispatcher.invoke(dispatcherpriority.send, new updateuitextdelegate(writedata), received_data); }
what's proper way handle corrupted data?
update: here's writedata():
private void writedata(string text) { string mytextstripped; string avgtext; string textchar; string currentfilteredtext; double current; double difference; int range; textbox2.text = mytext; // display raw string data mytextstripped = text.substring(1, 7); // remove chars after 7th char mytextstrippedtextbox.text = mytextstripped; // display trimmed string if (mytextstripped[5] == '2') // range 0 - 59.9 mv { range = 1; mytextstripped = mytext.remove(4); // keep first 4 chars current = float.parse(mytextstripped); current = current / 100; mytextstrippedtextbox.background = brushes.yellow; } else if (mytextstripped[5] == '3') // range 60mv - 600mv { range = 2; mytextstripped = mytext.remove(4); // keep first 4 chars current = float.parse(mytextstripped); current = current / 10; mytextstrippedtextbox.background = brushes.yellowgreen; } else // range 600mv+ { range = 3; mytextstripped = mytext.remove(4); // keep first 4 chars current = float.parse(mytextstripped); mytextstrippedtextbox.background = brushes.palevioletred; } // filter results because readings jump visually determine value difference = (current - currentfiltered); currentfiltered = currentfiltered + (filterfactor * difference); avgtext = currentfiltered.tostring(".000"); avgtext = avgtext.trimstart('0'); currenttextbox.text = avgtext; }
Comments
Post a Comment