I'm a program beginner.
Using Visual Studio 2019, we are creating a program to capture data for machine learning in C#.
Turn the for statement and change the file name according to the value of i.
Output various parameters to txt every second.
Currently, the data can be acquired, but the data cannot be acquired every second, and the number of seconds is jumping.
How do I get the value every second?
Changing the contents of the for statement to something other than 60 did not allow data to be retrieved once a second.
string fileName;
for (inti=0; i<60;i++)
{
if(NativeThinkear.TG_GetValueStatus(connectionID, NativeThinkear.DataType.TG_DATA_ATTENTION)!=0)
{
if(i==0)
{
SystemSounds.Beep.Play();
fileName=fileNameFolder+"data0.txt";
}
else if (i<=21)
{
fileName=fileNameFolder+"data1to21.txt";
}
else if(i==22)
{
SystemSounds.Beep.Play();
fileName=fileNameFolder+"data22.txt";
}
else if (i<=52)
{
fileName=fileNameFolder+"data23to43.txt";
}
else if(i==44)
{
SystemSounds.Beep.Play();
fileName=fileNameFolder+"data44.txt";
}
else
{
fileName = fileNameFolder + "data45to60.txt";
}
using (StreamWriter filewrite000 = new StreamWriter(fileName, true))
{
attention = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_ATTENTION);
meditation = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_MEDITATION);
delta = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_DELTA);
theta = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_THETA);
lowalpha = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_ALPHA1);
highalpha = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_ALPHA2);
lowbeta = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_BETA1);
highbeta = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_BETA2);
lowgamma = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_GAMMA1);
highgamma = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_GAMMA2);
DateTime dt = DateTime.Now;
filewrite000.Write(attention + ", " + meditation + ", " + delta + ", " + theta + ", " + lowalpha + ", " + highalpha + ", " + lowbeta + ", " + highbeta + ", " + lowgamma + ", " + highgamma + ", " + dt.ToString() + ", " + state+"\n");
Console.WriteLine(" ATTENTION: MEDITATION: STATE: DELTA: THETA: LOWALPHA: HIGHALPHA: LOWBETA: HIGHBETA: LOWGAMMA: HIGHGAMMA: " + attention, +meditation, +state, +delta, +theta, +lowalpha, +highalpha, +lowbeta, +highbeta, +lowgamma, +highgamma, dt.ToString());
}
}
if(NativeThinkear.TG_GetValueStatus(connectionID, NativeThinkear.DataType.TG_DATA_RAW)!=0)
{
if(NativeThinkear.TG_GetValueStatus(connectionID, NativeThinkear.DataType.MWM15_DATA_FILTER_TYPE)!=0)
{
Console.WriteLine("Find Filter Type:" + NativeThinkear.TG_GetValue(connectionID, NativeThinkear.DataType.MWM15_DATA_FILTER_TYPE) +"index:" + packetsRead);
// break; // Implementation //
}
/* Get and print out the updated raw value*/
NativeThinkear.TG_GetValue(connectionID, NativeThinkear.DataType.TG_DATA_RAW);
packetsRead++;
if (packetsRead==800||packetsRead==1600) // call twice interval than 1s (512)
{
errCode = NativeThinkear.MWM15_getFilterType(connectionID);
Console.WriteLine("MWM15_getFilterType called:"+errCode);
}
}
}
Typically, .NET uses a timer class for periodic processing.
There are several types of timer classes, but I think System.Timers.Timer is safe.
How do I process the timer at regular intervals? (Server-Based Timer Edition)
I don't know what the class and method used in the source is, and I don't have any explanation, so I don't know what I'm doing at all, but if it's jumped, doesn't it take more than a second to process it?
Try measuring the amount of time each process takes in the Stopwatch class.
© 2024 OneMinuteCode. All rights reserved.