c# mono raspberry pi GPIO with Raspberry# getting Operation is not valid - c#

I'm trying to use Raspberry# library to perform basic task whith GPIO pin on Raspberry PI (on and off).
As per example on github: https://github.com/raspberry-sharp/raspberry-sharp-io/wiki/Raspberry.IO.GeneralPurpose
Code:
var led1 = ConnectorPin.P1Pin11.Output();
var connection = new GpioConnection(led1);
for (var i = 0; i < 100; i++)
{
connection.Toggle(led1);
System.Threading.Thread.Sleep(250);
}
connection.Close();
on line var connection = new GpioConnection(led1); I get exception:
"Operation is not valid due to the current state of the object"
Stack trace
Unhandled Exception:
System.InvalidOperationException: Operation is not valid due to the current state of the object
at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.get_DefaultDriver () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, IEnumerable`1 pins) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <filename unknown>:0
at Hello.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Operation is not valid due to the current state of the object
at Raspberry.IO.GeneralPurpose.GpioConnectionDriver..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings.get_DefaultDriver () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnectionSettings..ctor () [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, IEnumerable`1 pins) [0x00000] in <filename unknown>:0
at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <filename unknown>:0
at Hello.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
I am able to switch pin state with Python so nothing is wrong with device.

Execute your Mono program as root. /dev/mem is not accessible to normal users.
public GpioConnectionDriver() {
using (var memoryFile = UnixFile.Open("/dev/mem", UnixFileMode.ReadWrite | UnixFileMode.Synchronized)) {
gpioAddress = MemoryMap.Create(
IntPtr.Zero,
Interop.BCM2835_BLOCK_SIZE,
MemoryProtection.ReadWrite,
MemoryFlags.Shared,
memoryFile.Descriptor,
Interop.BCM2835_GPIO_BASE
);
}
}
Explanation from here:
http://www.raspberrypi.org/forums/viewtopic.php?f=29&t=22515
To open /dev/mem you need both regular access permissions on the device file and the security capability CAP_SYS_RAWIO, or to be root. There is no getting around this, because full access to memory allows a lot more than just GPIO. It has huge security implications.

Related

SignalR run with Mono and apache: HTTP 500 error (in SignalR code)

