Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

minimal support for serial port on Linux. part 2.#30903

Merged
wfurt merged 6 commits intodotnet:masterfrom
wfurt:serial3
Sep 7, 2018
Merged

minimal support for serial port on Linux. part 2.#30903
wfurt merged 6 commits intodotnet:masterfrom
wfurt:serial3

Conversation

@wfurt
Copy link
Copy Markdown
Member

@wfurt wfurt commented Jul 9, 2018

related to #18012 and #29033

This is initial and minimal implementation of support for serial port on Linux. (managed part)
many unit tests still fail but many do pass.
Some tests make specific assumptions about buffering and that may not work properly on Unix.
It needs more investigation.
Usb2Serial devices seems to behave different than old good RS-232 chips.

With this change, simple serial apps should be possible on Linux.
Other Unix like systems should be feasible. I just did not have time to complete testing.

@wfurt wfurt added the os-linux Linux OS (any supported distro) label Jul 9, 2018
@wfurt wfurt self-assigned this Jul 9, 2018
Copy link
Copy Markdown
Contributor

@pjanotti pjanotti left a comment

Choose a reason for hiding this comment

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

@wfurt good to see that this is coming.

</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' != 'netfx' AND '$(TargetsWindows)' == 'true'">
<Compile Include="System\IO\Ports\InternalResources.cs" />
<Compile Include="System\IO\Ports\SerialStream.cs" />
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: renaming these files seems good, especially SerialStream.cs to something like SerialStream.Windows.cs

{
// /sys is mounted. Let's explore tty class and pick active nodes.
List<string> ports = new List<string>();
DirectoryInfo di = new DirectoryInfo(@"/sys/class/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.

Use the constant defined above

// /sys is mounted. Let's explore tty class and pick active nodes.
List<string> ports = new List<string>();
DirectoryInfo di = new DirectoryInfo(@"/sys/class/tty");
var entries = di.EnumerateFileSystemInfos(@"*", SearchOption.TopDirectoryOnly);
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: per coding style we put the explicity type in this case: IEnumerable<FileSystemInfo>. Alternatively you could use Directory.GetFiles and avoid the DirectoryInfo allocation.

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.

That does not work. /sys is bunch of symbolic links and GetFiles() seems to ignore them.
(works on device files)

}
else
{
// Fallback to scanning /dev. That may have more devices then needed as well as
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: comment sentence is incomplete

@@ -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.


if (value != _handshake)
{
_handshake = value;
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 should be done only in case of success.

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.

Or perhaps value not cached at all since this is hardware and someone might want to ensure that the operation is actually done.


internal void SetBufferSizes(int readBufferSize, int writeBufferSize)
{
if (_handle == null) throw new IOException();
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: ObjectDisposedException like above?

{
if (_handle == null) throw new ObjectDisposedException(SR.Port_not_open);
// This may or may not work depending on hardware.
Interop.Termios.TermiosDiscard(_handle, Interop.Termios.Queue.ReceiveQueue);
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.

I see the comment but it is not clear to me if an error is possible here. If error is possible: is ok to ignore it? Similar below

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.

I'm not sure but since it all best effort, I think it would be better not to error here.


internal unsafe int ReadByte(int timeout)
{
if (_handle == null) throw new IOException();
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: same as above: ObjectDisposedException for consistency?


while (count > 0)
{
Interop.Sys.PollEvents events = Interop.Sys.PollEvents.POLLNONE;
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: I rather see this in the scope that it is used.

nit: the whole poll handling, when timeout is greater than 0, is also repeated in a few places.

@karelz karelz added this to the 3.0 milestone Jul 19, 2018
@karelz
Copy link
Copy Markdown
Member

karelz commented Aug 15, 2018

@wfurt @pjanotti what is left to finish this PR? It's 5 weeks old now ...

@karelz
Copy link
Copy Markdown
Member

karelz commented Sep 5, 2018

@wfurt @pjanotti ping?

@pjanotti
Copy link
Copy Markdown
Contributor

pjanotti commented Sep 6, 2018

@dotnet-bot test this please

@pjanotti
Copy link
Copy Markdown
Contributor

pjanotti commented Sep 6, 2018

@wfurt do you have cycles to clean-up the build error on Windows?

CSC : error CS2001: Source file 'D:\j\workspace\windows-TGrou---f8ac6754\src\System.IO.Ports\src\System\IO\Ports\SerialStream.cs' could not be found. [D:\j\workspace\windows-TGrou---f8ac6754\src\System.IO.Ports\src\System.IO.Ports.csproj]

@wfurt
Copy link
Copy Markdown
Member Author

wfurt commented Sep 6, 2018

yes, I'll take a look.

@wfurt
Copy link
Copy Markdown
Member Author

wfurt commented Sep 7, 2018

@dotnet-bot test this please

Copy link
Copy Markdown
Contributor

@pjanotti pjanotti left a comment

Choose a reason for hiding this comment

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

Thanks @wfurt! I think this PR is at a good point to be committed and we can start testing/improving it until release.

@wfurt wfurt merged commit ed4e3ea into dotnet:master Sep 7, 2018
@wfurt wfurt deleted the serial3 branch June 15, 2020 18:21
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* minimal support for serial port on Linux

* feedback from review

* rename SerialStream -> SerialStream.Windows

* some fixes and improvements


Commit migrated from dotnet/corefx@ed4e3ea
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.IO os-linux Linux OS (any supported distro)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants