public static void ThrowErrorMessage(string msg, int errorCode) { StringBuilder errorString = new StringBuilder(50); LabJack.GetErrorString(errorCode, errorString); throw new Exception(msg + ":\r\n\r\n" + errorString); }
public static void SetDigitalOutput(int channel, bool writeD, int state) { if (channel == 0) { return; } #if MOCKUP lock (lockobj) { MockUp.SetDState(channel - 1, state != 0); } #elif DEBUG lock (lockobj) { RpiGPIO.SetDState(channel, state != 0, gpio); } #else lock (lockobj) { var ljID = 0; var result = LabJack.EDigitalOut(ref ljID, 0, channel - 1, writeD ? 1 : 0, state); if (result != 0) { throw new Exception("Error setting digital output"); } } #endif }
//--------------------------------------------------------------------- // AIBurst // public static float[,] AIBurst(int numScans) { int ljID = -1; int stateIOin = 0; int[] channels = { 0, 0, 0, 0 }; int[] gains = { 0, 0, 0, 0 }; float scanRate = 2048; float[,] voltages = new float[4096, 4]; int[] stateIOout = new int[4096]; int overVoltage = 0; int result = LabJack.AIBurst(ref ljID, 0, stateIOin, 0, 1, 1, channels, gains, ref scanRate, 0, 0, 0, numScans, 5, voltages, stateIOout, ref overVoltage, 2); if (result != 0) { ThrowErrorMessage("Error setting digital output", result); } return(voltages); }
public static int ReadDigitalInput(int channel, bool readD) { if (channel == 0) { return(0); } #if MOCKUP lock (lockobj) { return(MockUp.GetDState(channel - 1) ? 1 : 0); } #elif DEBUG lock (lockobj) { return(RpiGPIO.GetDState(channel, gpio) ? 1 : 0); } #else lock (lockobj) { var ljID = 0; var state = 0; var result = LabJack.EDigitalIn(ref ljID, 0, channel - 1, readD ? 1 : 0, ref state); if (result != 0) { throw new Exception("Error reading digital input"); } return(state); } #endif }
public static int ReadDigital() { #if MOCKUP lock (lockobj) { int result = 0; for (int p = 1, n = 0; n < 16; ++n, p <<= 1) { if (MockUp.GetDState(n)) { result |= p; } } return(result); } #elif DEBUG lock (lockobj) { int result = 0; for (int p = 1, n = 0; n < 16; ++n, p <<= 1) { if (RpiGPIO.GetDState(n, gpio)) { result |= p; } } return(result); } #else lock (lockobj) { var ljID = 0; var outputd = 0; var trisd = 0xff00; var stateio = 0; var stated = 0; var res = LabJack.DigitalIO(ref ljID, 0, ref trisd, 0, ref stated, ref stateio, 0, ref outputd); if (res != 0) { throw new Exception("Error reading digital inputs"); } return(stated); } #endif }
//--------------------------------------------------------------------- // This is our function that read's analog inputs // public static float ReadAnalogInput(int channel) { int ljID = -1; int overVoltage = 0; float voltage = 0.0f; int result = LabJack.EAnalogIn(ref ljID, 0, channel, 0, ref overVoltage, ref voltage); if (result != 0) { ThrowErrorMessage("Error reading analog input", result); } return(voltage); }
public static void ReadTemperatureHumidity(out float tempC, out float rh) { #if MOCKUP tempC = rh = 0.0f; #elif DEBUG tempC = rh = 0.0f; #else lock (lockobj) { var ljID = 0; tempC = rh = 0.0f; var tempF = 0.0f; LabJack.SHT1X(ref ljID, 0, 0, 0 /*mode*/, 0 /*status*/, ref tempC, ref tempF, ref rh); } #endif }
//--------------------------------------------------------------------- // This returns an array of all the local IDs which we use. // public static int[] GetAllLabJacks() { // Make sure we allocate space for what is passed int[] productIDList = new int[127]; int[] serialNumList = new int[127]; int[] localIDList = new int[127]; int[] powerList = new int[127]; int[,] calMatrix = new int[127, 20]; int numFound = 0; int reserved1 = 0, reserved2 = 0; // Call the ListAll function. We must use the keyword ref for parameters // that aren't arrays that return data int result = LabJack.ListAll(productIDList, serialNumList, localIDList, powerList, calMatrix, ref numFound, ref reserved1, ref reserved2); if (result != 0) { ThrowErrorMessage("Unable to enumerate controllers", result); } int[] ljs = new int[numFound]; int i = 0; // count how many we found and set // the array which will be returned // to contain valid IDs foreach (int id in localIDList) { if (id != 9999) { ljs[i] = id; ++i; } } // return that array return(ljs); }
public static float ReadAnalogInput(int channel) { #if MOCKUP return(MockUp.GetAState(0)); #elif DEBUG return(RpiGPIO.GetAState(0)); #else lock (lockobj) { var ljID = 0; var overVoltage = 0; var voltage = 0.0f; var result = LabJack.EAnalogIn(ref ljID, 0, channel, 0, ref overVoltage, ref voltage); if (result != 0) { throw new Exception("Error reading analog input"); } return(voltage); } #endif }
//--------------------------------------------------------------------- // Set the analog outputs // public static void SetAnalogOutput(int channel, float voltage) { int ljID = -1; int result = 0; if (channel == 0) { result = LabJack.EAnalogOut(ref ljID, 0, voltage, -1.0f); } else if (channel == 1) { result = LabJack.EAnalogOut(ref ljID, 0, -1.0f, voltage); } else { throw new Exception("Invalid analog output channel"); } if (result != 0) { ThrowErrorMessage("Error reading analog input", result); } }
public static long ReadCounter(bool reset) { #if MOCKUP return(0); #elif DEBUG return(0); #else lock (lockobj) { var ljID = 0; int stated = 0, stateio = 0; uint cnt = 0; long result = LabJack.Counter(ref ljID, 0, ref stated, ref stateio, reset ? 1 : 0, 0, ref cnt); if (result != 0) { throw new Exception("Error reading the counter"); } return(cnt); } #endif }
//--------------------------------------------------------------------- // AIStream // public void AIStream() { int error; int ljID = -1; int demo = 1, numChannels = 4, disableCal = 0; int[] channels = { 0, 1, 2, 3 }; int[] gains = { 0, 0, 0, 0 }; float sr = 250.0F; int numScans = 250, timeout = 10; float[,] voltages = new float[4096, 4]; int[] stateIOout = new int[4096]; int ljb = -1; int ov = -1; int numIts = 5; int reserved = 0; Console.WriteLine("AIStream"); error = LabJack.AIStreamStart(ref ljID, demo, 0, 0, 1, numChannels, channels, gains, ref sr, disableCal, 0, 0); if (error != 0) { richTextBox3.AppendText("AIStreamStart Error: " + error + "\r\n"); } int i = 0; while ((error == 0) && (i < numIts)) { for (int j = 0; j < 400; j++) { stateIOout[j] = 0; voltages[j, 3] = 0; //for (int k = 0; k < 4; k++) //{ //} double time = (double)j / numSample; chart3.Series["Second Harmonic"].Points.AddXY(time, voltages[0, 2]); } error = LabJack.AIStreamRead(ljID, numScans, timeout, voltages, stateIOout, ref reserved, ref ljb, ref ov); if (error != 0) { StringBuilder sb = new StringBuilder(); LabJack.GetErrorString(error, sb); richTextBox3.AppendText("AIStreamRead Error: " + sb + "\r\n"); } //richTextBox3.AppendText("1st Scan: V1="+ voltages[0, 0]+", V2="+ voltages[0, 1]+", V3="+ voltages[0, 2]+", V4="+ voltages[0, 3]+"\r\n"); richTextBox3.AppendText("1st Scan: V3=" + voltages[0, 2] + "\r\n"); richTextBox3.AppendText("LabJack Scan Backlog = " + ljb + "\r\n"); i++; chart3.ChartAreas["ChartArea1"].AxisX.Minimum = 0; chart3.ChartAreas["ChartArea1"].AxisX.RoundAxisValues(); } LabJack.AIStreamClear(ljID); }