I've been running SignalR on mono with both self-hosting on localhost and on IIS.
But self-hosting with mono on a server didn't work yet
The full error I get on the webpage is:
System.InvalidOperationException
Operation is not valid due to the current state of the object.
Description:` HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): Microsoft.Owin.Host.SystemWeb.
Exception stack trace:
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.BeginEvent (System.Object sender, System.EventArgs e, System.AsyncCallback cb, System.Object extradata) <0x404788f0 + 0x0074b> in <filename unknown>:0
at System.Web.HttpApplication+<RunHooks>c__Iterator0.MoveNext () <0x404715e0 + 0x00367> in <filename unknown>:0
at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext () <0x40468000 + 0x01a65> in <filename unknown>:0
at System.Web.HttpApplication.Tick () <0x40465950 + 0x00057> in <filename unknown>:0
In the Apache error log:
Cannot access a closed Stream.
at System.IO.MemoryStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) <0x4006e720 + 0x0008b> in <filename unknown>:0
at System.IO.BinaryWriter.Write (Int32 value) <0x4006e660 + 0x00087> in <filename unknown>:0
at Mono.WebServer.ModMonoRequest.IsConnected () <0x40540670 + 0x0001f> in <filename unknown>:0
at Mono.WebServer.Apache.ModMonoWorker.IsConnected () <0x40540640 + 0x0001b> in <filename unknown>:0
at Mono.WebServer.ModMonoWorkerRequest.IsClientConnected () <0x405405b0 + 0x00030> in <filename unknown>:0
at System.Web.HttpResponse.get_IsClientConnected () <0x40540570 + 0x0002a> in <filename unknown>:0
at System.Web.HttpResponseWrapper.get_IsClientConnected () <0x40540540 + 0x0001b> in <filename unknown>:0
at Microsoft.Owin.Host.SystemWeb.OwinCallContext.CheckIsClientConnected (System.Object obj) <0x40540490 + 0x00051> in <filename unknown>:0
at System.Threading.Timer+Scheduler.TimerCB (System.Object o) <0x4053daf0 + 0x001a7> in <filename unknown>:0
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () <0x403d3c60 + 0x0002f> in <filename unknown>:0
at System.Threading.ThreadPoolWorkQueue.Dispatch () <0x403d09b0 + 0x0021a> in <filename unknown>:0
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x403d0980 + 0x00010> in <filename unknown>:0
My code looks just fine, my program.cs contains:
class Program
{
static void Main(string[] args)
{
string url = "http://*:80";
using (Microsoft.Owin.Hosting.WebApp.Start<Startup>(url))
{
}
}
}
I have no idea what to do, should I look to change something in my code to let it work with mono?
I'm going to use SignalR Core, problem solved! :)

UCanAccess, IKVM and C#

I'm trying to make UCanAccess work in C#. I'm working on MonoDevelop using Mono on Linux. I already converted the needed .jar files into .dll .NET files using IKVM, this way:
ikvmc -target:library file.jar
Then I referenced the converted .dll files in my C# project. So, here is all DLLs I referenced:
ucanaccess-3.0.3.1.dll
hsqldb.dll
jackcess-2.1.3.dll
commons-logging-1.1.1.dll
commons-lang-2.6.dll
IKVM.OpenJDK.Core.dll
IKVM.OpenJDK.Jdbc.dll
IKVM.Runtime
I also put ucanaccess-3.0.3.1.jar, hsqldb.jar, jackcess-2.1.3.jar, commons-logging-1.1.1.jar and commons-lang-2.6.jar into my bin/Debug folder.
And here is the C# code I wrote in Main:
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("commons-lang-2.6"));
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("commons-logging-1.1.1"));
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("jackcess-2.1.3"));
//ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("hsqldb"));
ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("ucanaccess-3.0.3.1"));
java.sql.Driver v_driver = (java.sql.Driver) java.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver, ucanaccess-3.0.3.1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").newInstance();
java.sql.Connection v_con = v_driver.connect("northwind.mdb", null);
//java.lang.Class.forName("org.hsqldb.jdbcDriver, hsqldb, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
//java.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver, ucanaccess-3.0.3.1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
//java.sql.Connection v_con = java.sql.DriverManager.getConnection("jdbc:ucanaccess://northwind.mdb");
java.sql.Statement v_st = v_con.createStatement();
java.sql.ResultSet v_res = v_st.executeQuery("select * from Categories");
java.sql.ResultSetMetaData v_resmd = v_res.getMetaData();
for (int i = 0; i < v_resmd.getColumnCount(); i++)
Console.Write(v_resmd.getColumnLabel(i) + "|");
Console.WriteLine();
while (v_res.next())
{
for (int i = 0; i < v_resmd.getColumnCount(); i++)
Console.Write(v_res.getString(i) + "|");
Console.WriteLine();
}
The code compiles and when I try to execute, here is the error I got:
Unhandled Exception:
System.TypeInitializationException: An exception was thrown by the type initializer for net.ucanaccess.jdbc.UcanaccessDriver ---> java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers:RunClassConstructor (intptr)
at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (RuntimeTypeHandle type) [0x00000] in <filename unknown>:0
at IKVM.Internal.TypeWrapper.RunClassInit () [0x00000] in <filename unknown>:0
at IKVM.NativeCode.java.lang.Class.forName0 (System.String name, Boolean initialize, java.lang.ClassLoader loader) [0x00000] in <filename unknown>:0
at java.lang.Class.forName0 (System.String , Boolean , java.lang.ClassLoader ) [0x00000] in <filename unknown>:0
at java.lang.Class.forName (System.String className, ikvm.internal.CallerID ) [0x00000] in <filename unknown>:0
at java.lang.Class.forName (System.String className) [0x00000] in <filename unknown>:0
at Test.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for net.ucanaccess.jdbc.UcanaccessDriver ---> java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers:RunClassConstructor (intptr)
at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (RuntimeTypeHandle type) [0x00000] in <filename unknown>:0
at IKVM.Internal.TypeWrapper.RunClassInit () [0x00000] in <filename unknown>:0
at IKVM.NativeCode.java.lang.Class.forName0 (System.String name, Boolean initialize, java.lang.ClassLoader loader) [0x00000] in <filename unknown>:0
at java.lang.Class.forName0 (System.String , Boolean , java.lang.ClassLoader ) [0x00000] in <filename unknown>:0
at java.lang.Class.forName (System.String className, ikvm.internal.CallerID ) [0x00000] in <filename unknown>:0
at java.lang.Class.forName (System.String className) [0x00000] in <filename unknown>:0
at Test.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
The commented lines I already tried, with no success.
Am I missing something? Thanks in advance!
EDIT:
#jamadei was right, I merged all jars into one single jar and it worked!
Here is the working code (note that column indexes start in 1, not in 0):
ikvm.runtime.Startup.addBootClassPathAssemby(System.Reflection.Assembly.Load("ucanaccess-3.0.3.1"));
java.sql.Driver v_driver = (java.sql.Driver) java.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver, ucanaccess-3.0.3.1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").newInstance();
java.sql.Connection v_con = v_driver.connect("northwind.mdb", null);
java.sql.Statement v_st = v_con.createStatement();
java.sql.ResultSet v_res = v_st.executeQuery("select categoryid, categoryname, description from Categories");
java.sql.ResultSetMetaData v_resmd = v_res.getMetaData();
for (int i = 1; i <= v_resmd.getColumnCount(); i++)
Console.Write(v_resmd.getColumnLabel(i) + "|");
Console.WriteLine();
while (v_res.next())
{
for (int i = 1; i <= v_resmd.getColumnCount(); i++)
Console.Write(v_res.getString(i) + "|");
Console.WriteLine();
}
You need to merge all jars into one single jar and then converting it into dll.

Xamarin Android: System.IO.Compression.ZipFile.ExtractToDirectory Fails in release mode

Everything works fine in debug mode but when we run it in release the ExtractToDirectory call fails.
Here is the function for reference. Just to make sure we are not doing anything weird.
private bool UnzipFiles()
{
bool toReturn = true;
try
{
UpdateStatus("Almost done...");
string file = Path.Combine (DownloadFolder, "ZipFile.zip");
if(System.IO.Directory.Exists(UnzippingDestinationFolder))
{
System.IO.Directory.Delete(UnzippingDestinationFolder, recursive:true);
}
System.IO.Compression.ZipFile.ExtractToDirectory(file, UnzippingDestinationFolder);
UpdateStatus("Finished!");
var files = System.IO.Directory.GetFiles(UnzippingDestinationFolder);
int m = 3;
}
catch (Exception e)
{
toReturn = false;
}
Finally, here is the exception we get.
System.NullReferenceException: Object reference not set to an instance of an object
at SharpCompress.Common.Zip.Headers.ZipFileEntry.DecodeString (System.Byte[] str) [0x00000] in <filename unknown>:0
at SharpCompress.Common.Zip.Headers.DirectoryEntryHeader.Read (System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0
at SharpCompress.Common.Zip.ZipHeaderFactory.ReadHeader (UInt32 headerBytes, System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0
at SharpCompress.Common.Zip.SeekableZipHeaderFactory+<ReadSeekableHeader>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0
at SharpCompress.Archive.Zip.ZipArchive+<LoadEntries>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0
at SharpCompress.LazyReadOnlyCollection`1+LazyLoader[SharpCompress.Archive.Zip.ZipArchiveEntry].MoveNext () [0x00000] in <filename unknown>:0
at System.IO.Compression.ZipArchive.CreateZip (System.IO.Stream stream, ZipArchiveMode mode) [0x00000] in <filename unknown>:0
at System.IO.Compression.ZipArchive..ctor (System.IO.Stream stream, ZipArchiveMode mode, Boolean leaveOpen, System.Text.Encoding entryNameEncoding) [0x00000] in <filename unknown>:0
at System.IO.Compression.ZipFile.Open (System.String archiveFileName, ZipArchiveMode mode, System.Text.Encoding entryNameEncoding) [0x00000] in <filename unknown>:0
at System.IO.Compression.ZipFile.ExtractToDirectory (System.String sourceArchiveFileName, System.String destinationDirectoryName, System.Text.Encoding entryNameEncoding) [0x00000] in <filename unknown>:0
at System.IO.Compression.ZipFile.ExtractToDirectory (System.String sourceArchiveFileName, System.String destinationDirectoryName) [0x00000] in <filename unknown>:0
at NewBaron.Screens.DownloadContentScreen.UnzipFiles () [0x00000] in <filename unknown>:0
A slight changes to Victor's solution. Not linking the SDKs generated an apk that was 53MBs. Too large for the play store's apk size limit.
I set the linking behavior to link SDK assemblies only and it brought the apk size down to 29MBs
Here is the updated window.
#mattewrobbinsdev's suggestion was exactly it. For future readers, here's the dialog in Xamarin Studio:

