c# - While loop Does not finish even when statement goes false -
i'm writing winform application control encoder engine via serial port. protocol quite simple, send 1st command ask engine moving, , 2nd command confirm it's been in new position, , on other places. here code this:
string dataread =""; serialport1.write("p.1=2950\r\n"); //location register (2950) serialport1.write("^.1\r\n"); //moving command while (dataread.contains("px.1=2950") == false) { serialport1.write("?96.1\r\n"); //ask current location respond3 = serialport1.readexisting(); dataread = string.concat(dataread, respond3); } //keep moving 710 after stop @ 2950 serialport1.write("p.1=710\r\n"); //location register (710) serialport1.write("^.1\r\n"); //moving command
the problem is, when debug app, it's stuck in while-loop. if break all, , continue again, pass. respond3
used output engine. whenever gets correct respond, while-loop finish.
the problem code how readexisting
works. important part is:
reads immediately available bytes, based on encoding, in both stream , input buffer of serialport object.
emphasis mine.
when you're debugging, io port has more time read data, you'll long string in input so:
"?96.1\r\n?96.1\r\n?96.1\r\n?96.1\r\npx.1=2950\r\n"
however, when you're not debugging code runs faster, , get: "?"
followed "9"
etc.
as such immediately available string never match string "px.1=2950"
, loop never stop.
what want use, given newline delimited string using, readline
method.
Comments
Post a Comment