我已经将一个Web应用程序移植到.NET Core,我发现它可以在Windows上运行时通过USB与微控制器通信,但在Linux上运行则无法通过USB进行通信。我正在努力解决:
为什么会出现这种情况(这是我正在使用的库中的错误,还是我错误配置了Linux环境?)
我如何解决或解决它(替代图书馆,环境变化等)
如果有人成功地设法创建了一个基于.NET Core的应用程序,它可以通过USB以平台无关的方式进行通信,那么我将非常感谢您的意见。
在本文的其余部分中作为参考,这些是关于我当前环境的一些细节:
我以前有一个基于 .NET Framework 的Web服务,我用来控制微控制器。基于.NET Framework,它仅限于在Windows上运行。为了实现跨平台支持,我开始将应用程序移植到 .NET Core 。这个任务现在或多或少是完整的 - 例如,我现在可以构建和运行我的应用程序作为Docker服务,我认为这将是非常有益的。
我的应用程序分为多个项目,但就本文而言,我只会解决两个问题:
基于ASP.NET核心的Web服务项目,它使应用程序的核心功能可以通过Web通信进行控制。 1 它将所有其他内容联系在一起,是我使用dotnet <<application.dll>>
实际执行的内容。
USB通信服务项目,用于处理与USB设备的通信。 2 它目前使用CoreCompat.LibUsbDotNet库(v2.2.8-r101),它充当WinUSB
上的跨平台C#适配器, Windows和Unix上分别有libusb
个库。该项目作为Nuget包分发,由Web服务使用,并且是问题所在。
当我直接在Windows下运行时,应用程序运行正常。但是,如果我尝试在Linux虚拟机或Docker服务上运行它,则Web服务将无法初始化,并出现错误,抱怨无法找到libusb-1.0 library
。 第3
基于此错误消息,我尝试检查我尝试运行该应用程序的Linux VM和Docker容器的环境。 的 4
将我的USB设备安装在Docker虚拟机上并在特权模式下运行基于我的Web应用程序映像的容器后,我可以确认它看到了我的USB设备:
root@19e8929e1814:/app# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2b87:0001
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
我还确认libusb-1.0
已安装 5 并检查它是否可从ldconfig
缓存中获取:
root@19e8929e1814:/app# ldconfig -p | grep libusb
libusb-1.0.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libusb-1.0.so.0
libusb-1.0.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libusb-1.0.so
libusb-0.1.so.4 (libc6,x86-64) => /lib/x86_64-linux-gnu/libusb-0.1.so.4
libusb-0.1.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libusb-0.1.so.4
基于此,我不确定为什么CoreCompat.LibUsbDotNet
抛出一个关于无法找到libusb-1.0
的异常,并且只能假设它是库中的错误。
有没有人成功设法在Unix环境下使用此库进行通信?或者,是否有人发现了另一种与基于.NET Core的应用程序以平台无关的方式与USB设备通信的方式?
[1] project.json
用于网络服务项目
{
"dependencies": {
"<<Company>>.Communications.<<Product>>Usb": "0.4.9",
"<<Company>>.<<Product>>Web.Core": {
"target": "project"
},
"<<Company>>.<<Product>>WebComponentPackage": "0.4.9-beta0002",
"Autofac": "4.2.1",
"Common.Logging": "3.4.0-Beta2",
"Microsoft.AspNetCore.Diagnostics": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.AspNetCore.WebSockets.Server": "0.1.0",
"Microsoft.Extensions.DependencyModel": "1.1.0-preview1-001100",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.NETCore.App": {
"version": "1.1.0",
"type": "platform"
},
"Serilog.Enrichers.Thread": "3.0.0",
"Serilog.Sinks.Literate": "2.1.0-dev-00034",
"Serilog.Sinks.RollingFile": "3.3.0",
"System.Runtime.Loader": "4.3.0",
"Thrower": "3.0.4"
},
"tools": {
},
"frameworks": {
"netcoreapp1.1": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
[2] project.json
用于USB通信项目
{
"version": "0.4.9-*",
"description": "Provides a type of communication service that facilitates communication between .NET applications and <<Product>> over USB.",
"authors": ["<<Author>>"],
"dependencies": {
"Microsoft.Composition": "1.0.30",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"Thrower": "3.0.4",
"CoreCompat.LibUsbDotNet": "2.2.8-r101",
"Common.Logging": "3.4.0-Beta2",
"<<Company>>.Message": "0.4.3",
"NETStandard.Library": "1.6.1",
"<<Company>>.Communications.Core": "0.4.5",
"Serilog": "2.3.0"
},
"packOptions": {
"owners": ["<<Company>>"],
"repository": {
"type": "git",
"url": "https://bitbucket.org/<<Company>>-dev/<<Product>>usb"
}
},
"frameworks": {
"netstandard1.4": {
"imports": [ "dnxcore50", "portable-net45+win8" ]
}
},
"buildOptions": {
"xmlDoc": true
}
}
[3] USB通信失败异常
Unhandled Exception: System.DllNotFoundException: libusb-1.0 library not found. This is often an indication that libusb-1.0 was installed to '/usr/local/lib' and
mono.net is not looking for it there. To resolve this, add the path '/usr/local/lib' to '/etc/ld.so.conf' and run 'ldconfig' as root. (http://www.mono-project.co
m/DllNotFoundException) ---> System.DllNotFoundException: Unable to load DLL 'libusb-1.0.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)
at MonoLibUsb.MonoUsbApi.Init(IntPtr& pContext)
at MonoLibUsb.MonoUsbSessionHandle..ctor()
--- End of inner exception stack trace ---
at MonoLibUsb.MonoUsbSessionHandle..ctor()
at MonoLibUsb.MonoUsbEventHandler.Init(UnixNativeTimeval unixNativeTimeval)
at MonoLibUsb.MonoUsbEventHandler.Init()
at MonoLibUsb.MonoUsbApi.InitAndStart()
at LibUsbDotNet.LudnMonoLibUsb.MonoUsbDevice.get_MonoUsbDeviceList()
at LibUsbDotNet.Main.LegacyUsbRegistry.get_DeviceList()
at LibUsbDotNet.UsbDevice.get_AllLibUsbDevices()
at LibUsbDotNet.UsbDevice.get_AllDevices()
at LibUsbDotNet.UsbDevice.OpenUsbDevice(Predicate`1 findDevicePredicate)
at LibUsbDotNet.UsbDevice.OpenUsbDevice(UsbDeviceFinder usbDeviceFinder)
at <<Company>>.Communications.<<Product>>Usb.UsbCommunicationService.Start()
at <<Company>>.<<Product>>Web.Core.DeviceController.Initialize()
at <<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.Startup.ConfigureServices(IServiceCollection services)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at <<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.Program.Main(String[] args)
[4] Web服务Dockerfile
FROM microsoft/aspnetcore:1.1.0
ENTRYPOINT ["dotnet", "bin/Debug/netcoreapp1.1/publish/<<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.dll"]
ARG source=.
WORKDIR /app
EXPOSE 80
COPY $source .
RUN apt-get update
RUN apt-get install -y libusb-1.0-0-dev usbutils
[5] Docker容器上find / -name libusb*
的输出
root@19e8929e1814:/app# find / -name libusb*
/lib/x86_64-linux-gnu/libusb-1.0.so.0
/lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
/lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
/lib/x86_64-linux-gnu/libusb-0.1.so.4
/usr/lib/x86_64-linux-gnu/libusb-1.0.a
/usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc
/usr/lib/x86_64-linux-gnu/libusb-1.0.so
/usr/lib/x86_64-linux-gnu/libusb-0.1.so.4
/usr/include/libusb-1.0
/usr/include/libusb-1.0/libusb.h
/usr/share/doc/libusb-1.0-0
/usr/share/doc/libusb-1.0-doc
/usr/share/doc/libusb-1.0-doc/html/libusb_8h_source.html
/usr/share/doc/libusb-1.0-0-dev
/usr/share/doc/libusb-0.1-4
/usr/share/doc-base/libusb-1.0-doc
/var/lib/dpkg/info/libusb-1.0-0-dev:amd64.md5sums
/var/lib/dpkg/info/libusb-1.0-doc.list
/var/lib/dpkg/info/libusb-1.0-0:amd64.md5sums
/var/lib/dpkg/info/libusb-1.0-0:amd64.symbols
/var/lib/dpkg/info/libusb-1.0-0:amd64.shlibs
/var/lib/dpkg/info/libusb-1.0-0:amd64.list
/var/lib/dpkg/info/libusb-1.0-0:amd64.postrm
/var/lib/dpkg/info/libusb-1.0-0:amd64.postinst
/var/lib/dpkg/info/libusb-1.0-doc.md5sums
/var/lib/dpkg/info/libusb-1.0-0-dev:amd64.list
/var/lib/dpkg/info/libusb-0.1-4:amd64.postrm
/var/lib/dpkg/info/libusb-0.1-4:amd64.shlibs
/var/lib/dpkg/info/libusb-0.1-4:amd64.symbols
/var/lib/dpkg/info/libusb-0.1-4:amd64.postinst
/var/lib/dpkg/info/libusb-0.1-4:amd64.list
/var/lib/dpkg/info/libusb-0.1-4:amd64.md5sums
答案 0 :(得分:9)
将实际的共享库链接到您拥有的.Net Core版本的共享库位置。我设法使用:
加载库ln -s /lib/x86_64-linux-gnu/libusb-1.0.so.0 /opt/dotnet/shared/Microsoft.NETCore.App/1.1.0/libusb-1.0.dll
但我还没有得到设备列表。