InterruptPort - Strange behavior

Im make one InterruptPort and event handler for it but it behavior very strange.
I use Cobra and IO23(I also use some other ports for other things)…
Code which I use:


using System;
using System.Threading;
 #if DEBUG
using Microsoft.SPOT;
 #endif
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace PEPP
{
    class IOBoard
    {
        #region Variables & Constants

        private InterruptPort DoorState;

        public delegate void DoorStateChangeEventHandler(InterruptPort sender, bool state);
        public event DoorStateChangeEventHandler OnDoorStateChange;

        #endregion

        #region Constructor

        public IOBoard()
        {
            try
            {
                //Inputs
                DoorState = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.IO23, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
                DoorState.OnInterrupt += new NativeEventHandler(DoorState_OnInterrupt);
            }
            catch (Exception e)
            {
                Module.WriteErrorLog("IOBoard.IOBoard() - ERROR:" + e.Message.ToString());
            }
        }

        #endregion

        #region Events

        private void DoorState_OnInterrupt(uint port, uint state, DateTime time)
        {
            try
            {
                if (DoorState.Read())
                {
                    //Make some things
                }
                else
                {
                    //Make some things
                }
                OnDoorStateChange(DoorState, (DoorState.Read()) ? false : true);
            }
            catch { }
        }

        #endregion

        #region Public Methods

        #endregion

        #region Private Methods

        #endregion
    }
}


Im test app all night and approximately every 50 min I get error:


01/01/1977 03:47:47: 5130 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
The thread '<No Name>' (0x141c) has exited with code 0 (0x0).
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
01/01/1977 03:47:51: 5131 Network online: True
01/01/1977 04:34:39: 6550 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
The thread '<No Name>' (0x19a8) has exited with code 0 (0x0).
01/01/1977 04:34:41: 6551 Network online: True
01/01/1977 05:08:16: 7603 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
01/01/1977 05:08:21: 7606 Network online: True
01/01/1977 05:55:55: 9097 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
The thread '<No Name>' (0x239c) has exited with code 0 (0x0).
01/01/1977 05:55:57: 9098 Network online: True
01/01/1977 06:28:33: 10110 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
01/01/1977 06:28:35: 10111 Network online: True
01/01/1977 07:15:40: 11573 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
01/01/1977 07:15:42: 11574 Network online: True
01/01/1977 07:48:36: 12569 Network online: True
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0040] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
01/01/1977 07:48:40: 12570 Network online: True

In network testing thread I do not do anything with Inputs or outputs…
Also PIN is directly connected to ground all time and nobody do not touch it.

Personally I think something you’re instantiating is being GCed. So that probably means you will need to show main() and how you set things up

GC has run few times yes:


GC: 40msec 618984 bytes used, 9276480 bytes available
Type 0F (STRING              ):   5892 bytes
Type 11 (CLASS               ):  17436 bytes
Type 12 (VALUETYPE           ):    684 bytes
Type 13 (SZARRAY             ): 282972 bytes
Type 15 (FREEBLOCK           ): 9276480 bytes
Type 16 (CACHEDBLOCK         ):  60912 bytes
Type 17 (ASSEMBLY            ):  31872 bytes
Type 18 (WEAKCLASS           ):    144 bytes
Type 19 (REFLECTION          ):    192 bytes
Type 1B (DELEGATE_HEAD       ):   1764 bytes
Type 1C (DELEGATELIST_HEAD   ):    180 bytes
Type 1D (OBJECT_TO_EVENT     ):    768 bytes
Type 1E (BINARY_BLOB_HEAD    ): 203784 bytes
Type 1F (THREAD              ):   3456 bytes
Type 20 (SUBTHREAD           ):    384 bytes
Type 21 (STACK_FRAME         ):   3636 bytes
Type 22 (TIMER_HEAD          ):    144 bytes
Type 27 (FINALIZER_HEAD      ):    864 bytes
Type 31 (IO_PORT             ):    720 bytes
Type 34 (APPDOMAIN_HEAD      ):     72 bytes
Type 36 (APPDOMAIN_ASSEMBLY  ):   3108 bytes

In main I don’t setup event and only on app start init board in new thread and setup default states:


io_board = new IOBoard();
io_board.SetDefaultState();
io_board.OnTamperInputChange += new IOBoard.TamperInputEventHandler(io_board_OnTamperInputChange);
io_board.OnServiceButtonChange += new IOBoard.ServiceButtonEventHandler(io_board_OnServiceButtonChange);    

In IOBoard I also use other InterruptPorts which do not produce this automatic events!!!
For example:

ServiceButton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.IO0, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
ServiceButton.OnInterrupt += new NativeEventHandler(ServiceButton_OnInterrupt);
TamperInput = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.IO1, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
TamperInput.OnInterrupt += new NativeEventHandler(TamperInput_OnInterrupt);
Sensor = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.IO45, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
Sensor.OnInterrupt += new NativeEventHandler(Sensor_OnInterrupt);

Im add some “debug” points:


