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


running proximity sensor program, unfortunately cant compile program due errors in importing dlls.

this error: enter image description here

the dll i2c.cs file:

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

for reference, here full i2c.cs file , below main file:

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} 2 8-bit values {1} , {2}", getashexstring(value), msb, lsb);              console.writeline("16-bit: writing {0} 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 msb: {0}", data);               console.writeline("16-bit: writing {0} address {1}", lsb, getashexstring(add2));             data |= i2cnativelib.set(i2cbusid, deviceaddress, getashexstring(add2), lsb, force); //set lsb byte/8 bits             console.writeline("16-bit: response 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} 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);         }     }     } 

here main file:

using system;  namespace i2c {   class program   {     static void main(string[] argv)     {         try         {             console.writeline("starting communication 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 ads1115");             //var ads1115 = new ads1115("1");             //ads1115.message += ads1115_message;             //ads1115.singleendedconversionreading += ads1115_conversionreading;             //ads1115.start();                              console.writeline("press esc key stop");                         {                 while (!console.keyavailable)                 {                     //                 }             } 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);     }   } } 

i recognise code sample code wrote , shared on github, maybe can here. added readme.txt file information:

the i2c.so file can placed in same folder mono assembly (bin folder) or copied /usr/lib/ folder available mono assemblies. in cases may see dll file not found exceptions, placing in /usr/lib should resolve that.

so copy i2c.so file /usr/lib/ directory

linux command:

cp i2c.so /usr/lib/

to rebuild i2c.so file, shell, "cd" location of files, , run "make" command.

linux command:

make

"make" run build defined in makefile definition.

code borrowed i2ctools, v3.1.0, see credits.txt , licence.txt

the important part copying i2c.so file /usr/lib/ directory.

at bottom of file clue on how build i2c.so in first place if dont have it, or need updated version.

might worth pointing out code intended run on raspberry pi, using mono.

https://github.com/silentbobbert/pi_sensors


Comments