Socket.SocketIntefaces.Serial.cs ReadLineThread issue (because of stringbuilder bug)


Socket.SocketIntefaces.Serial.cs SocketInterfaces ReadLineThread is broken because of an apparent bug in the StringBuilder class. I tracked down some serial port issues and discovered that StringBuilder.ToString(0,i) is not returning the correct substring. I have reported the StringBuilder bug, but I suggest a simple work-around to make the Serial class functional. Currently the SerialInterfaces class sends truncated data strings from the ReadLineThread. e.g.
#VER"NV-I8G FWv2 (vs. #VER"NV-I8G FWv2.66 HWv0")
gadgeteer-32422 code (https://gadgeteer.codeplex.com/SourceControl/latest#Main/GadgeteerCore/Gadgeteer42/Socket.SocketInterfaces.Serial.cs):
 string line = outcome.ToString(lastStart, i - lastStart);
The following code works around this issue by using ToString(), instead of ToString(int,int). From my understanding, I do not believe there is an impact to performance. (I but I have not peeked at the compiled code)
 string line = outcome.ToString().Substring(lastStart,i-lastStart);  //  work-around
Mostly unrelated comment: is there a reason why ReadLineThread only reads one character at a time? I get much better performance reading into a 128 bit buffer. I am guessing it is to minimize the memory footprint and not for another technical reason.


jamesscott wrote Feb 28, 2014 at 5:42 PM

Thank you for this detailed investigation. We used the workaround you suggested in 2.43.900.