Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNotFoundException when calling GetPortNames() #48

Closed
NilesDavis opened this issue Feb 14, 2018 · 4 comments
Closed

FileNotFoundException when calling GetPortNames() #48

NilesDavis opened this issue Feb 14, 2018 · 4 comments
Labels

Comments

@NilesDavis
Copy link

Hi! I am running a UWP-App to test the Library. The first step in opening a new port was to check if it is available. But when I called SerialPortStream.GetPortNames() the FileNotFoundException was thrown with the description: "Could not load file or assembly 'Microsoft.Win32.Registry, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'"
OK, without validating the existence of the port I wanted to connect via

try
{
    string[] ports = SerialPortStream.GetPortNames();
    SerialPortStream sps = new SerialPortStream("COM3", 57600, 8, Parity.None, StopBits.One);
    sps.Open();
}
catch(Exception ex)
{
    Debugger.Break();
}

This led to "Access Denied: COM3"
There is no problem accessing the port with other tools like HTerm, and unlike #11 there is definitely no other program talking with the port, and there is no possiblilty to have an error like #38 because the above lines of code are the only ones existing. So I downloaded and referenced the source and the error happens here:

m_ComPortHandle = UnsafeNativeMethods.CreateFile(@"\\.\" + PortName,
                NativeMethods.FileAccess.GENERIC_READ | NativeMethods.FileAccess.GENERIC_WRITE,
                NativeMethods.FileShare.FILE_SHARE_NONE,
                IntPtr.Zero,
                NativeMethods.CreationDisposition.OPEN_EXISTING,
                NativeMethods.FileAttributes.FILE_FLAG_OVERLAPPED,
                IntPtr.Zero);

as the returned handle is invalid (-1) and the WinIOError() @ int e = Marshal.GetLastWin32Error(); shows a 5 leading to UnauthorizedAccessException.
Do you have a hint where to search for a possible mistakes?

@jcurl
Copy link
Owner

jcurl commented Feb 14, 2018

Works fine under Win32 Desktop. I suspect UWP is started in a sandboxed environment preventing you from accessing the COM port. Error 5 comes back due to permissions issues, or someone else already has the COM port opened.

@jcurl jcurl closed this as completed Feb 14, 2018
@NilesDavis
Copy link
Author

Obiously I forgot to define the device capabilities in the Package.appxmanifest like this:

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort"/>
  </Device>
</DeviceCapability>

@jcurl
Copy link
Owner

jcurl commented Feb 14, 2018

Good news! If you have success, let us know your experience, and perhaps you can make some suggestions for the Wiki.

@jcurl jcurl added the invalid label Apr 25, 2018
@jasells
Copy link

jasells commented Nov 8, 2018

@NilesDavis

Did you ever resolve this? I am looking into this as well, but I don't think this lib is compatible with UWP... I keep getting "FileNotfound" (from GetPortNames) or UnauthorizedAccess (from Open()).

The short version is that UWP does have sandboxing in place for lots of things, including File, Network, and Serial (device) IO and while UWP does support the .NET 2.0 API's, there's no guarantee that they will work. System.IO.File API's are similar on UWP (available, but throw runtime exceptions when called because sandboxing prohibits the low-level calls).

I know my serial device works, as I've tested it via the permitted API: Windows.Devices.SerialCommunication.SerialDevice (etc).

In case anyone needs an immediate UWP solution, here is an example.

@jcurl If you have any desire for supporting UWP, let me know. It would be possible...

@jcurl jcurl mentioned this issue Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants