Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.IO.Ports/src/Configurations.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<PackageConfigurations>
netstandard-Windows_NT;
netstandard-Linux;
netstandard;
net461;
</PackageConfigurations>
Expand Down
2 changes: 1 addition & 1 deletion src/System.IO.Ports/src/Interop/Unix/Interop.Termios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal enum Queue
internal static extern int TermiosGetSpeed(SafeFileHandle handle);

[DllImport(Libraries.IOPortsNative, EntryPoint = "SystemIoPortsNative_TermiosAvailableBytes", SetLastError = true)]
internal static extern int TermiosGetAvailableBytes(SafeFileHandle handle, int input);
internal static extern int TermiosGetAvailableBytes(SafeFileHandle handle, Queue input);

[DllImport(Libraries.IOPortsNative, EntryPoint = "SystemIoPortsNative_TermiosDiscard", SetLastError = true)]
internal static extern int TermiosDiscard(SafeFileHandle handle, Queue input);
Expand Down
55 changes: 48 additions & 7 deletions src/System.IO.Ports/src/System.IO.Ports.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ProjectGuid>{187503F4-BEF9-4369-A1B2-E3DC5D564E4E}</ProjectGuid>
<IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetGroup)' == 'netstandard' AND '$(TargetsWindows)' != 'true'">SR.PlatformNotSupported_IOPorts</GeneratePlatformNotSupportedAssemblyMessage>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetGroup)' == 'netstandard' AND '$(TargetsWindows)' != 'true' AND '$(TargetsLinux)' != 'true'">SR.PlatformNotSupported_IOPorts</GeneratePlatformNotSupportedAssemblyMessage>
<DefineConstants>$(DefineConstants);NOSPAN</DefineConstants>
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<Configurations>net461-Debug;net461-Release;netfx-Debug;netfx-Release;netstandard-Debug;netstandard-Release;netstandard-Windows_NT-Debug;netstandard-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release</Configurations>
<Configurations>net461-Debug;net461-Release;netfx-Debug;netfx-Release;netstandard-Debug;netstandard-Release;netstandard-Windows_NT-Debug;netstandard-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release;netstandard-Linux-Debug;netstandard-Linux-Release</Configurations>
</PropertyGroup>
<!-- Default configurations to help VS understand the options -->
<ItemGroup Condition="'$(TargetsNetFx)' != 'true' AND '$(TargetsWindows)' == 'true'">
<ItemGroup Condition="'$(TargetsNetFx)' != 'true' AND ('$(TargetsWindows)' == 'true' OR '$(TargetsLinux)' == 'true')">
<Compile Include="System\IO\Ports\Handshake.cs" />
<Compile Include="System\IO\Ports\InternalResources.cs" />
<Compile Include="System\IO\Ports\NativeMethods.cs" />
<Compile Include="System\IO\Ports\Parity.cs" />
<Compile Include="System\IO\Ports\SerialData.cs" />
Expand All @@ -23,9 +22,12 @@
<Compile Include="System\IO\Ports\SerialPinChange.cs" />
<Compile Include="System\IO\Ports\SerialPinChangedEventArgs.cs" />
<Compile Include="System\IO\Ports\SerialPinChangedEventHandler.cs" />
<Compile Include="System\IO\Ports\SerialPort.cs" />
<Compile Include="System\IO\Ports\SerialStream.cs" />
<Compile Include="System\IO\Ports\SerialPort.Windows.cs" />
<Compile Include="System\IO\Ports\StopBits.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' != 'netfx' AND '$(TargetsWindows)' == 'true'">
<Compile Include="System\IO\Ports\InternalResources.cs" />
<Compile Include="System\IO\Ports\SerialStream.Windows.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.DCB.cs">
<Link>Common\Interop\Windows\kernel32\Interop.DCB.cs</Link>
</Compile>
Expand Down Expand Up @@ -139,6 +141,30 @@
<Compile Include="System\IO\Ports\SerialPort.Win32.cs" />
<Compile Include="System\IO\Ports\SerialStream.Win32.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsLinux)' == 'true' ">
<Compile Include="System\IO\Ports\SerialPort.Linux.cs" />
<Compile Include="System\IO\Ports\SerialStream.Unix.cs" />
<Compile Include="Interop\Unix\Interop.Termios.cs" />
<Compile Include="Interop\Unix\Interop.Serial.cs" />
<Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Interop\Unix\Interop.Errors.cs">
<Link>Common\Interop\Unix\Interop.Errors.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Interop\Unix\System.Native\Interop.Read.cs">
<Link>Interop\Unix\System.Native\Interop.Read.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Interop\Unix\System.Native\Interop.Write.cs">
<Link>Interop\Unix\System.Native\Interop.Write.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Poll.cs">
<Link>Interop\Unix\System.Native\Interop.Poll.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Shutdown.cs">
<Link>Interop\Unix\System.Native\Interop.Shutdown.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)' == 'true'">
<Reference Include="mscorlib" />
<Reference Include="System" />
Expand All @@ -161,4 +187,19 @@
<Reference Include="System.Threading.Thread" />
<Reference Include="System.Threading.ThreadPool" />
</ItemGroup>
</Project>
<ItemGroup Condition="'$(TargetsLinux)' == 'true'">
<Reference Include="Microsoft.Win32.Primitives" />
<Reference Include="System.Collections" />
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Diagnostics.Tools" />
<Reference Include="System.IO.FileSystem" />
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Text.Encoding.Extensions" />
<Reference Include="System.Threading" />
<Reference Include="System.Threading.Thread" />
</ItemGroup>
</Project>
43 changes: 43 additions & 0 deletions src/System.IO.Ports/src/System/IO/Ports/SerialPort.Linux.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;

namespace System.IO.Ports
{
public partial class SerialPort : Component
{
public static string[] GetPortNames()
{
const string sysTtyDir = "/sys/class/tty";
const string sysUsbDir = "/sys/bus/usb-serial/devices/";

if (Directory.Exists(sysTtyDir))
{
// /sys is mounted. Let's explore tty class and pick active nodes.
List<string> ports = new List<string>();
DirectoryInfo di = new DirectoryInfo(sysTtyDir);
var entries = di.EnumerateFileSystemInfos(@"*", SearchOption.TopDirectoryOnly);
foreach (var entry in entries)
{
if (Directory.Exists(sysUsbDir + entry.Name) || File.Exists(entry.FullName + "/device/id"))
{
ports.Add("/dev/" + entry.Name);
}
}

return ports.ToArray();
}
else
{
// Fallback to scanning /dev. That may have more devices then needed.
// This can also miss usb or serial devices with non-standard name.
return Directory.GetFiles("/dev", "ttyS*");
}
}
}
}
39 changes: 39 additions & 0 deletions src/System.IO.Ports/src/System/IO/Ports/SerialPort.OSX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not being referenced in the csproj. Are planning to include support to macOS? If yes the csproj need a few updates besides including this file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. This is part I tested so far so I was not comfortable enable full build.
That will probably come when Linux is stable.

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;

namespace System.IO.Ports
{
public partial class SerialPort : Component
{
public static string[] GetPortNames()
{
List<string> ports = new List<string>();

foreach (string name in Directory.GetFiles("/dev", "tty.*"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not sure if it is worth but since Directory.GetFiles returns an array you can make the two calls to it to and set the capacity of ports. You just need to store the arrays in locals before doing that.

{
// GetFiles can return unexpected results because of 8.3 matching.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JeremyKuhne does this applies to macOS?

// Like /dev/tty
if (name.StartsWith("/dev/tty."))
{
ports.Add(name);
}
}

foreach (string name in Directory.GetFiles("/dev", "cu.*"))
{
if (name.StartsWith("/dev/cu."))
{
ports.Add(name);
}
}

return ports.ToArray();
}
}
}
Loading