# Computer peripherals to immediately (<5ms) detect digital input state change in C# programs

The C# application I'm working on needs to be able to be able to call a function within 5 milliseconds of a digital input signal's state change (0V = off, 5V = on).

Rather than using software to repeatedly poll that input's state, I would like some hardware to simply call a software function when the hardware detects a state change (like how hardware interrupts work).

Does anyone know how to do this (what ports to use) on a standard PC tower? Or does anyone know of any hardware peripherals that allow this functionality?

Occasional latencies are fine. I need <5ms average.

I basically need to take as many samples as possible of the input digital waveform. (preferably in the C# program, rather than in a peripheral micro-controller, although I am open to suggestions).

For instance, I found out the Labjack U3 can poll its digital input in less than a millisecond. This is a valid answer to the question, but I am interesting in more answers, especially ones that don't use polling or poll faster than 1ms.

High input impedance would be nice (digital inputs).

• 2ms hard realtime response? Under Windows? – Spehro Pefhany Jun 1 '15 at 21:12
• No way you are doing it. If you tell us the real problem, we might help you to come up with appropriate solution. – Eugene Sh. Jun 1 '15 at 21:17
• @ilkhd the C# or C++ program trying to do the data sampling and timestamping would have failed horribly at the task, as you say - but it has nothing to do with garbage collection. It's the operating system and the random 10ms delays come OS (kernel) level operations which ruin the performance – KyranF Jun 1 '15 at 23:14
• @ilkhd you must be doing something wrong mate, sorry. – KyranF Jun 1 '15 at 23:19
• @ilkhd My experience proves otherwise, you overestimate the delays. It may not be guaranteed performance but 1 minute random delays? No. I had amazing speed, zero delays, AND all running in a Windows Forms GUI application to boot. – KyranF Jun 1 '15 at 23:27

Do you need to perform a specific real-time action, or just record the event?

If the former, you will never accomplish this using commodity hardware and consumer-grade interfaces. You will need both a laboratory-grade signal processor ( it will be a card that can access hardware interrupts directly) and a real-time operating system like VxWorks. A user-centric OS won't interrupt a disk write just because the USB bus wants some attention.

If the latter, get yourself a signal recorder and post-process the buffer output at your convenience.

• I do not need to do anything in real-time, I simply need to time-stamp and record falling and rising edges, as they occur. I'm not sure what you mean by "signal recorder", but I really don't want do put the digital line into any ADCs, analog functionality is not really what I want. – OrangeSherbet Jun 2 '15 at 16:31

If your signal does not have too much of DC (long pauses with no data), use the sound card.

• Explain why.... – Null Jun 2 '15 at 0:50
• @Null what exactly you want me to explain? – ilkhd Jun 2 '15 at 2:43
• My signal has long periods of low DC (10 seconds of low). A sound card is an innovative idea, however I would have to scale my digital input down to 400mV, and I don't really need all the analog capability, would be better to go directly to something with a Digital Input pin. – OrangeSherbet Jun 2 '15 at 16:29
• @OrangeSherbet well, a standard modern PC does not have a buffered digital input. Sound card is the only option. to limit the digital signal all you need is a diode and 3 resistors. If you decide to go the sound card way, I'll explain how to deal with DC. – ilkhd Jun 3 '15 at 1:33

I ended up using an Arduino Uno's Timer Input Capture Interrupt Service Routine to set data over USB to a C# SerialPort object immediately upon input capture. The SerialPort fires a serialdatareceivedeventhandler immediately upon detecting an End Of File Character (0x1A), and a new thread launches to process the input data (such as assigning a timestamp to it).

To all the neighsayers, What the Heck? Obviously you were wrong that it couldn't be done.

• Have you verified your max 5ms latency?? – Scott Seidman Jun 11 '15 at 18:04
• I'm not sure how I would measure that (how to synchronize arduino and computer...), but it appears that I have a resolution of about 2 milliseconds (not latency, instead that's how much the latency varies). I really needed a latency that didn't vary by more than a few milliseconds, a constant latency was desirable. – OrangeSherbet Jun 11 '15 at 21:34