C# System.DllNotFoundException:无法加载DLL';i2c.so';
运行接近传感器程序,但不幸的是,由于导入DLL时出错,我无法编译该程序 这就是错误: DLL来自i2c.cs文件:C# System.DllNotFoundException:无法加载DLL';i2c.so';,c#,c,dll,C#,C,Dll,运行接近传感器程序,但不幸的是,由于导入DLL时出错,我无法编译该程序 这就是错误: DLL来自i2c.cs文件: private static class I2CNativeLib { [DllImport("i2c.so", EntryPoint = "Get", SetLastError = true)] public static extern int Get(string i2Cbusid, string deviceaddress, string dataaddre
private static class I2CNativeLib
{
[DllImport("i2c.so", EntryPoint = "Get", SetLastError = true)]
public static extern int Get(string i2Cbusid, string deviceaddress, string dataaddress);
[DllImport("i2c.so", EntryPoint = "Set", SetLastError = true)]
public static extern int Set(string i2Cbusid, string deviceaddress, string dataaddress, string datavalue, int force);
}
以下是完整的i2c.cs文件供参考,以下是主文件:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace RPi.I2C.Net
{
internal abstract class i2c
{
internal static Dictionary<string, int> Constants { get; set; }
protected static int Set16(string i2Cbusid, string deviceaddress, string dataaddress, string datavalue, int force)
{
var value = (UInt16)Convert.ToInt16(datavalue, 16);
var add1 = (UInt16)Convert.ToInt16(dataaddress, 16);
var add2 = ++add1;
var msb = GetAsHexString(value >> 8);
var lsb = GetAsHexString(value & 0xFF);
Console.WriteLine("16-bit: Writing 16-bit Value: {0} as 2 8-bit values {1} and {2}", GetAsHexString(value), msb, lsb);
Console.WriteLine("16-bit: Writing {0} to address {1}", msb, GetAsHexString(add1));
var data = I2CNativeLib.Set(i2Cbusid, deviceaddress, GetAsHexString(add1), msb, force); //set msb byte/8 bits
Console.WriteLine("16-bit: Response to msb: {0}", data);
Console.WriteLine("16-bit: Writing {0} to address {1}", lsb, GetAsHexString(add2));
data |= I2CNativeLib.Set(i2Cbusid, deviceaddress, GetAsHexString(add2), lsb, force); //set lsb byte/8 bits
Console.WriteLine("16-bit: Response to msb |= lsb: {0}", data);
return data;
}
protected static byte Set8(string i2Cbusid, string deviceaddress, string dataaddress, string datavalue, int force)
{
Console.WriteLine("8-bit: Writing {0} to address {1}", datavalue, dataaddress);
//8-bit
return (byte)I2CNativeLib.Set(i2Cbusid, deviceaddress, dataaddress, datavalue, force);
}
protected static byte Get(string i2Cbusid, string deviceaddress, string dataaddress)
{
return (byte)I2CNativeLib.Get(i2Cbusid, deviceaddress, dataaddress);
}
internal string Busid = string.Empty;
internal bool DoWork = false;
protected i2c()
{
Constants = new Dictionary<string, int>();
}
internal static int GetConstantAsByte(string key)
{
int value;
Constants.TryGetValue(key, out value);
return value;
}
internal static string GetConstantAsString(string key)
{
int value;
Constants.TryGetValue(key, out value);
return "0x" + value.ToString("X").PadLeft(2, '0');
}
internal static string GetAsHexString(uint value)
{
return "0x" + value.ToString("X").PadLeft(2, '0');
}
internal static string GetAsHexString(int value)
{
return "0x" + value.ToString("X").PadLeft(2, '0');
}
internal byte GetValue8(string deviceAddress, string dataAddress)
{
var result = Get(
Busid,
GetConstantAsString(deviceAddress),
GetConstantAsString(dataAddress)
);
return result;
}
internal UInt16 GetValue16(string deviceAddress, string dataAddress)
{
var result = (UInt16)(Get(Busid, GetConstantAsString(deviceAddress), GetConstantAsString(dataAddress)) << 8);
result |= Get(Busid, GetConstantAsString(deviceAddress), GetConstantAsString(dataAddress));
return result;
}
internal abstract void Start();
internal virtual void Stop()
{
DoWork = false;
}
private static class I2CNativeLib
{
[DllImport("i2c.so", EntryPoint = "Get", SetLastError = true)]
public static extern int Get(string i2Cbusid, string deviceaddress, string dataaddress);
[DllImport("i2c.so", EntryPoint = "Set", SetLastError = true)]
public static extern int Set(string i2Cbusid, string deviceaddress, string dataaddress, string datavalue, int force);
}
}
}
using System;
namespace i2c
{
class Program
{
static void Main(string[] argv)
{
try
{
Console.WriteLine("Starting Communication with VCNL4000");
var vcnl4000 = new VCNL4000("1", 100);
vcnl4000.ProximityReading += sensor_ProximityReading;
var productId = vcnl4000.ProductID;
OutputValue(productId, "Product ID");
vcnl4000.Start();
//Console.WriteLine("Starting Communication with ADS1115");
//var ads1115 = new ADS1115("1");
//ads1115.Message += ads1115_Message;
//ads1115.SingleEndedConversionReading += ads1115_ConversionReading;
//ads1115.Start();
Console.WriteLine("Press Esc key to stop");
do
{
while (!Console.KeyAvailable)
{
// Do something
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
//ads1115.Stop();
vcnl4000.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void ads1115_Message(object sender, ConverterMessageEventArgs e)
{
OutputValue(e.Message, "Convertor");
}
private static void OutputValue(string response, string message)
{
Console.WriteLine(message + " Response: {0}", response);
}
private static void ads1115_ConversionReading(object sender, SingleEndedConversionEventArgs e)
{
var _sender = (ADS1115)sender;
OutputValue(e.Data, "Conversion Reading");
}
static void sensor_ProximityReading(object sender, ProximtyEventArgs e)
{
var sensor = (VCNL4000)sender;
OutputValue(e.Proximity, "Proximity Reading");
OutputValue(e.RawValue, "Proximity Raw Value");
}
private static void OutputValue(int response, string message)
{
Console.WriteLine(message + " Response: {0} (0x{1})", response, response.ToString("X"));
}
private static void OutputValue(decimal response, string message)
{
Console.WriteLine(message + " Response: {0}", response);
}
private static void OutputValue(float response, string message)
{
Console.WriteLine(message + " Response: {0}", response);
}
}
}
你的“i2c.so”在正确的位置吗
请阅读这个关于如何使共享对象库与C#一起工作的精彩链接
查看DLLNotFoundException
我们可以从查找.so的实际位置以及命名是否正确开始
根据您使用的平台,目录按以下顺序查看:
注意:这是从上面提供的链接复制的:
窗口
从中加载应用程序的目录。当前目录
系统目录。使用GetSystemDirectory()函数获取 此目录的路径。
16位系统目录。
Windows目录。使用GetWindowsDirectory()函数获取 此目录的路径。
PATH环境变量中列出的目录 Linux 用户的LD\U库\U路径环境变量。
在/etc/ld.so.cache中缓存的库的列表。
/lib,后跟/usr/lib
请阅读文章。我认为代码示例是我在GitHub上编写和共享的代码,所以我可以在这里提供帮助。我最近添加了一个ReadMe.txt文件,其中包含以下信息: i2c.so文件可以与mono程序集放在同一文件夹中 (bin文件夹)或复制到/usr/lib/文件夹以供所有人使用 单声道组件。在某些情况下,您可能会看到未找到DLL文件 异常,但放在/usr/lib中应该可以解决这个问题 因此,将i2c.So文件复制到/usr/lib/目录 linux命令: cp i2c.so/usr/lib/ 要重建i2c.so文件,请从shell“CD”到 文件,并运行“make”命令 linux命令: 制造 “make”将运行makefile定义中定义的生成 代码借用自i2ctools,v3.1.0,请参阅Credits.txt和license.txt 重要的部分是将i2c.so文件复制到/usr/lib/目录 在文件的底部有一条关于如何构建i2c的线索。因此,如果你没有i2c,或者你需要一个更新版本,那么首先 也许值得指出的是,该代码最初打算使用Mono在Raspberry Pi上运行
在哪个站台?如果它在Windows下,你不应该用i2c.DLL而不是i2c.so吗?(仅从“按任意键继续…”消息中猜测)