L
LordHog
Hello all,
I have a little application that needs to poll a device (CAN
communications) every 10 to 15 ms otherwise the hardware buffer might
overflow when there are message burst on the bus. I would implement an
interrupt driven model, but the external device (which is connected
via USB) does not support interrupts therefore the device needs to be
polled at a specific interval. So my question is how can I implement a
"deterministic" polling mechanism in C# (.NET)?
Currently, once the CAN communications class is instantiated it
creates a polling thread which is implemented using the
System.Threading.Thread class which the thread creation mechanism
looks like
// Note: All error checking was removed from sample code
ThreadStart threadDelegate = new
ThreadStart(PollingThreadDelegate);
m_PoolingThread = new Thread(threadDelegate);
m_PoolingThread.IsBackground = true;
m_PoolingThread.Priority = ThreadPriority.AboveNormal;
m_PoolingThread.Start();
Then in the method which implements the thread delegate looks like
private void PollingThreadDelegate( )
{
while ( true )
{
// FreqCnt is of type System.Diagnostics.Stopwatch
FreqCnt.Stop();
if ( FreqCnt.ElapsedMilliseconds > WorstTime )
{
WorstTime = FreqCnt.ElapsedMilliseconds;
}
FreqCnt.Reset();
FreqCnt.Start();
m_RxThreadIsActive = false;
m_PoolingThread.Join( (int)m_ScanModeInterval );
// Perform Read from USB interface to retrieve message from
the CAN
// device and place them in a queue
// Once all messages have been retrieved and if message count
greater
// than 0 then raise an event so the upper layer can retrieve
the
// message from the queue
}
The problem that I have with the current implementation is that
polling thread needs to be deterministic and trigger at an interval no
greater than 15 ms otherwise there is a high chance of dropping
messages during communications burst. I have tweaked the application
to the best of my ability, but instrumentation that I have (FreqCnt)
tells me the worst case timing has been in the 100 ms time frames.
I am not sure how to create the thread so it is deterministic so the
application doesn't drop any messages. Since the CAN communications
thread is not on the same thread the created the presentation layer,
which displays the information for the user, needs a delegate (correct
term?) so the process can return to the thread that created the
controls to update the controls (implemented using the MethodInvoker).
I have tried several different approaches, but none of them work
correctly. I have tried taking out the m_PoolingThread.Join statement,
but then polling thread starves the rest of the system as one could
image and especially the presentation thread. In addition, I have
tried different ThreadPriority levels though none of them help.
Any help is greatly appreciated.
Mark
I have a little application that needs to poll a device (CAN
communications) every 10 to 15 ms otherwise the hardware buffer might
overflow when there are message burst on the bus. I would implement an
interrupt driven model, but the external device (which is connected
via USB) does not support interrupts therefore the device needs to be
polled at a specific interval. So my question is how can I implement a
"deterministic" polling mechanism in C# (.NET)?
Currently, once the CAN communications class is instantiated it
creates a polling thread which is implemented using the
System.Threading.Thread class which the thread creation mechanism
looks like
// Note: All error checking was removed from sample code
ThreadStart threadDelegate = new
ThreadStart(PollingThreadDelegate);
m_PoolingThread = new Thread(threadDelegate);
m_PoolingThread.IsBackground = true;
m_PoolingThread.Priority = ThreadPriority.AboveNormal;
m_PoolingThread.Start();
Then in the method which implements the thread delegate looks like
private void PollingThreadDelegate( )
{
while ( true )
{
// FreqCnt is of type System.Diagnostics.Stopwatch
FreqCnt.Stop();
if ( FreqCnt.ElapsedMilliseconds > WorstTime )
{
WorstTime = FreqCnt.ElapsedMilliseconds;
}
FreqCnt.Reset();
FreqCnt.Start();
m_RxThreadIsActive = false;
m_PoolingThread.Join( (int)m_ScanModeInterval );
// Perform Read from USB interface to retrieve message from
the CAN
// device and place them in a queue
// Once all messages have been retrieved and if message count
greater
// than 0 then raise an event so the upper layer can retrieve
the
// message from the queue
}
The problem that I have with the current implementation is that
polling thread needs to be deterministic and trigger at an interval no
greater than 15 ms otherwise there is a high chance of dropping
messages during communications burst. I have tweaked the application
to the best of my ability, but instrumentation that I have (FreqCnt)
tells me the worst case timing has been in the 100 ms time frames.
3 percent of the messages which is a high rate.From other instrumentation generally the application drops about 2 to
I am not sure how to create the thread so it is deterministic so the
application doesn't drop any messages. Since the CAN communications
thread is not on the same thread the created the presentation layer,
which displays the information for the user, needs a delegate (correct
term?) so the process can return to the thread that created the
controls to update the controls (implemented using the MethodInvoker).
I have tried several different approaches, but none of them work
correctly. I have tried taking out the m_PoolingThread.Join statement,
but then polling thread starves the rest of the system as one could
image and especially the presentation thread. In addition, I have
tried different ThreadPriority levels though none of them help.
Any help is greatly appreciated.
Mark