- Sep 10, 2001
- 12,348
- 1
- 81
So I've been programming in C# since last Friday or so and I've written a basic program that allows me to talk to the piece of hardware that I am controlling. I can make the hardware do more or less as I want while only using negligible processing power.
The problem I'm having is that getting the data back from the hardware and trying to manage it is very processor intensive. Whether this is a result of the way I'm managing it or not is unclear to me at this point. I'm getting 17 doubles from the hardware every 2 milliseconds, then throwing them in a double-buffered array. The code I'm using to do that is:
for (int i = 0; i < 8; i++)
{
if (ind > ArrayLength - 1)
{
ind = 0;
if (j==1) { j=0; } else { j = 1; }//toggle j to switch buffers
}
Time[ind, j] = g.sourceValue(record, "TIME");
Pos[i, ind, j] = g.sourceValue(record, "TP" + AxesString);
Force[i, ind, j] = g.sourceValue(record, "TT" + AxesString);
ind += ind;//increment the counting variable
}
This executes on an interrupt from the hardware (every 2 ms). It's critical that I don't lose any of this data, so having this run as fast as possible is very important. As I have no formal programming/CS training, I'm not really sure if this is the best way to do it. I thought that I could run this in its own thread to ensure that it has enough CPU time to do its thing, but I'm not really sure how to make that happen. Does anyone have any advice on how I can speed this up/ensure that I don't miss data or on whether this could/should run in its own thread? Like I said, this is amateur hour here so I won't be offended by very simple answers.
The problem I'm having is that getting the data back from the hardware and trying to manage it is very processor intensive. Whether this is a result of the way I'm managing it or not is unclear to me at this point. I'm getting 17 doubles from the hardware every 2 milliseconds, then throwing them in a double-buffered array. The code I'm using to do that is:
for (int i = 0; i < 8; i++)
{
if (ind > ArrayLength - 1)
{
ind = 0;
if (j==1) { j=0; } else { j = 1; }//toggle j to switch buffers
}
Time[ind, j] = g.sourceValue(record, "TIME");
Pos[i, ind, j] = g.sourceValue(record, "TP" + AxesString);
Force[i, ind, j] = g.sourceValue(record, "TT" + AxesString);
ind += ind;//increment the counting variable
}
This executes on an interrupt from the hardware (every 2 ms). It's critical that I don't lose any of this data, so having this run as fast as possible is very important. As I have no formal programming/CS training, I'm not really sure if this is the best way to do it. I thought that I could run this in its own thread to ensure that it has enough CPU time to do its thing, but I'm not really sure how to make that happen. Does anyone have any advice on how I can speed this up/ensure that I don't miss data or on whether this could/should run in its own thread? Like I said, this is amateur hour here so I won't be offended by very simple answers.