private void DoorState_OnInterrupt(uint port, uint state, DateTime time)
        {
            try
            {
                Debug.Print("1");
                if (DoorState.Read())
                {
                    Debug.Print("2");
                    Output(1, false);
                    Debug.Print("3");
                    Output(2, false);
                    Debug.Print("4");
                }
                else
                {
                    Debug.Print("5");
                    Output(1, true);
                    Debug.Print("6");
                }
                Debug.Print("7");
                OnDoorStateChange(DoorState, (DoorState.Read()) ? false : true);
                Debug.Print("8");
            }
            catch { }
        }

And output:


01/01/1977 01:29:32: 866 Network online: True
1
5
6
7
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0071] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
The thread '<No Name>' (0x374) has exited with code 0 (0x0).
01/01/1977 01:29:35: 867 Network online: True
01/01/1977 02:13:52: 2238 Network online: True
1
5
6
7
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
    #### Message: 
    #### DDD.IOBoard::DoorState_OnInterrupt [IP: 0071] ####
A first chance exception of type 'System.NullReferenceException' occurred in Application.exe
The thread '<No Name>' (0x8d0) has exited with code 0 (0x0).
01/01/1977 02:13:54: 2239 Network online: True

There is not problem with null exception because Im already handle it with:


if (OnDoorStateChange != null)
    OnDoorStateChange(DoorState, (DoorState.Read()) ? false : true);

Issue is why is Interrupt event raised on port IO23 after cca. 50 minutes if there is not any IO change on device. It use only PPP connection and “ping” host, showing images on LCD(Change image every 5 second and read it from SD card), Have opened serial communication with RFID reader(Sending data to it) and nothing else.
So device use:
1xUART for PPP(Motorola G24 GPRS) (active state)
1xUART for RFID Read/Write device (active state)
1xUSB UART(FT232R) for RS485 communication(idle state)
1xOneWire for DS18B20(idle state)
1xLCD (active state)

I don’t have idea what can be wrong… Im make global static port and change input:


private static InterruptPort DoorState = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.IO21, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);

But do not help. I don’t have anymore null exception error but Interrupt is raised without any user action…

What’s connected to that pin? Have you tried disconnecting it and see if the magic still happens? Might be you have something screwy going on with your external trigger.

To this pin I have connected Cobra’s ground so I don’t use switch or something like this… It is connected direct to ground. I will try disconnect it and test…

I don’t understand why you would do that. Is there something else connected to ground that could possibly be feeding back into your InterruptPin?

Hmmm maybe I didin’t understand how work InterruptPin’s… Is not:
-When pin is connected to ground that port state is True
-When on pin is not connected anything that port state if False

All I wan’t is that I connect switch/button/magnet switch to pin and detect when door are opened/closed also event must be raised when someone open/close door.
Will connecting it to 3.3V be better option? So it will be:
-When pin is connected to 3.3V that port state is True
-When on pin is not connected anything that port state if False

P.S. Ground is used for all devices(Cobra, GPRS, RFID …)

So, there is a switch/button? I thought you were saying you just had it wired directly to ground. The other thing to check is your Cpu.GlitchFilterTime. You may have a really sensitive switch that is sending false interrupts.

Also, check this post for anything you might be missing.

@ ianlee74: I think Im figure out what cause problem, but Im not 100% shure…
In main class named “Module” I load other classes and one of them is this IOModule class. I for each class start new thread that I get faster loading time(Because some classes need wait some time to external modules start and in this time can other modules normaly load and init… When “module”(class) loading&init is completed this thread stop and exit. Im in “Module” class create this modules as global public but looks like GC has clear some things in this classes:

public IOBoard io_board;

Im change them to static:


public static IOBoard io_board;

And now after 1h looks like it work no mather if GC run every 10-20 minutes:


GC: 17msec 527796 bytes used, 9367668 bytes available
Type 0F (STRING              ):   5616 bytes
Type 11 (CLASS               ):  14088 bytes
Type 12 (VALUETYPE           ):    660 bytes
Type 13 (SZARRAY             ): 267084 bytes
Type 15 (FREEBLOCK           ): 9367668 bytes
Type 17 (ASSEMBLY            ):  31848 bytes
Type 18 (WEAKCLASS           ):    144 bytes
Type 19 (REFLECTION          ):    192 bytes
Type 1B (DELEGATE_HEAD       ):    684 bytes
Type 1D (OBJECT_TO_EVENT     ):    480 bytes
Type 1E (BINARY_BLOB_HEAD    ): 198828 bytes
Type 1F (THREAD              ):   1920 bytes
Type 20 (SUBTHREAD           ):    240 bytes
Type 21 (STACK_FRAME         ):   1572 bytes
Type 22 (TIMER_HEAD          ):     72 bytes
Type 27 (FINALIZER_HEAD      ):    456 bytes
Type 31 (IO_PORT             ):    540 bytes
Type 34 (APPDOMAIN_HEAD      ):     72 bytes
Type 36 (APPDOMAIN_ASSEMBLY  ):   3300 bytes

Now I must only check how to fix leak for SZARRAY&BINARY_BLOB_HEAD when loading JPG images from SDcard and show them on LCD :slight_smile:

Thanks for your help.

Excellent!