Project

General

Profile

System.IO.Ports.SerialPort.GetPortNames error with Bluetooth

Using .NET 3.5, sometimes System.IO.Ports.SerialPort.GetPortNames returns COM ports with a "c" at the back, e.g. COM24c. This is a bug in .NET 3.5. This bug causes the sample programs failed to sort the COM ports correctly.
Added by admin almost 8 years ago

This problem is due to the COM port values are not null terminated in the registry.

See more information available here http://connect.microsoft.com/VisualStudio/feedback/details/236183/system-io-ports-serialport-getportnames-error-with-bluetooth

I have seen the issue and trying some workarrounds. If you open the registry key HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM and look at a bluetooth device (right click and open "edit binary data") you will see that the portname is not null terminated, thus not respecting the registry type REG_SZ. The function to read the registry in .NET wrappers at least, are assuming the NULL will be there and not checking anything (making a memcpy and the last two bytes - unicode null - are left with whatever garbage is there) so the last to bytes can lead to some unicode value, sometimes a letter sometimes a symbol, whatever. You can verify this reading directly the registry entry for the port. My workarround will consist in getting the bytes from the string, replacing the last two bytes with 00 00 and reencodign the string... not done yet. Please comment if someone can workarround on this, also Microsoft, maybe a fix can be included in ORCAS... the bug is in the bluetooth stack from microsoft and in the registry functions...


Comments