I'm working on a personal project using a WinCE7 system and coding using C#. I need access to the system's GPIO and I have been provided with a GPIO api but little to no explanation or support from the developer. I've successfully used the api to set the output and get the input via polling but I need to set an interrupt driven mechanism. The api has a function that allows me to achieve this but i have been unable to figure out how to use it. The function looks like this: "BOOL GpioSetupInterruptPin(HANDLE hGPIO, PIN_GPIO_PORT port, UINT pin, INT_TRIGGER_MODE mode, INT_TRIGGER_POLARITY polarity, LPCWSTR szEventName, BOOL enable);"
I've been given the structures for the PORT, TRIGGER_MODE and TRIGGER_POLARITY and the following description for szEventName: "event name of a named event", but that is about it.
I'm new at event handling in c# and i've done a lot of tutorials and code samples i found on google but i'm not sure how to pass my event to the above function. I've followed a number of examples online but I still don't understand what i'm doing wrong.
Things i know i'm doing wrong in the code bellow: my event has no arguments, the code in red is not recognized by the compiler. I would really appreciate some help with this.
public partial class Form1 : Form
{
private void Interrupt_Setup(){
hGPIO = GPIOapi.GpioOpenHandle(); //returns a handle to the gpioGIPO_ON = true;Debug.WriteLine("Driver open \n" + hGPIO);string Trigger = "InputProcessUpdateHandler";IntPtr handle = CreateEvent(IntPtr.Zero, false, true, Trigger); //used P/InvokeGPIO_Output(); //set output pinsGPIO_Interrupt(Trigger);//configure interrupt
}private void GPIO_Interrupt(string trigger){
bool ok;//INTERRUPT DECALRATIONok = GPIOapi.GpioSetupInterruptPin(hGPIO, port6, 4, GPIOapi.INT_TRIGGER_MODE.TRIGGER_MODE_EDGE,GPIOapi.INT_TRIGGER_POLARITY.TRIGGER_POL_HIGH_RISING, trigger, true); //throws exceptionif (!ok)Debug.WriteLine("NO interrupt");elseDebug.WriteLine("Interrupt set for:" + port6 + "04" + " at " + hGPIO);
}private InputProcessor _inputProcessor = null;public ShowToggle(InputProcessor inputProcessor){
_inputProcessor = inputProcessor;_inputProcessor.updateHandeledBy += InputProcessUpdateHandler;
}private void InputProcessUpdateHandler(){
Debug.Write("execute");
}}
public class InputProcessor
{
public event InputProcessUpdateHandler updateHandledBy = null;public void Process(Label label){
if (updateHandledBy != null)updateHandledBy(label);
}
}
Some of the sites i've looked at:
http://www.pinfaq.com/123/events-and-delegates-and-its-difference-dotnet-application
http://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable
http://timok.tumblr.com/post/57441520214/simplistic-event-example-in-c
http://www.codearsenal.net/2012/09/csharp-custom-event-handling.html
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396%28v=vs.85%29.aspx
http://stackoverflow.com/questions/12576944/triggering-event-in-c-sharp-from-c-dll?lq=1
http://stackoverflow.com/questions/17073386/raise-events-in-c-cli-dll-and-consume-in-c-sharp?lq=1
VulpesPosted Nov 27, 2014, 11:07 AM
If it still doesn't work, can you tell me what exception you're getting?
VulpesPosted Nov 27, 2014, 11:30 AM
I know how frustrating it can be trying to call C functions when you don't have proper documentation or examples.
Stefan CazacuPosted Nov 27, 2014, 11:26 AM
Stefan CazacuPosted Nov 27, 2014, 10:45 AM
[DllImport("gpiosdk53.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool GpioSetupInterruptPin(IntPtr hGPIO, PIN_GPIO_PORT port, uint pin, INT_TRIGGER_MODE mode, INT_TRIGGER_POLARITY polarity, [MarshalAs(UnmanagedType.LPArray)] String szEventName, bool enable);
I've fixed the red lines but calling GpioSetupInterruptPin still throws an exception
VulpesPosted Nov 27, 2014, 10:22 AM
Incidentally, the reasons for the red lines appear to be that you haven't defined the InputProcessUpdateHandler delegate anywhere and there's a typo in the updateHandeledBy event which should be updateHandledBy. However, as mentioned earlier, I don't think you need this stuff anyway.