11-08-2017 06:42 AM
When I use this method ReadMultiSample in ReadCcv and ReadOcv its take more then a second of run time.
I think its too long What can I do to make it run faster?
My controller class
class Controller
{
public AnalogMultiChannelReader reader;
public bool TaskIsRunning { get; set; }
public double Current_ { get; set; }
public double dTime_ { get; set; }
public Single Ocv_ { get; set; }
public Single Ccv_ { get; set; }
public long OcvtaskHandle { get; set; }
private MessageBasedSession mbSession;
Task CcvTask;
Task OcvTask;
~Controller()
{
CcvTask.Dispose();
OcvTask.Dispose();
}
public Controller() {
CcvTask = null;
OcvTask = null;
}
public double ReadCcv()
{
// Create a new task
double frequency = 50000;
double TimePerPoint = (1 / frequency);
int numSampsPerChannel = (int)((dTime_ / 1000.0) * frequency) + 95;
if (CcvTask == null)
{
InitLoad();
// Initialize Local Variables
double rangeMinimum = Convert.ToDouble(GlobalVariables.rangeMinVolt);
double rangeMaximum = Convert.ToDouble(GlobalVariables.rangeMaxVolt);
CcvTask = new Task();
CcvTask.AIChannels.CreateVoltageChannel(GlobalVariables.voltageSorceInput, "",
(AITerminalConfiguration)(-1), rangeMinimum, rangeMaximum, AIVoltageUnits.Volts);
CcvTask.Timing.ConfigureSampleClock("", frequency, SampleClockActiveEdge.Rising,
SampleQuantityMode.FiniteSamples, numSampsPerChannel);
CcvTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(GlobalVariables.triggerSorcePort, DigitalEdgeStartTriggerEdge.Rising);// ReferenceTrigger.ConfigureDigitalEdgeTrigger(
CcvTask.Control(TaskAction.Verify);
}
try
{
AnalogMultiChannelReader reader = new AnalogMultiChannelReader(CcvTask.Stream);
SendSCPI("PROG:STAT RUN");
double[,] x = reader.ReadMultiSample(numSampsPerChannel);
string res = "";
return x[0, numSampsPerChannel - 90];
}
catch (DaqException exception)
{
//startButton.Enabled = true;
//myTask.Dispose();
throw exception;
}
}
public bool GetFootSwitch(){
Task myTask = new NationalInstruments.DAQmx.Task();
myTask.DIChannels.CreateChannel(GlobalVariables.footSwitchPort, "FootSwitch",ChannelLineGrouping.OneChannelForEachLine);
DigitalSingleChannelReader reader = new DigitalSingleChannelReader(myTask.Stream);
bool val = reader.ReadSingleSampleMultiLine().First();
return val;
}
public void BeepOk(int Periode=600){
Task myTask = new Task();
myTask.DOChannels.CreateChannel(GlobalVariables.beepPort, "beep",ChannelLineGrouping.OneChannelForAllLines);
DigitalSingleChannelWriter writer = new DigitalSingleChannelWriter(myTask.Stream);
writer.WriteSingleSampleMultiLine(true, new bool[]{true});
Thread.Sleep(Periode);
writer.WriteSingleSampleMultiLine(true, new bool[]{false});
}
public double ReadOcv()
{
int numSampsPerChannel = 1000;
int numChannels = 1;
List<double> data = new List<double>();
double rangeMinimum = Convert.ToDouble(GlobalVariables.rangeMinVolt);
double rangeMaximum = Convert.ToDouble(GlobalVariables.rangeMaxVolt);
string x = "";
int arraySizeInSamps = numSampsPerChannel * numChannels;
if (OcvTask == null)
{
OcvTask = new Task();
OcvTask.AIChannels.CreateVoltageChannel(GlobalVariables.voltageSorceInput, "", (AITerminalConfiguration)(-1), rangeMinimum, rangeMaximum, AIVoltageUnits.Volts);
OcvTask.Timing.ConfigureSampleClock("", numSampsPerChannel, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, 101);
data = new List<double>(arraySizeInSamps - 1);
}
AnalogSingleChannelReader r = new AnalogSingleChannelReader(OcvTask.Stream);
data = r.ReadMultiSample(numSampsPerChannel).ToList();
return data.Average();
}
private void SendSCPI(string text)
{
mbSession.BeginWrite(text + "\n");
}
public void InitLoad()
{
ResourceManager rmSession = ResourceManager.GetLocalManager();
mbSession = (UsbSession)rmSession.Open("USB0::0x0B3E::0x1004::ND002613::0::INSTR", AccessModes.NoLock, 10000);
SendSCPI("PROG:FSP:END 10");
SendSCPI("PROG:FSP:TIME 0.0001");
SendSCPI("PROG:NAME 11");
for (int j = 1; j <= 10; j++)
{
Thread.Sleep(5);
SendSCPI("PROG:FSP:EDIT " + j + "," + (Current_/1000).ToString("#0.000") + ",1,1");
}
SendSCPI("PROG:LOOP" + dTime_);
}
// This routine reads the string returned by the instrument
private string GetSCPI()
{
string ret = mbSession.ReadString();
return ret.Replace("\n", "").Replace("\0", "");
}
public void initFluke(){
ResourceManager rmSession = ResourceManager.GetLocalManager();
mbSession = (SerialSession)rmSession.Open("asrl3::instr",AccessModes.NoLock,10000);
SendSCPI("PROG:FSP:END 10");
((SerialSession)mbSession).BaudRate = 115200;
((SerialSession)mbSession).FlowControl = 0;
((SerialSession)mbSession).Parity = 0;
}
}
My main class :
class Program
{
static Controller controller = new Controller();
static void testRun() {
double ocv = controller.ReadOcv();
double ccv = controller.ReadCcv();
}
static void Main(string[] args)
{
controller.dTime_ = 100;
controller.Current_ = 5000;
testRun();
testRun();
testRun();
testRun();
testRun();
Console.ReadLine();
}
}
11-09-2017 09:38 AM
Hi Shazoom,
This is likely an issue with how you are configuring your sample clock. Check out the forum post below and consider working on your sample timing.
I also notice you are calculating and casting to a list in line with your measurement. You may want to look into the computing resources associated with that. Lastly, next time you post code on the forum I would highly recommend keeping the formatting and adding code documentation. It is difficult to sift through undocumented code.
Good luck!