Skip to content

Commit

Permalink
循环时禁止发送及接收设置
Browse files Browse the repository at this point in the history
  • Loading branch information
cnxy committed May 11, 2021
1 parent 0331216 commit 8c715fd
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions VISAInstrument/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private bool Write()
_isWritingError = false;
if (string.IsNullOrEmpty(txtCommand.Text))
{
Invoke(new Action(() => MessageBox.Show(Resources.CommandNotEmpty)));
Invoke(new Action(() => MessageBox.Show(this,Resources.CommandNotEmpty)));
return false;
}
string asciiString = string.Empty;
Expand All @@ -114,7 +114,7 @@ private bool Write()
else
{
_isWritingError = true;
Invoke(new Action(() => MessageBox.Show(@"转换字节失败,请按照“XX XX XX”格式输入内容")));
Invoke(new Action(() => MessageBox.Show(this,@"转换字节失败,请按照“XX XX XX”格式输入内容")));
return false;
}
}
Expand Down Expand Up @@ -144,11 +144,10 @@ private bool Write()
_portOperatorBase.Write(byteArray);
}
}

}
catch
catch(Exception ex)
{
Invoke(new Action(() => MessageBox.Show($@"写入命令“{txtCommand.Text}”失败!")));
Invoke(new Action(() => MessageBox.Show(this,$@"写入命令“{txtCommand.Text}”失败!\r\n{ex.Message}")));
return false;
}
Invoke(new Action(() => DisplayToTextBox($"[Time:{stopwatch.ElapsedMilliseconds}ms] Write: {txtCommand.Text}")));
Expand Down Expand Up @@ -225,7 +224,7 @@ private bool NewPortInstance(out string message)
() =>
{
string message1 = string.Empty;
if (cboRS232.SelectedIndex == -1) return message1;
if (cboRS232.SelectedIndex == -1) return "没有串口选中";
try
{
_portOperatorBase = new RS232PortOperator(((Pair<string, string>)cboRS232.SelectedItem).Value.ToString(),
Expand All @@ -245,7 +244,7 @@ private bool NewPortInstance(out string message)
() =>
{
string message2 = string.Empty;
if (cboUSB.SelectedIndex == -1) return message2;
if (cboUSB.SelectedIndex == -1) return "没有USB选中";
try
{
_portOperatorBase = new USBPortOperator(cboUSB.SelectedItem.ToString());
Expand All @@ -261,7 +260,7 @@ private bool NewPortInstance(out string message)
() =>
{
string message3 = string.Empty;
if (cboGPIB.SelectedIndex == -1) return message3;
if (cboGPIB.SelectedIndex == -1) return "没有GPIB选中";
try
{
_portOperatorBase = new GPIBPortOperator(cboGPIB.SelectedItem.ToString());
Expand All @@ -277,7 +276,7 @@ private bool NewPortInstance(out string message)
() =>
{
string message4 = string.Empty;
if (cboLAN.SelectedIndex == -1) return message4;
if (cboLAN.SelectedIndex == -1) return "没有LAN选中";
try
{
_portOperatorBase = new LANPortOperator(cboLAN.SelectedItem.ToString());
Expand All @@ -303,7 +302,7 @@ private void DisplayToTextBox(string content)

private void ClearIfTextBoxOverFlow()
{
if (txtDisplay.Text.Length > 20480) txtDisplay.Clear();
if (txtDisplay.Text.Length > 204800) txtDisplay.Clear();
}

Task t = null;
Expand Down Expand Up @@ -460,7 +459,7 @@ private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)

if (t != null && !t.IsCompleted)
{
MessageBox.Show(Resources.LoadingInstrumentResource);
MessageBox.Show(this,Resources.LoadingInstrumentResource);
e.Cancel = true;
return;
}
Expand All @@ -477,25 +476,25 @@ private void btnCheckIP_Click(object sender, EventArgs e)
{
if (!txtIPAddress.Text.IsMatch(IpRegex))
{
MessageBox.Show(Resources.NotCorrectIP);
MessageBox.Show(this,Resources.NotCorrectIP);
txtIPAddress.SetSelect();
return;
}
if (cboLAN.Items.Cast<object>().Any(item => ((string)item).Contains(txtIPAddress.Text)))
{
MessageBox.Show(Resources.LANContainIP);
MessageBox.Show(this,Resources.LANContainIP);
txtIPAddress.SetSelect();
return;
}
if (!PortUltility.OpenIPAddress(txtIPAddress.Text, out string fullAddress))
{
MessageBox.Show(Resources.NotDetectIP);
MessageBox.Show(this,Resources.NotDetectIP);
txtIPAddress.SetSelect();
return;
}
cboLAN.Items.Add(fullAddress);
cboLAN.Text = cboLAN.Items[cboLAN.Items.Count-1].ToString();
MessageBox.Show(Resources.DetectOK);
MessageBox.Show(this,Resources.DetectOK);
}

private void githubToolStripMenuItem1_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -645,7 +644,13 @@ private void rdoSend_CheckedChanged(object sender, EventArgs e)

private void EnableCycle(bool enabled)
{
Invoke(new Action(() => flowLayoutPanel9.Enabled = !enabled));
Invoke(new Action(() =>
{
flowLayoutPanel9.Enabled = !enabled;
groupBox1.Enabled = !enabled;
groupBox2.Enabled = !enabled;
txtCommand.Enabled = !enabled;
}));
cycleEnabled = enabled;
}

Expand Down Expand Up @@ -684,15 +689,13 @@ private void btnCycle_Click(object sender, EventArgs e)
EnableCycle(false);
break;
}
bool isSuccessful;
if (rdoSend.Checked) isSuccessful = Write();
else isSuccessful = Query();
bool isSuccessful = rdoSend.Checked ? Write() : Query();
if (!isSuccessful)
{
EnableCycle(false);
break;
}
else Thread.Sleep(intervalTime);
Thread.Sleep(intervalTime);
if(cycleCount != 0) count++;
}
Invoke(new Action(() => btnCycle.Text = originalCycleText));
Expand All @@ -705,7 +708,7 @@ private bool CheckCycleEnable(string operationName)
bool cycleEnabledTemp = cycleEnabled;
if (cycleEnabledTemp)
{
MessageBox.Show($"请停止循环操作后再执行{operationName}操作");
MessageBox.Show(this,$"请停止循环操作后再执行{operationName}操作");
}
return cycleEnabledTemp;
}
Expand Down

0 comments on commit 8c715fd

Please sign in to comment.