public override async Task <int> Run() { try { await YAPI.RegisterHub(HubURL); YCurrent sensor; YCurrent sensorDC = null; YCurrent sensorAC = null; if (Target.ToLower() == "any") { // retreive any voltage sensor (can be AC or DC) sensor = YCurrent.FirstCurrent(); if (sensor == null) { WriteLine("No module connected"); return(-1); } Target = await(await sensor.get_module()).get_serialNumber(); } WriteLine("using " + Target); // we need to retreive both DC and AC from the device. sensorDC = YCurrent.FindCurrent(Target + ".current1"); sensorAC = YCurrent.FindCurrent(Target + ".current2"); while (await sensorDC.isOnline()) { Write("DC: " + await sensorDC.get_currentValue() + " mA / "); WriteLine("AC: " + await sensorAC.get_currentValue() + " mA "); await YAPI.Sleep(1000); } WriteLine("Module not connected (check identification and USB cable)"); } catch (YAPI_Exception ex) { WriteLine("error: " + ex.Message); } YAPI.FreeAPI(); return(0); }
static void Main(string[] args) { string errmsg = ""; string target; YCurrent sensor; YCurrent sensorDC = null; YCurrent sensorAC = null; YModule m = null; if (args.Length < 1) { usage(); } target = args[0].ToUpper(); // Setup the API to use local USB devices if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS) { Console.WriteLine("RegisterHub error: " + errmsg); Environment.Exit(0); } if (target == "ANY") { // retreive any voltage sensor (can be AC or DC) sensor = YCurrent.FirstCurrent(); if (sensor == null) { die("No module connected"); } } else { sensor = YCurrent.FindCurrent(target + ".current1"); } // we need to retreive both DC and AC voltage from the device. if (sensor.isOnline()) { m = sensor.get_module(); sensorDC = YCurrent.FindCurrent(m.get_serialNumber() + ".current1"); sensorAC = YCurrent.FindCurrent(m.get_serialNumber() + ".current2"); } else { die("Module not connected"); } if (!m.isOnline()) { die("Module not connected"); } while (m.isOnline()) { Console.Write("DC: " + sensorDC.get_currentValue().ToString() + " mA "); Console.Write("AC: " + sensorAC.get_currentValue().ToString() + " mA "); Console.WriteLine(" (press Ctrl-C to exit)"); YAPI.Sleep(1000, ref errmsg); } YAPI.FreeAPI(); }
private void refreshUI() { // draw the dial. double value = -5; bool on = false; if (comboBox1.Enabled) { // if a yocto-amp device is connected, lets check it value YModule m = (YModule)comboBox1.Items[comboBox1.SelectedIndex]; YCurrent DC = YCurrent.FindCurrent(m.get_serialNumber() + ".current1"); YCurrent AC = YCurrent.FindCurrent(m.get_serialNumber() + ".current2"); if (DC.isOnline()) { // read DC or AC value, according to ACDCcheckBox if (ACDCcheckBox.Checked) { value = 100 * (AC.get_currentValue() / maxvalue); } else { value = 100 * (DC.get_currentValue() / maxvalue); } on = true; } } // lets use a double buffering technique to avoid flickering Bitmap BackBuffer = new Bitmap(on ? Properties.Resources.bg : Properties.Resources.bgoff); Graphics buffer = Graphics.FromImage(BackBuffer); buffer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; int DialWidth = BackBuffer.Width; int DialHeight = BackBuffer.Height; // add inertia to the needle needleposition = needleposition + (value - needleposition) / 10; // make sure une needle won't go off chart if (needleposition < -5) { needleposition = -5; } if (needleposition > 105) { needleposition = 105; } double angle = 3.1416 * (180 - 30 - 120 * (needleposition / 100)) / 180; int x = Convert.ToInt32(DialWidth / 2 + Math.Cos(angle) * (DialHeight - 15)); int y = Convert.ToInt32(DialHeight * 1.066 - Math.Sin(angle) * (DialHeight - 15)); // draw the needle shadow Pen shadow = new Pen(Color.FromArgb(16, 0, 0, 00), 3); Point point1 = new Point(DialWidth / 2 - 3, DialHeight + 3); Point point2 = new Point(Convert.ToInt32(x) - 3, Convert.ToInt32(y) + 3); buffer.DrawLine(shadow, point1, point2); // draw the needle Pen red = new Pen(on ? Color.FromArgb(255, 255, 0, 00) : Color.FromArgb(255, 64, 0, 00), 3); point1 = new Point(DialWidth / 2, DialHeight); point2 = new Point(Convert.ToInt32(x), Convert.ToInt32(y)); buffer.DrawLine(red, point1, point2); // draw the scale FontFamily fontFamily = new FontFamily("Arial"); Font font = new Font(fontFamily, DialHeight / 10, FontStyle.Regular, GraphicsUnit.Pixel); buffer.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 20, 20, 20)); for (int i = 0; i <= 10; i++) { double dvalue = ((maxvalue / 1000) * i / 10); angle = 3.1416 * (180 - 30 - 120 * (i / 10.0)) / 180; string text = dvalue.ToString(); SizeF size = buffer.MeasureString(text, font); int tx = Convert.ToInt32(DialWidth / 2 + Math.Cos(angle) * DialHeight * 1.01 - size.Width / 2); int ty = Convert.ToInt32(DialHeight * 1.066 - Math.Sin(angle) * DialHeight * 0.98 - size.Height / 2); buffer.DrawString(dvalue.ToString(), font, solidBrush, new PointF(tx, ty)); } Bitmap frame = new Bitmap(Properties.Resources.frame); buffer.DrawImage(frame, 0, 0); Graphics Viewable = pictureBox1.CreateGraphics(); // fast rendering //Viewable.DrawImageUnscaled(BackBuffer, 0, 0); // slower, but pictureBox can be resized, rendering will still be ok, // try to respect a 2:1 ratio anyway Viewable.DrawImage(BackBuffer, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); Viewable.Dispose(); }