我想问你们有关在一个项目中将一个串口传递给多个表单的人。我完成了从“主”窗体传递到“Form1”,但我不知道如何通过按钮,组合框等处理它。我想以单独的形式更改串口参数。我在c#中很新,我无法理解。
形式“主要”
namespace RS232
{
public partial class mainform : Form
{
Form1 frm1;
Form2 frm2;
public mainform()
{
InitializeComponent();
this.Text = "Mitasubaszi controller";
frm1 = new Form1(sport);
frm2 = new Form2();
}
public void toolStripMenuItem9_Click(object sender, EventArgs e)
{
DialogResult resultquit;
resultquit = MessageBox.Show("Are you sure?","Warning", MessageBoxButtons.YesNo);
if (resultquit == System.Windows.Forms.DialogResult.Yes)
{
if (sport.IsOpen) sport.Close();
Close();
}
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
frm1.ShowDialog();
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
frm2.ShowDialog();
}
}
}
表格“Form1”
namespace RS232
{
public partial class Form1 : Form
{
int brate;
public Form1(System.IO.Ports.SerialPort main)
{
InitializeComponent();
initvalue(main);
}
public void initvalue(System.IO.Ports.SerialPort initport)
{
boxbaud.SelectedIndex = 5;
boxdata.SelectedIndex = 3;
boxpar.SelectedIndex = 0;
boxstop.SelectedIndex = 2;
checkDTR.Checked = true;
checkRTS.Checked = true;
initport.BaudRate = 9600;
initport.DataBits = 8;
initport.PortName = "COM1";
initport.DtrEnable = true;
initport.RtsEnable = true;
initport.Parity = System.IO.Ports.Parity.Even;
initport.StopBits = System.IO.Ports.StopBits.Two;
}
public void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void DTR_CheckedChanged(object sender, EventArgs e)
{
if (checkDTR.Checked) .DtrEnable = true;
else .DtrEnable = false;
}
public void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
try
{
if (.IsOpen) .Close();
int comvalue = Convert.ToInt32(numericUpDown1.Value);
string comport = String.Format("COM{0}", comvalue);
}
catch
{
MessageBox.Show("Port does not exist");
}
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (boxbaud.SelectedIndex == -1) MessageBox.Show("Baud Rate is not selected", "Warning");
else
{
brate = Convert.ToInt32(boxbaud.SelectedItem);
}
}
private void button7_Click(object sender, EventArgs e)
{
string stat;
if (.IsOpen) stat = "Open";
else stat = "Close";
string br = Convert.ToString(.BaudRate);
string dts = Convert.ToString(.DtrEnable);
string com = Convert.ToString(.PortName);
string par = Convert.ToString(.Parity);
string dat = Convert.ToString(.DataBits);
string rts = Convert.ToString(.RtsEnable);
string stop = Convert.ToString(.StopBits);
string text = string.Format("Status: {0}\nPort: {1}\nBaud rate: {2}\nData bits: {3}\nParity: {4}\nStop bits: {5}\nDTS: {6}\nRTS {7}", stat, com, br, dat, par, stop, dts, rts);
MessageBox.Show(text);
}
private void checkRTS_CheckedChanged(object sender, EventArgs e)
{
if (checkRTS.Checked) .RtsEnable = true;
else .RtsEnable = false;
}
private void boxpar_SelectedIndexChanged(object sender, EventArgs e)
{
if (boxpar.SelectedItem == "Even") .Parity = System.IO.Ports.Parity.Even;
if (boxpar.SelectedItem == "Mark") .Parity = System.IO.Ports.Parity.Mark;
if (boxpar.SelectedItem == "None") .Parity = System.IO.Ports.Parity.None;
if (boxpar.SelectedItem == "Odd") .Parity = System.IO.Ports.Parity.Odd;
if (boxpar.SelectedItem == "Space") .Parity = System.IO.Ports.Parity.Space;
}
private void boxdata_SelectedIndexChanged(object sender, EventArgs e)
{
if (boxdata.SelectedIndex == -1) MessageBox.Show("Data bits are not selected", "Warning");
else
{
int bdata = Convert.ToInt32(boxdata.SelectedItem);
.DataBits = bdata;
}
}
private void boxstop_SelectedIndexChanged(object sender, EventArgs e)
{
if (boxstop.SelectedItem == "1") .StopBits = System.IO.Ports.StopBits.One;
if (boxstop.SelectedItem == "1.5") .StopBits = System.IO.Ports.StopBits.OnePointFive;
if (boxstop.SelectedItem == "2") .StopBits = System.IO.Ports.StopBits.Two;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (!.IsOpen) .Open();
}
catch
{
MessageBox.Show("Invalid port parameters");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (.IsOpen) .Close();
}
}
}
如果你想在你的整个应用程序中有一个SerialPort
,你可以使用类型为static
的静态属性创建一个SerialPort
类。
public static class SerialPortCommunicator
{
private static SerialPort _serialPort=new SerialPort();
public static SerialPort SerialPort
{
get { return _serialPort; }
set { _serialPort = value; }
}
}
现在,您可以从项目中的任何位置更改其属性的值。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_InForm1_Click(object sender, EventArgs e)
{
SerialPortCommunicator.SerialPort.DtrEnable = true;
// Manipulate every other properties here (inside this from)
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_InFrom2_Click(object sender, EventArgs e)
{
SerialPortCommunicator.SerialPort.BaudRate = 4900;
// Manipulate every other properties here(inside this form as well)
}
}