private void cbxAoChannel_SelectedIndexChanged(object sender, EventArgs e) { int iRetryLimit = 10; int iRetryCount = 0; if (cbxAoChannel.SelectedIndex != this.m_iAoChTotal) { if (timer1.Enabled) { timer1.Enabled = false; RefreshAoChannelRange(cbxAoChannel.SelectedIndex, true); if (cbxAoChannel.SelectedIndex >= 0) { dgViewAoChannelInfo.Rows[cbxAoChannel.SelectedIndex].Selected = true; } timer1.Enabled = true; } else { RefreshAoChannelRange(cbxAoChannel.SelectedIndex, true); if (cbxAoChannel.SelectedIndex >= 0) { dgViewAoChannelInfo.Rows[cbxAoChannel.SelectedIndex].Selected = true; } } gpBoxAoSetValue.Visible = true; for (int i = 0; i < iRetryLimit; i++) { AnalogOutput.GetRangeHighLow(m_Adam6000Type, m_usRange[cbxAoChannel.SelectedIndex], out m_fHigh, out m_fLow); if ((m_fHigh == 0.0f) && (m_fLow == 0.0f)) { iRetryCount++; System.Threading.Thread.Sleep(150); continue; } else { break; } } txtSelAoChannel.Text = cbxAoChannel.SelectedIndex.ToString(); RefreshOutputPanel(m_fHigh, m_fLow); if (iRetryCount >= iRetryLimit) { MessageBox.Show("AnalogOutput.GetRangeHighLow failed! Please retry again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); } } else { gpBoxAoSetValue.Visible = false; } }
private void btnSetAsSafety_Click(object sender, EventArgs e) { if (!CheckControllable()) { return; } try { float fVal, fHigh, fLow; int iChannelTotal = this.m_aConf.HwIoTotal[m_tmpidx]; //Get range higf & low AnalogOutput.GetRangeHighLow(m_usRanges[m_iSelChannels], out fHigh, out fLow); if (fHigh - fLow == 0) { MessageBox.Show("GetRangeHighLow() failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } //convert output value to float fVal = 0.0f; if (txtOutputVal.Text != null && txtOutputVal.Text.Length > 0) { try { fVal = Convert.ToSingle(txtOutputVal.Text); } catch { System.Windows.Forms.MessageBox.Show("Invalid value: " + txtOutputVal.Text); } } ushort[] usAOSafetyVals = new ushort[m_usAOSafetyVals.Length]; Array.Copy(m_usAOSafetyVals, 0, usAOSafetyVals, 0, m_usAOSafetyVals.Length); usAOSafetyVals[m_iSelChannels] = Convert.ToUInt16(65535.0f * ((fVal - fLow) / (fHigh - fLow))); if (!m_adamSocket.AnalogOutput().SetSafetyValues(m_idxID, iChannelTotal, usAOSafetyVals)) { MessageBox.Show("Set safety value failed! (Err: " + m_adamSocket.Modbus().LastError.ToString() + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); } RefreshSafetySetting(); } catch { return; } return; }
/// <summary> /// Apply setting when user configure safety status /// </summary> /// <param name="bVal"></param> private void formSafety_ApplySafetyValueClick(string[] szVal) { int iChannelTotal = this.m_aConf.HwIoTotal[m_tmpidx]; float fVal, fHigh, fLow; ushort[] usAOSafetyVals = new ushort[m_usAOSafetyVals.Length]; for (int i = 0; i < iChannelTotal; i++) { fVal = 0.0f; if (szVal[i] != null && szVal[i].Length > 0) { try { fVal = Convert.ToSingle(szVal[i]); } catch { System.Windows.Forms.MessageBox.Show("Invalid value: " + szVal[i]); } } AnalogOutput.GetRangeHighLow(m_usRanges[i], out fHigh, out fLow); if (fHigh - fLow == 0) { MessageBox.Show("GetRangeHighLow() failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } if (fVal > fHigh || fVal < fLow) { MessageBox.Show("Channel " + i.ToString() + " is illegal value! Please enter the value " + fLow.ToString() + " ~ " + fHigh.ToString() + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } usAOSafetyVals[i] = Convert.ToUInt16(65535.0f * ((fVal - fLow) / (fHigh - fLow))); } if (!m_adamSocket.AnalogOutput().SetSafetyValues(m_idxID, iChannelTotal, usAOSafetyVals)) { MessageBox.Show("Set safety value failed! (Err: " + m_adamSocket.Modbus().LastError.ToString() + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); } RefreshSafetySetting(); }
/// <summary> /// Refresh AI Channel Information /// </summary> /// <returns></returns> private bool RefreshData() { int iChannelTotal = this.m_aConf.HwIoTotal[m_tmpidx]; ushort[] usVal; string[] strVal; float fHigh = 0, fLow = 0; if (!m_adamSocket.AnalogOutput().GetValues(m_idxID, iChannelTotal, out usVal)) { StatusBar_IO.Text += "ApiErr:" + m_adamSocket.Modbus().LastError.ToString() + " "; return(false); } strVal = new string[usVal.Length]; for (int i = 0; i < iChannelTotal; i++) { if (this.IsShowRawData) { strVal[i] = usVal[i].ToString("X04"); } else { strVal[i] = AnalogOutput.GetScaledValue(this.m_usRanges[i], usVal[i]).ToString(AnalogOutput.GetFloatFormat(this.m_usRanges[i])); } listViewChInfo.Items[i].SubItems[3].Text = strVal[i].ToString(); //moduify "Value" column } //Update tBarOutputVal if (!b_AOValueModified) { int idx = 0; for (int i = 0; i < listViewChInfo.Items.Count; i++) { if (listViewChInfo.Items[i].Selected) { idx = i; } } AnalogOutput.GetRangeHighLow(m_usRanges[idx], out fHigh, out fLow); RefreshOutputPanel(fHigh, fLow, AnalogOutput.GetScaledValue(this.m_usRanges[idx], usVal[idx])); } return(true); }
private void btnSetAoValue_Click(object sender, EventArgs e) { float fVal, fHigh, fLow; ushort usValue; int iPresetRegAddr = 0; byte i_byResolution = 12; string strVal = txtAoOutputVal.Text.ToString().Trim(); if (strVal.Length == 0) { MessageBox.Show("Illegal value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } fVal = ConvertUSSingle(strVal); AnalogOutput.GetRangeHighLow(m_Adam6000Type, m_usRange[cbxAoChannel.SelectedIndex], out fHigh, out fLow); if (fHigh - fLow == 0) { MessageBox.Show("GetRangeHighLow() failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } if (fVal > fHigh || fVal < fLow) { MessageBox.Show("Illegal value! Please enter the value " + fLow.ToString() + " ~ " + fHigh.ToString() + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } usValue = Convert.ToUInt16((Convert.ToSingle(System.Math.Pow(2, i_byResolution) - 1) / (fHigh - fLow)) * (fVal - fLow)); iPresetRegAddr = m_iAoValueStartAddr + cbxAoChannel.SelectedIndex; if (adamModbus.Modbus().PresetSingleReg(iPresetRegAddr, (int)usValue)) { m_usAoValue[cbxAoChannel.SelectedIndex] = usValue; } else { MessageBox.Show("PresetSingleReg() failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); } cbxAoChannel_SelectedIndexChanged(this, null); }
private void btnSetAsStartup_Click(object sender, EventArgs e) { if (!CheckControllable()) { return; } float fVal, fHigh, fLow; timer1.Enabled = false; if (txtOutputVal.Text.Length == 0) { MessageBox.Show("Illegal value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } try { //Get range higf & low AnalogOutput.GetRangeHighLow(m_usRanges[m_iSelChannels], out fHigh, out fLow); if (fHigh - fLow == 0) { MessageBox.Show("GetRangeHighLow() failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } //convert output value to float fVal = 0.0f; if (txtOutputVal.Text != null && txtOutputVal.Text.Length > 0) { try { fVal = Convert.ToSingle(txtOutputVal.Text); } catch { System.Windows.Forms.MessageBox.Show("Invalid value: " + txtOutputVal.Text); } } if (fVal > fHigh || fVal < fLow) { MessageBox.Show("Illegal value! Please enter the value " + fLow.ToString() + " ~ " + fHigh.ToString() + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } ushort[] usStartupAO = new ushort[m_usStartupAO.Length]; Array.Copy(m_usStartupAO, 0, usStartupAO, 0, m_usStartupAO.Length); usStartupAO[m_iSelChannels] = Convert.ToUInt16(65535.0f * ((fVal - fLow) / (fHigh - fLow))); if (m_adamSocket.AnalogOutput().SetStartupValues(this.m_idxID, usStartupAO)) { MessageBox.Show("Set AO startup values done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); RefreshAoStartupValues(); } else { MessageBox.Show("Set AO startup values failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); } } catch { MessageBox.Show("Illegal value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } timer1.Enabled = true; }
private void btnApplyOutput_Click(object sender, EventArgs e) { b_AOValueModified = false; if (!CheckControllable()) { return; } timer1.Enabled = false; float fVal, fHigh, fLow; if (txtOutputVal.Text.Length == 0) { MessageBox.Show("Illegal value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } try { //Get range higf & low AnalogOutput.GetRangeHighLow(m_usRanges[m_iSelChannels], out fHigh, out fLow); if (fHigh - fLow == 0) { MessageBox.Show("GetRangeHighLow() failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } //convert output value to float fVal = 0.0f; if (txtOutputVal.Text != null && txtOutputVal.Text.Length > 0) { try { fVal = Convert.ToSingle(txtOutputVal.Text); } catch { System.Windows.Forms.MessageBox.Show("Invalid value: " + txtOutputVal.Text); } } if (fVal > fHigh || fVal < fLow) { MessageBox.Show("Illegal value! Please enter the value " + fLow.ToString() + " ~ " + fHigh.ToString() + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } //Set channel value if (this.m_adamSocket.AnalogOutput().SetCurrentValue(this.m_idxID, m_iSelChannels, m_usRanges[m_iSelChannels], fVal)) { RefreshOutputPanel(fHigh, fLow, fVal); } else { MessageBox.Show("Change current value failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } RefreshData(); MessageBox.Show("Set output value done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); timer1.Enabled = true; }
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { string strSelPageName = tabControl1.TabPages[tabControl1.SelectedIndex].Text; int idx; float fHigh = 0, fLow = 0, fVal = 0; ushort usVal; StatusBar_IO.Text = ""; m_adamSocket.Disconnect(); m_adamSocket.Connect(m_adamSocket.GetIP(), protoType, portNum); if (strSelPageName == "Module Information") { m_iFailCount = 0; m_iScanCount = 0; } else if (strSelPageName == "AO") { RefreshRanges(); RefreshAoStartupValues(); RefreshSafetySetting(); chbxEnableSafety.Checked = m_bIsEnableSafetyFcn; //Set AO info idx = 0; //initial index if (m_adamSocket.AnalogOutput().GetCurrentValue(m_idxID, idx, out usVal)) { AnalogOutput.GetRangeHighLow(m_usRanges[idx], out fHigh, out fLow); fVal = AnalogOutput.GetScaledValue(m_usRanges[idx], usVal); RefreshOutputPanel(fHigh, fLow, fVal); } else { this.StatusBar_IO.Text += "GetValues() filed!"; } //synchronize channel status for (int i = 0; i < listViewChInfo.Items.Count; i++) { if (i == idx) { listViewChInfo.Items[i].Selected = true; } else { listViewChInfo.Items[i].Selected = false; } } string szTemp = AnalogOutput.GetRangeName(m_usRanges[idx]); for (int i = 0; i < cbxRange.Items.Count; i++) { if (szTemp == cbxRange.Items[i].ToString()) { cbxRange.SelectedIndex = i; break; } } } if (tabControl1.SelectedIndex == 0) { this.timer1.Enabled = false; } else { this.timer1.Enabled = true; } }