/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using log4net; using TBF.Resources; using TBF.BenchControl.WaterMeters.iPerl; namespace TBF.BenchControl.TestMethods.iPerlAdjustment { public partial class iPerlAdjustmentForm : Form, GenericDevices.IHasCompleted { private static readonly ILog log = LogManager.GetLogger(typeof(iPerlAdjustmentForm)); [DllImport("libRfid.dll", EntryPoint = "getDllVersion")] public static extern unsafe int getDllVersion(); [DllImport("libRfid.dll", EntryPoint = "openPort")] static extern unsafe int openPort(uint comPort); [DllImport("libRfid.dll", EntryPoint = "closePort")] static extern unsafe int closePort(); [DllImport("libRfid.dll", EntryPoint = "readRequestPort")] static extern unsafe int readRequestPort(MessageID messageId, int offset, int lenght, byte* ptr, int timeout_ms); [DllImport("libRfid.dll", EntryPoint = "writeRequestPort")] static extern unsafe int writeRequestPort(MessageID messageId, int offset, int lenght, byte* ptr, int timeout_ms); /// /// Wrapper function with safe interface adn unsafe body /// /// Value returned by readRequestPort(...) public unsafe int ReadRequestPort(MessageID messageID, int offset, int lenght, out byte[] buffer, int timeout) { byte[] buf = new byte[1000]; int retv; fixed (byte* pBuf = buf) { retv = readRequestPort(messageID, offset, lenght, pBuf, timeout); buffer = new byte[lenght]; for (int i = 0; i < lenght; i++) buffer[i] = buf[i]; } return retv; } /// /// Wrapper function with safe interface adn unsafe body /// /// Value returned by writeRequestPort(...) public unsafe int WriteRequestPort(MessageID messageID, int offset, int lenght, byte[] buffer, int timeout) { int retv; fixed (byte* pBuf = buffer) { retv = writeRequestPort(messageID, offset, lenght, pBuf, timeout); } return retv; } // Set to 'true' when the form closes public bool Completed { get { return completed; } } bool completed; /// Number of text boxes for serial numbers public readonly int WaterMetersCount; readonly bool[] disabled; Entities.TestResult tr; int textBoxesCount; Label[] labels; TextBox[] errors; IList WaterMeters; Modbus.QuidoRS.QuidoRS quido; /// Parameterless constructor for 3 watermeters public iPerlAdjustmentForm() { InitializeComponent(); textBoxesCount = 48; labels = new Label[48] { wmErrorLabel1, wmErrorLabel2, wmErrorLabel3, wmErrorLabel4, wmErrorLabel5, wmErrorLabel6, wmErrorLabel7, wmErrorLabel8, wmErrorLabel9, wmErrorLabel10, wmErrorLabel11, wmErrorLabel12, wmErrorLabel13, wmErrorLabel14, wmErrorLabel15, wmErrorLabel16, wmErrorLabel17, wmErrorLabel18, wmErrorLabel19, wmErrorLabel20, wmErrorLabel21, wmErrorLabel22, wmErrorLabel23, wmErrorLabel24, wmErrorLabel25, wmErrorLabel26, wmErrorLabel27, wmErrorLabel28, wmErrorLabel29, wmErrorLabel30, wmErrorLabel31, wmErrorLabel32, wmErrorLabel33, wmErrorLabel34, wmErrorLabel35, wmErrorLabel36, wmErrorLabel37, wmErrorLabel38, wmErrorLabel39, wmErrorLabel40, wmErrorLabel41, wmErrorLabel42, wmErrorLabel43, wmErrorLabel44, wmErrorLabel45, wmErrorLabel46, wmErrorLabel47, wmErrorLabel48, }; errors = new TextBox[48] { wmErrorTextBox1, wmErrorTextBox2, wmErrorTextBox3, wmErrorTextBox4, wmErrorTextBox5, wmErrorTextBox6, wmErrorTextBox7, wmErrorTextBox8, wmErrorTextBox9, wmErrorTextBox10, wmErrorTextBox11, wmErrorTextBox12, wmErrorTextBox13, wmErrorTextBox14, wmErrorTextBox15, wmErrorTextBox16, wmErrorTextBox17, wmErrorTextBox18, wmErrorTextBox19, wmErrorTextBox20, wmErrorTextBox21, wmErrorTextBox22, wmErrorTextBox23, wmErrorTextBox24, wmErrorTextBox25, wmErrorTextBox26, wmErrorTextBox27, wmErrorTextBox28, wmErrorTextBox29, wmErrorTextBox30, wmErrorTextBox31, wmErrorTextBox32, wmErrorTextBox33, wmErrorTextBox34, wmErrorTextBox35, wmErrorTextBox36, wmErrorTextBox37, wmErrorTextBox38, wmErrorTextBox39, wmErrorTextBox40, wmErrorTextBox41, wmErrorTextBox42, wmErrorTextBox43, wmErrorTextBox44, wmErrorTextBox45, wmErrorTextBox46, wmErrorTextBox47, wmErrorTextBox48, }; //rects = new Rectangle[48]; //for (int i = 0; i < 48; i++) //{ // int row = i % 12; // int col = i / 12; // rects[i] = new Rectangle(350 + col * 557, 25 + row * 47, 230, 37); //} completed = false; tr = null; /// Attach to 'AdjustmentInProgress' handler UiBridge.Bridge.AdjustmentInProgressHandler += delegate(object sender, UiBridge.AdjustmentInProgressEventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(OnAdjustmentInProgress), sender, args); } else OnAdjustmentInProgress(sender, args); }; } /// /// Constructor /// /// Number of text boxes for serial numbers public iPerlAdjustmentForm(IList waterMeters) : this() { this.WaterMetersCount = waterMeters.Count; this.WaterMeters = new List(); foreach (var wm in waterMeters) this.WaterMeters.Add(wm as WaterMeters.iPerl.WaterMeter); ShuffleTextBoxes(this.WaterMetersCount, Program.LineSize); disabled = new bool[this.WaterMetersCount]; foreach (var comp in StateMachine.Components) { if (comp is Modbus.QuidoRS.QuidoRS) { quido = comp as Modbus.QuidoRS.QuidoRS; break; } } } /// /// Make sure the layout of labels/text boxes on the screen /// corresponds to the layout of watermeters of the test bench. /// /// Number of watermeters /// Number of watermeters in one line void ShuffleTextBoxes(int wmsCount, int lineSize) { if (wmsCount < textBoxesCount && lineSize > 0) { int nrLines = (wmsCount + lineSize - 1) / lineSize; int gap = (textBoxesCount - wmsCount) / nrLines; int dest = 0; for (int l = 0; l < nrLines; l++) { for (int i = 0; i < lineSize; i++) { labels[dest] = labels[l * lineSize + l * gap + i]; errors[dest] = errors[l * lineSize + l * gap + i]; dest++; } } textBoxesCount = wmsCount; } } void Localize() { Text = Strings.Water_Meter_States; for (int i = 0; i < textBoxesCount; i++) { labels[i].Text = string.Format("WM{0} {1}", i + 1, Strings.Error_pct); } okButton.Text = Strings.OkBtnText; } private void WMErrorsForm24_Load(object sender, EventArgs e) { Localize(); //Height = 50 + 90 * WaterMetersCount; for (int i = 0; i < textBoxesCount; i++) { if (disabled != null && i < disabled.Length && disabled[i]) { labels[i].Visible = errors[i].Visible = false; } else { labels[i].Visible = errors[i].Visible = true; errors[i].Text = "---"; } } } private void okButton_Click(object sender, EventArgs e) { completed = true; Close(); } void OnAdjustmentInProgress(object sender, UiBridge.AdjustmentInProgressEventArgs args) { tr = args.TestResult; Redraw(); } void Redraw() { if (tr != null) { for (int i = 0; i < textBoxesCount; i++) { errors[i].Text = tr.Meters[i].VolumeErrorPct.ToString("F1"); } } } private void WMErrorsForm_Paint(object sender, PaintEventArgs e) { //if (tr != null) //{ // System.Drawing.Graphics graphics = this.CreateGraphics(); // for (int i = 0; i < WaterMetersCount; i++) // { // PaintOne(graphics, rects[i], tr.Meters[i].VolumeErrorPct, tr.ErrLimLo, tr.ErrLimHi); // } //} } void PaintOne(System.Drawing.Graphics g, Rectangle r, float value, float limLo, float limHi) { Rectangle rect1 = new Rectangle(r.Left, r.Top, r.Width / 3, r.Height); Rectangle rect2 = new Rectangle(r.Left + r.Width / 3, r.Top, r.Width / 3, r.Height); Rectangle rect3 = new Rectangle(r.Left + 2 * (r.Width / 3), r.Top, r.Width / 3, r.Height); Rectangle rectCent = new Rectangle(r.Left + r.Width / 2 - 1, r.Top, 3, r.Height); Brush redBrush = new SolidBrush(Color.FromName("Salmon")); Brush greenBrush = new SolidBrush(Color.FromName("LightGreen")); Brush centBrush = new SolidBrush(Color.FromName("Green")); g.FillRectangle(redBrush, rect1); g.FillRectangle(greenBrush, rect2); g.FillRectangle(redBrush, rect3); g.FillRectangle(centBrush, rectCent); int x0 = r.Left + r.Width / 2; int dx = r.Height / 3; int x = (int)((float)r.Width * (2.0 * value - limLo - limHi) / (limHi - limLo) / 6.0f + 0.5f); if (x0 - dx + x >= r.Left && x0 + dx + x < r.Left + r.Width) { Point[] triangle = new Point[3] { new Point(x0 - dx + x, r.Top), new Point(x0 + dx + x, r.Top), new Point(x0 + x, r.Top + 2 * r.Height / 3), }; Brush blackBrush = new SolidBrush(Color.Black); g.FillPolygon(blackBrush, triangle); } } #region Forced close handling public void StartForceCloseHandler() { UiBridge.Bridge.CloseModelessFormHandler += delegate(object sender, EventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(OnForceClose), sender, args); } else OnForceClose(sender, args); }; } void OnForceClose(object sender, EventArgs args) { DialogResult = DialogResult.Cancel; Close(); } #endregion private void readConfigBtn_Click(object sender, EventArgs e) { int i = 0; for (int group = 1; group <= 10; group++) { if (quido != null) { quido.SetOutputs((ushort)(16 - group)); Thread.Sleep(200); } foreach (var wm in WaterMeters) { if (wm.Group == group) { Text = string.Format("WM={0} Group={1}", wm.Name, group); if (wm.DebugLevel == Entities.DebugMode.Normal) { byte[] data = null; bool success = false; int r1 = openPort((uint)wm.RfidComPortNr); for (int j = 0; j < 3; j++) { if (0 == ReadRequestPort(MessageID.Configuration, 0, ConfigStruct.Length, out data, 1000)) { success = true; break; } } closePort(); if (r1 == 0 && success && data != null) { errors[i].Text = ConfigStruct.FromByteArray(data).ToString(1); } else { errors[i].Text = "Cannot read configuration"; } } i++; } } } } private void toTestMdBtn_Click(object sender, EventArgs e) { byte[] data = new byte[1]; data[0] = (byte)7; int i = 0; for (int group = 1; group <= 10; group++) { if (quido != null) { quido.SetOutputs((ushort)(16 - group)); Thread.Sleep(200); } foreach (var wm in WaterMeters) { if (wm.DebugLevel == Entities.DebugMode.Normal && wm.Group == group) { int r1 = openPort((uint)wm.RfidComPortNr); int r2 = WriteRequestPort(MessageID.Command, 0, 1, data, 1000); int r3 = closePort(); } else i++; } } } private void toActiveMdBtn_Click(object sender, EventArgs e) { byte[] data = new byte[1]; data[0] = (byte)6; int i = 0; for (int group = 1; group <= 10; group++) { if (quido != null) { quido.SetOutputs((ushort)(16 - group)); Thread.Sleep(200); } foreach (var wm in WaterMeters) { if (wm.DebugLevel == Entities.DebugMode.Normal && wm.Group == group) { int r1 = openPort((uint)wm.RfidComPortNr); int r2 = WriteRequestPort(MessageID.Command, 0, 1, data, 1000); int r3 = closePort(); } else i++; } } } } }