Does the Unity compiler not fully support named parameters?

The following code compiles just fine in Visual Studio, but in Unity (4.6.x) it creates a compiler error:
public class ErrorTest
{
void DoSomething(bool a = true, bool f = true) { }
void DoSomething(int b, bool f = true) { }
public void SomeMethod()
{
DoSomething(f: false);
}
}
The call to DoSomething is what makes the compiler choke. The question is why, there's really no ambiguity.
The console output when Unity chokes like this:
Internal compiler error. See the console log for more information. output was:
Unhandled Exception: Mono.CSharp.InternalErrorException: Internal error
at Mono.CSharp.MethodGroupExpr.IsApplicable (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& arguments, Int32 arg_count, System.Reflection.MethodBase& method, System.Boolean& params_expanded_form) [0x00000] in <filename unknown>:0
at Mono.CSharp.MethodGroupExpr.OverloadResolve (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& Arguments, Boolean may_fail, Location loc) [0x00000] in <filename unknown>:0
at Mono.CSharp.Invocation.DoResolveOverload (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.Invocation.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0
at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.ToplevelBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, Mono.CSharp.ParametersCompiled ip, IMethodData md) [0x00000] in <filename unknown>:0
There was/is apparently a bug where combining the use of namespaces and default parameters caused an internal error like this. It was allegedly fixed in 4.5, but from the comments it has been allegedly fixed before. You might try eliminating the namespace declaration in your class as an experiment to see if that bug is what you're experiencing.

