gpt4 book ai didi

c# - 系统.DllNotFoundException : Unable to load DLL 'i2c.so'

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:25 26 4
gpt4 key购买 nike

正在运行接近传感器程序,但不幸的是,由于导入 DLL 时出错,我无法编译该程序。

这是错误: enter image description here

DLL 来自 i2c.cs 文件:

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);
}
}
}

最佳答案

我认为代码示例是我在 GitHub 上编写和共享的代码,所以也许我可以在这里提供帮助。我最近添加了包含以下信息的 ReadMe.txt 文件:

The i2c.so file can be placed in the same folder as the mono assembly (bin folder) or copied to the /usr/lib/ folder to be available to all mono assemblies. In some cases you may see DLL File Not Found exceptions, but placing in /usr/lib should resolve that.

So copy the i2c.so file to /usr/lib/ directory

linux command:

cp i2c.so /usr/lib/

To rebuild the i2c.so file, from shell, "CD" to the location of the files, and run the "make" command.

linux command:

make

"make" will run the build defined in the makefile definition.

Code borrowed from i2ctools, v3.1.0, see Credits.txt and Licence.txt

重要的部分是将 i2c.so 文件复制到/usr/lib/目录。

如果您没有 i2c.so,或者您需要一个更新的版本,文件底部是关于如何构建 i2c.so 的线索。

可能值得指出的是,代码最初打算使用 Mono 在 Raspberry Pi 上运行。

https://github.com/silentbobbert/pi_sensors

关于c# - 系统.DllNotFoundException : Unable to load DLL 'i2c.so' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24462295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com