Device.Net icon indicating copy to clipboard operation
Device.Net copied to clipboard

SerialPort.Net returned device can't be initialized

Open Xriuk opened this issue 2 years ago • 5 comments

Describe the issue I'm trying to retrieve a SerialPort device, it gets inside devices (IEnumerable<ConnectedDeviceDefinition>), as I can see WindowsSerialPortDeviceFactory is retrieving it from the Registry (because it has a label assigned), but when I try to read/write to it it says 'The device has not been initialized.'. Is the device at fault? Because I see that it provides too little info down below (buffer sizes, etc.), but when I use it with other programs like Realterm to test it, it connects to the COM port and works. Is there something else I can test? Thanks

Your Code

using Device.Net;
using Hid.Net.Windows;
using SerialPort.Net.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Usb.Net.Windows;

namespace MyProgram
{
	class Program
	{
		static (int, int)[] vidPids = new[] {
			( 1637 , 20833 ),
			( 11878, 256   ),
			( 11878, 257   ),
			( 11878, 258   ),
			( 11816, 1     ),
			( 4271 , 2     ),
			( 2478 , 12328 )
		};

		static async Task Main(string[] args)
		{
			FilterDeviceDefinition[] devicesDefs = vidPids .Select(vp => new FilterDeviceDefinition(vendorId: (uint)vp.Item1, productId: (uint)vp.Item2)).ToArray();
			var deviceFactory = devicesDefs.CreateWindowsUsbDeviceFactory()
				.Aggregate(devicesDefs.CreateWindowsHidDeviceFactory())
				.Aggregate(new WindowsSerialPortDeviceFactory());
			var devices = await deviceFactory.GetConnectedDeviceDefinitionsAsync();
			var firstConnectedDeviceDefinition = devices.FirstOrDefault();

			IDevice device = await deviceFactory.GetDeviceAsync(firstConnectedDeviceDefinition);
			if (device == null)
				throw new Exception("There were no devices found");

			await device.InitializeAsync();

			TransferResult res = await device.WriteAndReadAsync(Utils.ToCmd("T")); // Here the exception is thrown

			Console.WriteLine(Utils.ToString(res.Data));
		}
	}
}

Log / Stack Trace

info: Device.Net.DeviceFactory[0]
      Getting connected device definitions...
dbug: Device.Net.Windows.WindowsDeviceEnumerator[0]
      About to call SetupDiGetClassDevs for class Guid dee824ef-729b-4a0e-9c14-b7117d33a817. Flags: 18
dbug: Device.Net.Windows.WindowsDeviceEnumerator[0]
      The call to SetupDiEnumDeviceInterfaces  returned ERROR_NO_MORE_ITEMS
info: Device.Net.DeviceFactory[0]
      Getting connected device definitions...
dbug: Device.Net.Windows.WindowsDeviceEnumerator[0]
      About to call SetupDiGetClassDevs for class Guid 4d1e55b2-f16f-11cf-88cb-001111000030. Flags: 18
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_04d9&pid_a09f&mi_01&col01#9&3581eb34&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}\kbd. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col06#7&289d55ad&0&0005#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col07#7&289d55ad&0&0006#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#asue120a&col01#4&2b4ef635&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#asue120a&col02#4&2b4ef635&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0000&pid_0000&mi_03#b&c67dab0&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_05a3&pid_9331&mi_04#9&375d7c32&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#asue120a&col03#4&2b4ef635&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_04d9&pid_a09f&mi_02#9&11aaad72&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col03#7&289d55ad&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}\kbd. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_04d9&pid_a09f&mi_01&col02#9&3581eb34&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_04d9&pid_a09f&mi_01&col03#9&3581eb34&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_04d9&pid_a09f&mi_01&col04#9&3581eb34&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0d8c&pid_0012&mi_03#a&34fbbbf7&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_04d9&pid_a09f&mi_00#9&1dbe5eef&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#atk4002#3&1c78ff7d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetManufacturer
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetProduct
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col01#7&289d55ad&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col02#7&289d55ad&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col04#7&289d55ad&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
info: Hid.Net.Windows.WindowsHidApiService[0]
      Calling CreateFile Area: ApiService for DeviceId: \\?\hid#vid_0b05&pid_1866&col05#7&289d55ad&0&0004#{4d1e55b2-f16f-11cf-88cb-001111000030}. Desired Access: None. Share mode: 3. Creation Disposition: 3
dbug: WindowsHidDeviceFactoryExtensions[0]
      Found device
warn: Hid.Net.Windows.WindowsHidApiService[0]
      Could not get Hid string. Message:  Region: GetHidString Caller: GetSerialNumber
dbug: Device.Net.Windows.WindowsDeviceEnumerator[0]
      The call to SetupDiEnumDeviceInterfaces  returned ERROR_NO_MORE_ITEMS
info: Device.Net.DeviceFactory[0]
      Checking if the factory supports this connected device. Device Definition: Device Id: \\.\COM6 Label: \Device\ProlificSerial0 Vid:  Pid:
      Read Buffer Size:  Write Buffer Size:
      Manufacturer:  Product Name:
info: Device.Net.DeviceFactory[0]
      Checking if the factory supports this connected device. Device Definition: Device Id: \\.\COM6 Label: \Device\ProlificSerial0 Vid:  Pid:
      Read Buffer Size:  Write Buffer Size:
      Manufacturer:  Product Name:

Info

  • Platform: Windows
  • Device Type: SerialPort
  • Version: 4.2.1

Xriuk avatar Apr 27 '22 16:04 Xriuk

Also I don't know if this might be a problem, but the device is supposed to be using a baud rate of 2400, I tried instantiating it manually with WindowsSerialPortDevice (by providing the port, baud rate, bytes, ...) but it's not helping, it still does not initialize.

Xriuk avatar Apr 27 '22 16:04 Xriuk

I was trying with a RS232 adapter to USB, now I tried with a serial USB cable (on another port of the device) and it's initializing but I get an error during write/read, I guess that it's something inside the Windows ApiService

Xriuk avatar Apr 27 '22 16:04 Xriuk

Just tested out with the default class SerialPort (System.IO.Ports) and it works, is there any reason why you are not using this package in your library?

Xriuk avatar Apr 28 '22 07:04 Xriuk

Hi @Xriuk.Thanks for the detailed information. I had hoped to spend more time on serial ports but unfortunately, it was just never a priority because there is so much need for Hid stuff.

I notice here that you are talking to two possible devices: hid and serial. This is exactly what this framework was designed for so so it's a shame that it's not working.

is there any reason why you are not using this package in your library

I think this would require an extra nuget package. However, you can create your own factory to do this

MelbourneDeveloper avatar May 20 '22 21:05 MelbourneDeveloper

is there any reason why you are not using this package in your library

I think this would require an extra nuget package. However, you can create your own factory to do this

Hi, just wanted to add that System.IO.Ports is in the .NET Core BCL since .NET Core 2.0 and is also in the regular .NET Framework. It's not a part of .NET Standard 2.0 but does support that standard.

One option could be to add a .NET Core target alongside netstandard2.0;net45, then reference the package on the .NET Standard target while avoiding the extra package reference on the other targets.

danielchalmers avatar Jun 16 '22 00:06 danielchalmers