C# Constant Stream pop3

Okay so I have came to an issue that I thought I fixed with a crappy rig and it failed in the long run, so I came to ask you guys the correct way to do it. Here is the issue I have, i'm making an application to filter out a certain type of email via POP3 (using the ActiveUp api) but I need it to have a constant connection to the POP3 server...this is what I have right now:
public static void Main (string[] args)
{
while(true)
{
POP3();
Console.Title = "Waiting for "+GetSubject+POST;
Thread.Sleep(5000);
}
}
public static void POP3()
{
Pop3Client pop3 = new Pop3Client();
try
{
pop3.ConnectSsl("pop.gmail.com",995,"", "");
if(pop3.MessageCount > 0)
{
int CurrentMessage = pop3.MessageCount;
ActiveUp.Net.Mail.Message message = pop3.RetrieveMessageObject(CurrentMessage);
if(message.Subject == GetSubject+POST)
{
Console.WriteLine("command reconized from {0}: {1}\n Sending responce now!",message.From.Email, message.Subject);
SMTP (message.From.Email, _message);
POST++;
pop3.Close();
}
}
else
{
Console.WriteLine("no mail found");
}
}
catch(Pop3Exception iex)
{
Console.WriteLine(iex);
pop3.Close();
}
}
Everything works perfect until about 10-20 minuets of it running I get kicked off of google's POP3 server and have to restart the application...Ill run the application now and reedit my question with the error...but until then can you guys please help me? Thanks!
Error:
nhandled Exception: System.Net.Sockets.SocketException: No such host is known
at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
at System.Net.Sockets.TcpClient.Connect (System.String hostname, Int32 port) [0x00000] in <filename unknown>:0
at ActiveUp.Net.Mail.Pop3Client.ConnectSsl (System.String host, Int32 port, System.String user, System.String pass, ActiveUp.Net.Security.SslHandShake sslHandShake) [0x00000] in <filename unknown>:0
at ActiveUp.Net.Mail.Pop3Client.ConnectSsl (System.String host, Int32 port, System.String user, System.String pass) [0x00000] in <filename unknown>:0
at AutoThreadOwnerCopy.MainClass.POP3 () [0x00006] in /home/m44m31/AutoThreadOwnerCopy/AutoThreadOwnerCopy/Main.cs:35
at AutoThreadOwnerCopy.MainClass.Main (System.String[] args) [0x00019] in /home/m44m31/AutoThreadOwnerCopy/AutoThreadOwnerCopy/Main.cs:25
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException: No such host is known
at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
at System.Net.Sockets.TcpClient.Connect (System.String hostname, Int32 port) [0x00000] in <filename unknown>:0
at ActiveUp.Net.Mail.Pop3Client.ConnectSsl (System.String host, Int32 port, System.String user, System.String pass, ActiveUp.Net.Security.SslHandShake sslHandShake) [0x00000] in <filename unknown>:0
at ActiveUp.Net.Mail.Pop3Client.ConnectSsl (System.String host, Int32 port, System.String user, System.String pass) [0x00000] in <filename unknown>:0
at AutoThreadOwnerCopy.MainClass.POP3 () [0x00006] in /home/m44m31/AutoThreadOwnerCopy/AutoThreadOwnerCopy/Main.cs:35
at AutoThreadOwnerCopy.MainClass.Main (System.String[] args) [0x00019] in /home/m44m31/AutoThreadOwnerCopy/AutoThreadOwnerCopy/Main.cs:25

Categories

Resources