I found 2 bugs in the serial port code from the latest gadgeteer souce (gadgeteer-32422).
I was wondering what was the best way to report these.
- The Serial42/Serial.cs class has duplicate registrations to Interface.LineReceived
https://gadgeteer.codeplex.com/SourceControl/latest#Main/GadgeteerCore/Libraries/Core/Serial42/Serial.cs
I believe the registration of the Interface.LineReceived event was meant to be handled in the AutoReadLineEnabled method (using the value of _autoReadLineEnabled to track if it is currently registered).
However, inside:
public event LineReceivedEventHandler LineReceived
, it also registers the Serial class with Interface.LineReceived when it adds the first Event handler.
Bug- this code using the Serial class results in doubled events:
serialPort.AutoReadLineEnabled = true;
serialPort.LineReceived += serialPort_LineReceived;
the LineReceived Eventr’s add/remove blocks in Serial.cs should let AutoReadLineEnable handle the Interface.LineRecieved registration:
add
{
if (value == null)
return;
if (_lineReceivedHandler == null)
{
AutoReadLineEnabled = true; // this method handles Interface.LineRecieved registration
}
_lineReceivedHandler += value;
}
- Socket.SocketIntefaces.Serial.cs 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 modified the class as follows:
if (newLineFound)
{
string line = outcome.ToString(lastStart, i - lastStart);
string lineFixed = outcome.ToString().Substring(lastStart,i-lastStart);
DebugPrint("Error - Stringbuilder substring "+line+", not "+lineFixed);
RaiseLineReceived(lineFixed);
lastStart = i + this.newLine.Length;
i = lastStart - 1;
}
This works, and I get errors like: (these are reproducible, but random from I can tell)
Error - Stringbuilder substring #VER"NV-I8G FWv2 not #VER"NV-I8G FWv2.66 HWv0"
Error - Stringbuilder substring #SCFG1,ENABLE1,NAME"iMusic",GAIN not #SCFG1,ENABLE1,NAME"iMusic",GAIN0,NUVONET0,SHORTNAME"iM"
Error - Stringbuilder substring #ZCFG1,ENABLE1,NAME"North Master",SLAVETO0,GROUP0,SOURCES27,XRC0 not #ZCFG1,ENABLE1,NAME"North Master",SLAVETO0,GROUP0,SOURCES27,XRC0,IR2,DND0,LO#OK
A bug like this in StringBuilder is pretty serious. I need to double check my StringBuilder usage now.
It is picking up System.Text.StringBuilder from
#region Assembly mscorlib.dll, v4.2.0.0
// C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll
#endregion
Runtime version: v4.0.30319
C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll
Created: Monday, October 01, 2012, 10:36:20 AM
Size: 83.5 KB (85,504 bytes)
I’m still tracking down why that last message was “#ZCFG1,ENABLE1,NAME"North Master”,SLAVETO0,GROUP0,SOURCES27,XRC0,IR2,DND0,LO#OK" when the device apparently sent #ZCFG1,ENABLE1,NAME"North Master",SLAVETO0,GROUP0,SOURCES27,XRC0,IR2,DND0,LOCKED0#OK … so I still have more issues.